PHP jpgraph库的配置及生成多种统计图表
JpGraph简介
JpGraph是开源的PHP统计图表生成库,基于PHP的GD2图形库构建,把生成统计图的相关操纵封装,潜藏了部分复杂的操纵,使在PHP页面上输出统计图表变得愈加容易。JpGraph的官方网站为:http://jpgraph.net,开发者可以在上面免费下载最新版的JpGraph和阅读相关帮忙文档或示例程序。
相关学习引荐:PHP编程从入门到熟知
JpGraph的配置
(1)修改文件php.ini
在include_path中增加jpgraph的名目途径,并将jpgraph解压后的src名目名称更换为jpgraph。
(2)检查PHP可否支撑GD库
在php.ini文件中寻到语句;extension=php_gd2.dll。把上述语句前的;号去除,即去除注释。假如由于PHP版本不一样而寻不到此语句,则可直接增加extension=php_gd2.dll
(3)修改文件jpgraph_gb2312.php
寻到函数:function gb2utf8($gb)
把函数修改为:
function gb2utf8($gb) { return $gb; }
即不使用gb2编码方式转utf8方式的那段代码。
折线图
<?php require_once ("jpgraph/jpgraph.php"); require_once ("jpgraph/jpgraph_line.php"); $data1 = array(523,634,371,278,685,587,490,256,398,545,367,577); //第一条曲线的数组 $graph = new Graph(500,300); $graph->SetScale("textlin"); $graph->SetShadow(); $graph->img->SetMargin(60,30,30,70); //设定图像边距 $graph->graph_theme = null; //设定主题为null,不然value->Show(); 无效 $lineplot1=new LinePlot($data1); //创立设定两条曲线对象 $lineplot1->value->SetColor("red"); $lineplot1->value->Show(); $graph->Add($lineplot1); //将曲线放置到图像上 $graph->title->Set("CDN流量图"); //设定图像标题 $graph->xaxis->title->Set("月份"); //设定坐标轴名称 $graph->yaxis->title->Set("流 量(Gbits)"); $graph->title->SetMargin(10); $graph->xaxis->title->SetMargin(10); $graph->yaxis->title->SetMargin(10); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设定字体 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth()); $graph->Stroke(); //输出图像 ?>
柱状图
<?php require_once ("jpgraph/jpgraph.php"); require_once ("jpgraph/jpgraph_bar.php"); $data = 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(40,30,40,50); //设定边距 $graph->graph_theme = null; //设定主题为null,不然value->Show(); 无效 $barplot = new BarPlot($data); //创立BarPlot对象 $barplot->SetFillColor('blue'); //设定色彩 $barplot->value->Show(); //设定显示数字 $graph->Add($barplot); //将柱形图增加到图像中 $graph->title->Set("CDN流量图"); $graph->xaxis->title->Set("月份"); //设定标题和X-Y轴标题 $graph->yaxis->title->Set("流 量(Mbits)"); $graph->title->SetColor("red"); $graph->title->SetMargin(10); $graph->xaxis->title->SetMargin(5); $graph->xaxis->SetTickLabels($ydata); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设定字体 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD); $graph->Stroke(); ?>
饼状图
<?php require_once ("jpgraph/jpgraph.php"); require_once ("jpgraph/jpgraph_pie.php"); require_once ("jpgraph/jpgraph_pie3d.php"); $data = array(19,23,34,38,45,67,71,78,85,87,90,96); $graph = new PieGraph(550,500); $graph->SetShadow(); $graph->title->Set("CDN流量比例"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot3D($data); //创立PiePlot3D对象 $pieplot->SetCenter(0.4, 0.5); //设定饼图中心的位置 $pieplot->SetLegends($gDateLocale->GetShortMonth()); //设定图例 $graph->Add($pieplot); $graph->Stroke(); ?>
以上就是PHP jpgraph库的配置及生成多种统计图表的具体内容,更多请关注百分百源码网其它相关文章!