此文为转载,仅作保存文档使用,转自:http://blog.csdn.net/aoshilang2249/article/details/46956163
注意:原文中提到配置php.ini中的include_path,亲测不需要配置(gd2需要开启),只需要将下载的文件引入项目即可
JpGraph简介
JpGraph是开源的PHP统计图表生成库,基于PHP的GD2图形库构建,把生成统计图的相关操作封装,隐藏了部分复杂的操作,使在PHP页面上输出统计图表变得更加容易。JpGraph的官方网站为:http://jpgraph.net,开发者可以在上面免费下载最新版的JpGraph和阅读相关帮助文档或示例程序。
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;
-
- $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->SetScale("textlin");
- $graph->SetShadow();
- $graph->img->SetMargin(40,30,40,50);
-
- $graph->graph_theme = null;
-
- $barplot = new BarPlot($data);
- $barplot->SetFillColor('blue');
- $barplot->value->Show();
- $graph->Add($barplot);
-
- $graph->title->Set("CDN流量图");
- $graph->xaxis->title->Set("月份");
- $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);
- $pieplot->SetCenter(0.4, 0.5);
- $pieplot->SetLegends($gDateLocale->GetShortMonth());
-
- $graph->Add($pieplot);
- $graph->Stroke();
- ?>