您好!欢迎访问家园网-www.jy.wang!

家园网

柱形图测试正确代码

网络 作者:本站 点击:
<?php
// 1. 引入库
require_once ('src/jpgraph.php');  //必须要引用的文件
require_once ('src/jpgraph_bar.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];


// 3. 创建图表
$graph = new Graph(800,400);  //设置画布大小
$graph->SetScale('textlin');  //设置刻度类型,X轴刻度可作为文本标注的直线刻度,Y轴为直线刻度
$graph->img->SetMargin(50, 30, 40, 50); // 左、右、上、下边距


// 4. 设置标题
$graph->title->SetFont(FF_CHINESE);  // 设置图表标题字体
$graph->title->Set('2025年计算机图书销售报告');  //设置标题

$graph->xaxis->SetFont(FF_SIMSUN, FS_NORMAL);  // 设置X轴刻度标签字体
$graph->xaxis->SetTickLabels($months);  //设置X轴刻度标签

$graph->xaxis->title->SetFont(FF_SIMSUN, FS_NORMAL);  //设置X轴标题字体
$graph->xaxis->title->Set('月份');  //设置X轴标题

$graph->yaxis->title->SetFont(FF_SIMSUN, FS_BOLD);  //设置Y轴标题字体
$graph->yaxis->title->SetAngle(90);  //设置Y轴标题字体倾斜90度,逆时针
$graph->yaxis->SetLabelMargin(1);   // 用于调整Y轴刻度标签与轴线之间的间距(单位为像素)
$graph->yaxis->title->Set('销量(本)');  //设置Y轴标题


// 5. 创建柱形
$barplot = new BarPlot($salesData);  //创建柱状坐标类,将y轴数据注入
$barplot->SetColor("white"); //设置柱状边框颜色
$barplot->SetFillGradient("#4B0082","white",GRAD_LEFT_REFLECTION);//设置柱体颜色
$barplot->SetWidth(35);  //设置柱状间距

$graph->Add($barplot);  // 柱状坐标类注入图表

$barplot->value->Show(); //柱形图上显示数据
$barplot->value->SetFormatCallback(function($value) {
    return (int)$value; // 强制柱形图上显示的数据转换为整数
});


// 6. 显示图表
$graph->Stroke();  //显示图表
?>


360截图20250820212815126.jpg

标签: