您的位置:首页 >PHP读取文件生成统计图表方法详解
发布于2026-03-15 阅读(0)
扫一扫,手机访问
PHP生成可视化图表有五种方法:一、解析CSV输出JSON供前端调用;二、读取JSON嵌入HTML配合ECharts;三、拼接SVG代码生成静态矢量图;四、用GD库绘制PNG位图;五、解析INI生成HTML指标卡片。

如果您拥有一个包含结构化数据的文本文件(如CSV或JSON格式),并希望使用PHP读取其中内容后生成可视化图表,则需要借助PHP的数据解析能力与前端图表库协同工作。以下是实现此目标的具体步骤:
该方法适用于以逗号分隔的表格型数据,PHP通过fgetcsv函数逐行读取,再将结果组织为数组并编码为JSON,供前端图表库调用。
1、在Web服务器根目录下创建data.csv文件,内容示例如下:
月份,销售额,用户数
1月,12000,85
2月,13500,92
3月,11800,78
2、新建chart_data.php文件,写入以下代码:
<?php
$file = 'data.csv';
$data = [];
if (($handle = fopen($file, 'r')) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) {
if (count($row) >= 3 && $row[0] !== '月份') {
$data[] = ['month' => $row[0], 'sales' => (int)$row[1], 'users' => (int)$row[2]];
}
}
fclose($handle);
}
header('Content-Type: application/json');
echo json_encode($data);
?>
3、确保chart_data.php可被AJAX请求访问,且返回格式为标准JSON数组。
该方法适用于已预先整理好的JSON格式数据文件,PHP通过file_get_contents和json_decode解析内容,并在HTML中以内联脚本方式传递给前端图表库。
1、创建data.json文件,内容示例如下:
[{"month":"1月","sales":12000,"users":85},{"month":"2月","sales":13500,"users":92}]
2、新建chart_page.php,写入以下代码:
<?php
$json_content = file_get_contents('data.json');
$chart_data = json_decode($json_content, true);
?>
<!DOCTYPE html>
<html>
<head><title>统计图表</title></head>
<body>
<div id="chart" style="width:600px;height:400px;"></div>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
<script>
const data = <?php echo json_encode($chart_data); ?>;
const chartDom = document.getElementById('chart');
const myChart = echarts.init(chartDom);
myChart.setOption({
tooltip: {},
xAxis: { data: data.map(d => d.month) },
yAxis: {},
series: [{
name: '销售额',
type: 'bar',
data: data.map(d => d.sales)
}]
});
</script>
</body></html>
3、确保data.json与chart_page.php位于同一目录,且Web服务器支持PHP执行。
该方法不依赖JavaScript图表库,而是利用PHP字符串拼接生成静态SVG代码,适合轻量级、无交互需求的图表展示。
1、创建svg_chart.php文件,写入以下代码:
<?php
$data = [[12000, 85], [13500, 92], [11800, 78]]; // 销售额、用户数
$width = 600;
$height = 400;
$margin = 40;
$barWidth = ($width - 2 * $margin) / count($data);
$maxValue = max(array_column($data, 0));
$scale = ($height - 2 * $margin) / $maxValue;
echo '<svg width="' . $width . '" height="' . $height . '" xmlns="http://www.w3.org/2000/svg">';
echo '<rect width="100%" height="100%" fill="white"/>';
for ($i = 0; $i < count($data); $i++) {
$x = $margin + $i * $barWidth;
$y = $height - $margin - $data[$i][0] * $scale;
$barHeight = $data[$i][0] * $scale;
echo '<rect x="' . $x . '" y="' . $y . '" width="' . ($barWidth * 0.7) . '" height="' . $barHeight . '" fill="steelblue"/>';
echo '<text x="' . ($x + $barWidth * 0.35) . '" y="' . ($height - $margin + 20) . '" font-size="12" text-anchor="middle">' . ($i+1) . '月</text>';
}
echo '</svg>';
?>
2、访问svg_chart.php即可直接渲染出柱状图SVG图像。
该方法利用PHP内置GD扩展,在服务端生成位图格式图表,适用于需导出图片或规避前端JS依赖的场景。
1、确认PHP已启用GD扩展:运行phpinfo()检查gd项是否启用。
2、创建png_chart.php文件,写入以下代码:
<?php
$data = [12000, 13500, 11800];
$width = 600;
$height = 400;
$img = imagecreatetruecolor($width, $height);
$bg = imagecolorallocate($img, 255, 255, 255);
$bar_color = imagecolorallocate($img, 70, 130, 180);
$text_color = imagecolorallocate($img, 0, 0, 0);
imagefilledrectangle($img, 0, 0, $width, $height, $bg);
$max = max($data);
$bar_width = 60;
$gap = 40;
$x_start = 80;
for ($i = 0; $i < count($data); $i++) {
$bar_height = ($data[$i] / $max) * ($height - 100);
$x = $x_start + $i * ($bar_width + $gap);
$y = $height - 50 - $bar_height;
imagefilledrectangle($img, $x, $y, $x + $bar_width, $height - 50, $bar_color);
imagestring($img, 5, $x + 10, $height - 30, ($i+1).'月', $text_color);
imagestring($img, 4, $x + 5, $y - 15, $data[$i], $text_color);
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
3、访问png_chart.php将直接输出PNG图像流,浏览器自动渲染。
该方法适用于非数值密集型统计,如系统状态、配置摘要等,PHP读取INI格式后生成带样式的HTML指标块。
1、创建config.ini文件,内容如下:
[system]
uptime = 142
memory_usage = 68
disk_usage = 42
2、创建dashboard.php文件,写入以下代码:
<?php
$ini = parse_ini_file('config.ini', true);
$sys = $ini['system'];
?>
<div style="display:flex;gap:16px;padding:16px;">
<div style="background:#e3f2fd;padding:12px;border-radius:4px;">
<div style="font-size:12px;color:#666;">运行时间(小时)</div>
<div style="font-size:24px;font-weight:bold;color:#1976d2;"><?php echo $sys['uptime']; ?></div>
</div>
<div style="background:#e8f5e9;padding:12px;border-radius:4px;">
<div style="font-size:12px;color:#666;">内存占用(%)</div>
<div style="font-size:24px;font-weight:bold;color:#388e3c;"><?php echo $sys['memory_usage']; ?></div>
</div>
<div style="background:#fff3cd;padding:12px;border-radius:4px;">
<div style="font-size:12px;color:#666;">磁盘占用(%)</div>
<div style="font-size:24px;font-weight:bold;color:#f57c00;"><?php echo $sys['disk_usage']; ?></div>
</div>
</div>
3、确保config.ini与dashboard.php同目录,且PHP有读取权限。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9