一、开启GD库
修改php.ini 配置文件,去掉extension前的;
;extension=gd2.dll
改为
extension=gd2.dll
二、安装JpGraph
composer运行命令
composer require amenadiel/jpgraph
三、使用JpGraph生成统计图
官方文档
https://jpgraph.net/
按需引用
use Amenadiel\JpGraph\Graph\Graph;
use Amenadiel\JpGraph\Graph\PieGraph;
use Amenadiel\JpGraph\Plot\BarPlot;
生成柱状图、饼图、折线图
public function chart()
{
$xdata = array(19,23,34,38,45,67,71,78,85,87,96,145);
$ydata = array("一","二","三","四","五","六","七","八","九","十","十一","十二");
//柱状图
$graph = new Graph(500,300); //创建新的Graph对象
$graph->SetScale("textlin"); //刻度样式
$graph->SetShadow(); //设置阴影
$graph->img->SetMargin(80,30,40,70); //设置边距
$graph->img->SetTextAlign('right','top');
$graph->graph_theme = null; //设置主题为null,否则value->Show(); 无效
$barplot = new BarPlot($ydata); //创建BarPlot对象
$barplot->value->Show(); //设置显示数字
$barplot->value->SetFormat('%d');
$barplot->value->SetColor('#666666');
$barplot->SetFillColor('#FF6778'); //设置颜色
$barplot->SetColor('#FF6778');
$graph->Add($barplot); //将柱形图添加到图像中
//设置标题和X-Y轴标题
$graph->title->Set(iconv("UTF-8","gb2312",$title));
$graph->xaxis->title->Set(iconv("UTF-8","gb2312",'x轴'));
$graph->yaxis->title->Set(iconv("UTF-8","gb2312",'y轴'));
$graph->title->SetColor("red");
$graph->title->SetMargin(10);
$graph->xaxis->title->SetMargin(5);
$xdata = $this->array2gbk($xdata);
$graph->xaxis->SetTickLabels($xdata);
$graph->xaxis->SetLabelAngle(15);
$graph->title->SetFont(FF_SIMSUN); //设置字体
$graph->yaxis->title->SetFont(FF_SIMSUN);
$graph->xaxis->title->SetFont(FF_SIMSUN);
$graph->xaxis->SetFont(FF_SIMSUN);
$graph->Stroke('./img/'.$title.'.png');
//饼图
$graph = new PieGraph(500,300);
$graph->SetShadow();
$graph->title->Set(iconv("UTF-8","gb2312",$title));
$graph->title->SetFont(FF_SIMSUN);
$pieplot = new PiePlot3D($ydata); //创建PiePlot3D对象
$pieplot->SetCenter(0.5, 0.4); //设置饼图中心的位置
$xdata = $this->array2gbk($xdata);
$pieplot->SetLegends($xdata); //设置图例
$graph->legend->SetFont(FF_SIMSUN);
$pieplot->SetSliceColors(array('#FF6778','#FFC91D','#22ACFD'));
$graph->Add($pieplot);
$graph->Stroke($url.$title.'.png');
//折线图
$graph = new Graph(500,300); //创建新的Graph对象
$graph->SetScale("textlin");
$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);
// $graph->title->Set(iconv("UTF-8","gb2312",$title));
$graph->SetBox(false);
$graph->SetMargin(40,20,36,63);
$graph->img->SetAntiAliasing();
$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$week_list_zn = $this->array2gbk(array('周一','周二','周三','周四','周五','周六','周日'));
$graph->xaxis->SetTickLabels($week_list_zn);
$graph->xgrid->SetColor('#E3E3E3');
// 第一条线
$p1 = new LinePlot(array(20,15,23,15));
$graph->Add($p1);
$p1->SetColor("#22ACFD");
$p1->SetLegend(iconv("UTF-8","gb2312",'图例1'));
// 第二条线
$p2 = new LinePlot(array(20,15,23,15));
$graph->Add($p2);
$p2->SetColor("#FFA785");
$p2->SetLegend(iconv("UTF-8","gb2312",'图例2'));
$graph->title->SetFont(FF_SIMSUN); //设置字体
$graph->legend->SetFont(FF_SIMSUN);
$graph->xaxis->title->SetFont(FF_SIMSUN);
$graph->yaxis->title->SetFont(FF_SIMSUN);
$graph->xaxis->SetFont(FF_SIMSUN);
$graph->legend->SetFrameWeight(1);
$graph->Stroke($url.$title.'.png');
}
四、解决中文乱码问题
看网上好多方案的解决办法都是修改源码,这里不对源码进行修改来解决这个问题
乱码的原因是JpGraph中默认是要把字符串转成utf8但是如果你文件自身就是utf8并且要用中文字体,还会转一遍,结果多转了一次,就会出现乱码
用iconv将字符串转为gb2312格式
//设置标题
$graph->title->Set(iconv("UTF-8","gb2312",$title));
//设置字体
$graph->title->SetFont(FF_SIMSUN);
//x轴 数组用array2gbk转换
$xdata = $this->array2gbk($xdata);
$graph->xaxis->SetTickLabels($xdata);
数组转换UTF-8编码为GB2312
//数组转换UTF-8编码为GB2312
protected function array2gbk($array){
$array = array_map(function($value){
if(is_array($value)){
return $this->array2gbk($value);
} else{
return mb_convert_encoding($value, "GB2312", "UTF-8");
}
}
, $array);
return $array;
}