php -m | grep gd 或 phpinfo() 查看 GD 區塊/var/www/html/jpgraph/jpgraph.php(第 35、38 行附近):
// 快取目錄(需可寫入)
define('CACHE_DIR', '/tmp/jpgraph_cache/'); // Linux
// define('CACHE_DIR', 'C:/tmp/jpgraph_cache/'); // Windows
// 字型目錄
define('TTF_DIR', '/usr/share/fonts/truetype/'); // Linux
// define('TTF_DIR', 'C:/Windows/Fonts/'); // Windows
mkdir -p /tmp/jpgraph_cache && chmod 777 /tmp/jpgraph_cachesrc/Examples/ 下範例複製至 Web 根目錄,瀏覽驗證。<?php
require_once 'jpgraph/src/jpgraph.php';
require_once 'jpgraph/src/jpgraph_bar.php';
// 1. 連線資料庫
$pdo = new PDO('mysql:host=localhost;dbname=jpg;charset=utf8', 'root', '');
$stmt = $pdo->query('SELECT number FROM test ORDER BY id');
$values = $stmt->fetchAll(PDO::FETCH_COLUMN);
// 2. 建立圖表
$graph = new Graph(400, 200, 'auto');
$graph->SetScale('textlin');
$graph->img->SetMargin(40, 130, 20, 40);
$graph->SetShadow();
// 3. 長條圖資料
$bplot = new BarPlot($values);
$bplot->SetFillColor('orange');
$bplot->SetLegend('Result');
// 4. 組裝
$graph->Add($bplot);
$graph->title->Set('資料庫驅動長條圖');
$graph->xaxis->title->Set('項目');
$graph->yaxis->title->Set('數值');
$graph->title->SetFont(FF_FONT1, FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD);
// 5. 輸出
$graph->Stroke();
?>
| 圖表 | Include | Plot Class |
|---|---|---|
| 長條圖 | jpgraph_bar.php | BarPlot |
| 折線圖 | jpgraph_line.php | LinePlot |
| 圓餅圖 | jpgraph_pie.php | PiePlot / PiePlot3D |
| 面積圖 | jpgraph_line.php | AreaPlot |
| 散佈圖 | jpgraph_scatter.php | ScatterPlot |
| 雷達圖 | jpgraph_radar.php | RadarPlot |
| Gantt 圖 | jpgraph_gantt.php | GanttChart |
$graph->SetCacheDir(CACHE_DIR); 避免重複運算。img->SetAntiAliasing(false) 關閉抗鋸齒。JpGraph 免費版授權為 QPL 1.0,商業專案需購買專業授權。現代替代方案: