在 GD 库普及前,開發者常利用 HTML 表格模擬圖表效果。本文示範如何僅用 <table> 與 PHP 迴圈,輕鬆產出直觀的長條圖,適合簡單統計展示場景。
<table> 排列各數據欄位。<table> 的 height 屬性對應數值大小。bgcolor 設定長條顏色,valign="bottom" 使長條底部對齊。<?php
$b = array(150, 110, 125, 180, 160, 175, 230, 220);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
td { font-size: 9pt; }
</style>
</head>
<body>
<table border="0">
<tr valign="bottom">
<?php for ($i = 0; $i < 8; $i++) { ?>
<td align="center">
<table height="<?php echo $b[$i]; ?>" border="0">
<tr>
<td bgcolor="#3F7F9F" width="40"></td>
</tr>
</table><br>
<font color="#3F7F9F"><?php echo $b[$i]; ?></font>
</td>
<?php } ?>
</tr>
</table>
</body>
</html>
| 優點 | 缺點 |
|---|---|
| 無需 GD 擴展,相容性極佳 | 無法繪製複雜圖形(曲線、餅圖等) |
| 代碼簡單,易於理解與修改 | 大量數據時 HTML 體積龐大,渲染較慢 |
| 純前端呈現,伺服器負擔小 | 缺乏互動功能(Tooltip、點擊事件等) |
若需專業級圖表,建議改用 GD 库、Chart.js 或 Highcharts 等現代方案。