萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 動態生成 HTML/CSS 柱狀圖:無依賴、零配置的數據可視化

需求

在無法使用 JavaScript 圖表庫(如 ECharts、Chart.js)的環境(純後端渲染、郵件模板、PDF 生成、舊瀏覽器兼容),需用純 PHP + HTML/CSS 繪製簡單柱狀圖。

核心思路

  1. 查詢數據:總量、各階段通過量。
  2. 計算百分比:height = floor((stage_count / total_count) * max_height)。
  3. 輸出 HTML 表格:每欄一個 <div> 高度對應百分比,底部對齊 (vertical-align: bottom)。

完整實現

<?php
// config.php 已包含 PDO 實例 $pdo
require __DIR__ . '/../config.php';

// 1. 計算時間範圍:最近 30 天
$endDate   = new DateTime('today');
$startDate = (clone $endDate)->modify('-30 days');
$startStr  = $startDate->format('Y-m-d');
$endStr    = $endDate->format('Y-m-d');

// 2. 查詢數據
$baseSql = "SELECT COUNT(*) FROM newpro WHERE p_date >= ? AND p_date < ?";
$total   = (int)$pdo->query($baseSql, [$startStr, $endStr])->fetchColumn();

$stage1  = (int)$pdo->query($baseSql . " AND is_pa_check_first = 1", [$startStr, $endStr])->fetchColumn();
$stage2  = (int)$pdo->query($baseSql . " AND is_pa_check_first = 1 AND is_pa_check_second = 1", [$startStr, $endStr])->fetchColumn();
$stage3  = (int)$pdo->query($baseSql . " AND is_pa_check_first = 1 AND is_pa_check_second = 1 AND is_pa_check_third = 1", [$startStr, $endStr])->fetchColumn();

$maxHeight = 200; // 最大柱高 px
$bars = [
    ['總數',      $total],
    ['一審通過',  $stage1],
    ['二審通過',  $stage2],
    ['三審通過',  $stage3],
];
?>
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
    <meta charset="UTF-8">
    <title>審核流程漏斗圖</title>
    <style>
        .chart-table { border-collapse: collapse; font-family: sans-serif; }
        .chart-table td { text-align: center; vertical-align: bottom; padding: 0 10px; }
        .bar { width: 40px; background: #4CAF50; border-radius: 4px 4px 0 0; transition: height 0.3s; }
        .bar:hover { background: #45a049; }
        .label { margin-top: 4px; font-size: 12px; color: #333; }
        .value { font-weight: bold; color: #2196F3; }
    </style>
</head>
<body>
    <h2>最近 30 天審核漏斗 (<?= $startStr ?> 至 <?= $endStr ?>)</h2>
    <table class="chart-table">
        <tr>
            <?php foreach ($bars as [$label, $value]): 
                $h = $total ? floor(($value / $total) * $maxHeight) : 0;
            ?>
            <td>
                <div class="value"><?= number_format($value) ?></div>
                <div class="bar" style="height: <?= $h ?>px"></div>
                <div class="label"><?= htmlspecialchars($label) ?></div>
            </td>
            <?php endforeach; ?>
        </tr>
    </table>
</body>
</html>

進階:CSS 漸變與動畫

<style>
.bar {
    width: 40px;
    background: linear-gradient(to top, #1e88e5, #64b5f6);
    border-radius: 4px 4px 0 0;
    animation: grow 0.8s ease-out forwards;
    transform-origin: bottom;
    transform: scaleY(0);
}
@keyframes grow { to { transform: scaleY(1); } }
</style>

// PHP 輸出時添加 style="--h: <?= $h ?>px" 並用 CSS 變量控制
// 或直接內聯 height,配合 transition 也有動畫效果

響應式適配

<style>
@media (max-width: 480px) {
    .bar { width: 28px; }
    .chart-table td { padding: 0 4px; }
}
</style>

數據庫查詢優化

原代碼執行 4 次查詢,可合併為單次聚合:

$sql = "SELECT 
    COUNT(*) AS total,
    SUM(is_pa_check_first = 1) AS stage1,
    SUM(is_pa_check_first = 1 AND is_pa_check_second = 1) AS stage2,
    SUM(is_pa_check_first = 1 AND is_pa_check_second = 1 AND is_pa_check_third = 1) AS stage3
FROM newpro WHERE p_date >= ? AND p_date < ?";
$row = $pdo->query($sql, [$startStr, $endStr])->fetch(PDO::FETCH_ASSOC);

總結

純 PHP + HTML/CSS 柱狀圖零依賴、極輕量、SEO 友好、可直接嵌入郵件/PDF。核心是「後端計算百分比 → 前端 CSS 高度映射」。複雜圖表(折線、餅圖、交互)仍建議用 Canvas/SVG 庫,但簡單漏斗、對比柱狀圖此法最划算。

copyright © 萬盛學電腦網 all rights reserved