Step1: 环境
Windows10 + php5.3(下载) + jpgraph-3.5.0b1(下载)
Step2: 开启GD库
打开路径“S:\Xampp\php\php.ini”,打开php.ini文件,找到“;extension=php_gd2.dll”选项,将其前的注释符号“;”去掉。
extension=php_gd2.dll
Step3: 关键点!!!
在htdocs目录下新建一个名为test的文件夹;然后,解压下载的jpgraph-3.5.0b1压缩包,将解压后的“src”文件夹和"docs"文件夹复制到htdocs/test文件夹中;然后将"src"重命名为"jpgraph"。
Step4: 测试
将jpgraph\Examples\accbarex1.php拷贝到test目录下,然后打开浏览器,输入http://localhost/test/accbarex1.php,Yeah, done it!!
拓展1: 同样地,新建文件“S:\Xampp\htdocs\test\test2.php”
<?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("Month"); //设置坐标轴名称
$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(); //输出图像
?>
拓展2: 同样地,新建文件“S:\Xampp\htdocs\test\test3.php”
<?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("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
$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("Month"); //设置标题和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();
?>
拓展3: 同样地,新建文件“S:\Xampp\htdocs\test\test4.php”
<?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 ratio");
$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();
?>
参考