PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

时间:2022-11-20 08:36:38

jpgraph简介

jpgraph是开源的php统计图表生成库,基于php的gd2图形库构建,把生成统计图的相关操作封装,隐藏了部分复杂的操作,使在php页面上输出统计图表变得更加容易。jpgraph的官方网站为:http://jpgraph.net,开发者可以在上面免费下载最新版的jpgraph和阅读相关帮助文档或示例程序。

PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

jpgraph的配置

(1)修改文件php.ini

在include_path中添加jpgraph的目录路径,并将jpgraph解压后的src目录名称更改为jpgraph。

PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

(2)检查php是否支持gd库

在php.ini文件中找到语句;extension=php_gd2.dll。把上述语句前的;号去掉,即去掉注释。如果因为php版本不同而找不到此语句,则可直接添加extension=php_gd2.dll

(3)修改文件jpgraph_gb2312.php

找到函数:function gb2utf8($gb)

把函数修改为:

?
1
2
3
function gb2utf8($gb) {
return $gb;
}

即不使用gb2编码方式转utf8方式的那段代码。

PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

折线图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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 jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

柱状图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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 jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图

饼状图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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库的配置及生成统计图表:折线图、柱状图、饼状图

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!

原文链接:http://blog.csdn.net/aoshilang2249/article/details/46956163