PHP使用JpGraph生成柱形图超详细解析
总体思路
我们将通过6个关键步骤,使用JpGraph库创建一个完整的柱形图。整个过程包括:引入库文件→准备数据→创建图表对象→设置图表样式→绘制柱形→输出结果。每个步骤我都会严格对应您提供的代码进行逐行解析。
1. 引入库文件
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_bar.php');作用:加载必要的图形库文件
详细解析:
jpgraph.php:核心图形库,包含基础绘图功能jpgraph_bar.php:柱形图专用库,提供柱形图相关类和方法
注意事项:
必须按顺序引入,先核心库后专用库
路径需与实际安装位置一致
文件扩展名
.php不可省略
2. 准备数据
$months = ['1月','2月','3月','4月','5月','6月', '7月','8月','9月','10月','11月','12月']; $salesData = [120,150,180,210,250,300,320,310,280,260,230,200];
作用:定义图表所需的数据源
详细解析:
$months:字符串数组,定义X轴标签(12个月份)元素数量决定X轴刻度数量
中文需确保编码正确
$salesData:数值数组,定义Y轴数据(每月销量)必须与
$months元素数量一致数值决定柱形高度
数据结构关系:
3. 创建图表对象
$graph = new Graph(800,400);
$graph->SetScale('textlin');
$graph->SetMargin(50,30,40,50);3.1 创建画布
$graph = new Graph(800,400);
参数详解:
3.2 设置坐标轴类型
$graph->SetScale('textlin');参数详解:
'textlin':复合参数,由两部分组成text:X轴使用文本标签lin:Y轴使用线性刻度其他可选值:
'intint':整数坐标'linlin':双线性坐标
3.3 设置边距
$graph->SetMargin(50,30,40,50);
参数详解:
4. 设置标题
$graph->title->Set('2025年计算机图书销售报告');
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
$graph->xaxis->title->Set('月份');
$graph->yaxis->title->Set('销量(本)');4.1 主标题设置
$graph->title->Set('2025年计算机图书销售报告');方法解析:
Set():设置标题文本内容参数:任意字符串,支持中文
4.2 标题字体设置
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
参数详解:
4.3 坐标轴标题
$graph->xaxis->title->Set('月份');
$graph->yaxis->title->Set('销量(本)');对象结构:
xaxis:X轴对象yaxis:Y轴对象title:标题子对象Set():设置标题文本
5. 创建柱形图
$barplot = new BarPlot($salesData);
$barplot->SetFillGradient('navy','lightblue',GRAD_VER);
$barplot->SetWidth(0.6);
$barplot->value->Show();
$barplot->value->SetFormat('%d本');5.1 创建柱形图对象
$barplot = new BarPlot($salesData);
关键解析:
BarPlot:柱形图类$salesData:必须传入数值数组构造函数将自动计算柱形高度
5.2 设置渐变填充
$barplot->SetFillGradient('navy','lightblue',GRAD_VER);参数详解:
5.3 设置柱形宽度
$barplot->SetWidth(0.6);
参数详解:
0.6:柱形宽度比例(0-1)
0.9:柱形几乎相连
0.3:柱形很细
5.4 显示数值标签
$barplot->value->Show();
$barplot->value->SetFormat('%d本');方法解析:
Show():显示柱形顶部的数值SetFormat():设置数值格式%d:整数%.1f:保留1位小数'本':追加单位
6. 组合输出
$graph->Add($barplot);
$graph->xaxis->SetTickLabels($months);
$graph->Stroke('book_sales.png');6.1 添加柱形到画布
$graph->Add($barplot);
方法解析:
将柱形图对象添加到主画布
一个画布可添加多个图形对象
6.2 设置X轴标签
$graph->xaxis->SetTickLabels($months);
参数要求:
必须与数据数组长度一致
支持HTML特殊字符转义
6.3 输出图像
$graph->Stroke('book_sales.png');输出方式:
无参数:直接输出到浏览器
带文件名:保存为PNG文件
支持格式:PNG/JPG/GIF
函数封装建议
/**
* 生成柱形图并保存
* @param array $data Y轴数据
* @param array $labels X轴标签
* @param string $title 图表标题
* @param string $filename 输出文件名
* @param int $width 图像宽度
* @param int $height 图像高度
* @return bool 是否成功
*/
function generateBarChart(array $data, array $labels, string $title,
string $filename, int $width = 800, int $height = 400) {
require_once 'jpgraph/src/jpgraph.php';
require_once 'jpgraph/src/jpgraph_bar.php';
try {
$graph = new Graph($width, $height);
// ...中间代码与示例相同...
$graph->Stroke($filename);
return file_exists($filename);
} catch (Exception $e) {
error_log('图表生成错误: '.$e->getMessage());
return false;
}
}使用示例:
generateBarChart( [120,150,180], ['Q1','Q2','Q3'], '季度销售报告', 'sales_report.png' );