php实现图表统计
PHP 实现图表统计的方法
使用 GD 库生成基础图表
PHP 的 GD 库可以创建简单的图表。以下是一个生成柱状图的示例代码:
<?php
$width = 400;
$height = 300;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$red = imagecolorallocate($image, 255, 0, 0);
imagefill($image, 0, 0, $white);
$data = [30, 50, 70, 90, 110];
$barWidth = 50;
$maxValue = max($data);
for ($i = 0; $i < count($data); $i++) {
$barHeight = ($data[$i] / $maxValue) * ($height - 50);
imagefilledrectangle($image, $i * $barWidth + 30, $height - $barHeight - 20,
($i + 1) * $barWidth, $height - 20, $red);
}
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
使用第三方库(如 JpGraph)
JpGraph 是一个专业的 PHP 图表库,支持多种图表类型:

- 下载并安装 JpGraph
- 创建折线图示例:
<?php
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
$ydata = [12, 15, 22, 19, 26];
$graph = new Graph(350, 250);
$graph->SetScale("textlin");
$graph->title->Set('Monthly Sales');
$line = new LinePlot($ydata);
$graph->Add($line);
$graph->Stroke();
?>
集成 JavaScript 图表库
通过 PHP 输出数据给前端 JavaScript 图表库(如 Chart.js):
<?php
$data = [
'labels' => ['January', 'February', 'March', 'April', 'May'],
'datasets' => [[
'label' => 'Sales',
'data' => [65, 59, 80, 81, 56],
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
'borderColor' => 'rgba(75, 192, 192, 1)'
]]
];
?>
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: <?php echo json_encode($data); ?>,
options: { responsive: true }
});
</script>
使用 Google Charts API
PHP 准备数据,通过 Google Charts 显示:

<?php
$chartData = [
['Month', 'Sales'],
['Jan', 1000],
['Feb', 1170],
['Mar', 660]
];
$jsonData = json_encode($chartData);
?>
<div id="piechart" style="width: 500px; height: 300px;"></div>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(<?php echo $jsonData; ?>);
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data);
}
</script>
数据库数据可视化
从 MySQL 数据库获取数据生成图表:
<?php
$conn = new mysqli("localhost", "user", "password", "database");
$result = $conn->query("SELECT product, sales FROM sales_data");
$chartData = [];
while ($row = $result->fetch_assoc()) {
$chartData[] = [$row['product'], (int)$row['sales']];
}
array_unshift($chartData, ['Product', 'Sales']);
?>
使用 SVG 创建矢量图表
PHP 生成 SVG 格式的图表:
<?php
header('Content-Type: image/svg+xml');
$data = [30, 70, 50, 90];
$width = 300;
$height = 200;
$max = max($data);
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<svg width="'.$width.'" height="'.$height.'" viewBox="0 0 '.$width.' '.$height.'">';
echo '<rect width="100%" height="100%" fill="#f8f9fa"/>';
foreach ($data as $i => $value) {
$barHeight = ($value / $max) * ($height - 20);
$x = $i * 60 + 20;
$y = $height - $barHeight;
echo '<rect x="'.$x.'" y="'.$y.'" width="40" height="'.$barHeight.'" fill="#007bff"/>';
}
echo '</svg>';
?>
选择合适的方法取决于项目需求、图表复杂度和性能要求。简单的静态图表可使用 GD 库或 SVG,复杂交互式图表推荐结合前端库实现。






