您的位置:首页 > 编程语言 > PHP开发

PHP扩展类JpGraph绘制统计图像 (一) 图表绘制

2017-05-20 13:30 621 查看
转载 http://www.imooc.com/learn/417

一、绘制线性图表



<?php

//引入类库

require_once "/data/webroot/resource/php/src/jpgraph.php";

require_once "/data/webroot/resource/php/src/jpgraph_line.php";

$data=array(0=>-21,1=>-3,2=>12,3=>19,4=>23,5=>29,6=>30,7=>22,8=>26,9=>18,10=>5,11=>-10);//第一条数据

$data2y=array(0=>3,1=>12,2=>18,3=>30,4=>28,5=>33,6=>43,7=>39,8=>36,9=>29,10=>15,11=>10);//第二条数据

//得到Graph对象

$graph=new Graph(400,400);

//设置X和Y轴样式及Y轴的最大值最小值

$graph->SetScale("textint",-30,50);

//设置右侧Y轴样式及其最大值最小值

$graph->SetY2Scale("int",-30,50);

//设置图像样式,加入阴影

$graph->SetShadow();

//设置图像边界范围

$graph->img->setMargin(40,30,50,70);

//设置标题

$graph->title->Set("this is a test X-Y-Y");

//得到曲线实例

$linePlot=new LinePlot($data);

//得到第二条曲线

$linePlot2y=new LinePlot($data2y);

//将曲线加入到图像中

$graph->Add($linePlot);

$graph->Add($linePlot2y);

//设置三个坐标轴名称

$graph->xaxis->title->Set("Month");

$graph->yaxis->title->Set("beijing");

$graph->y2axis->title->Set("ShangHai");

//设置两条曲线的颜色

//设置两条曲线的图例

$linePlot->SetLegend("Beijing");

$linePlot2y->SetLegend("Shanghai");

//设置图例样式

$graph->legend->setlayout(LEGEND_HOR);

$graph->legend->Pos(0.45,0.9,"center","bottom");

//将图像输出到浏览器

$graph->Stroke();

二、绘制柱状图表



<?php

//引入类库

require_once "/data/webroot/resource/php/src/jpgraph.php";

require_once "/data/webroot/resource/php/src/jpgraph_bar.php";

//柱形图模拟数据

$data=array(0=>-21,1=>-3,2=>12,3=>19,4=>23,5=>29,6=>30,7=>22,8=>26,9=>18,10=>5,11=>-10);

//创建背景图

$graph=new Graph(400,300);

//设置刻度样式

$graph->SetScale("textlin");

//设置边界范围

$graph->img->SetMargin(30,30,80,30);

//设置标题

$graph->title->Set("BarPlot test");

//得到柱形图对象

$barPlot=new BarPlot($data);

//设置柱形图图例

$barPlot->SetLegend("beijing");

//显示柱形图代表数据的值

$barPlot->value->show();

//将柱形图加入到背景图

$graph->Add($barPlot);

//设置柱形图填充颜色

//设置边框颜色

//将柱形图输出到浏览器

$graph->Stroke();

三、绘制饼图



<?php

require_once "/data/webroot/resource/php/src/jpgraph.php";

require_once "/data/webroot/resource/php/src/jpgraph_pie.php";

//模拟数据

$data=array(0=>3.5,1=>4.6,2=>9.1,3=>21.9,4=>42.3,5=>90.7,6=>183.5,7=>127.5,8=>61.4,9=>33.5,10=>11.5,11=>4.4);

//创建画布

$graph=new PieGraph(800,500);

//设置图像边界范围

$graph->img->SetMargin(30,30,80,30);

//设置标题

$graph->title->Set("PiePlot Test");

//得到饼图对象

$piePlot=new PiePlot($data);

//设置图例

//设置图例位置

$graph->legend->Pos(0.01,0.45,"left","top");

//添加到画布中

$graph->Add($piePlot);

//输出

$graph->Stroke();

四、绘制3D饼图



<?php

require_once "/data/webroot/resource/php/src/jpgraph.php";

require_once "/data/webroot/resource/php/src/jpgraph_pie.php";

require_once "/data/webroot/resource/php/src/jpgraph_pie3d.php";

$data=array(0=>3.5,1=>4.6,2=>9.1,3=>21.9,4=>42.3,5=>90.7,6=>183.5,7=>127.5,8=>61.4,9=>33.5,10=>11.5,11=>4.4);

//创建画布

$graph=new pieGraph(500,500);

//设置图像边界范围

$graph->img->SetMargin(30,30,80,30);

//设置标题

$graph->title->Set("piePlot3d Test");

//得到3D饼图对象

$piePlot3d=new piePlot3d($data);

//设置图例

$piePlot3d->SetLegends(array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"));

//设置图例位置

$graph->legend->Pos(0.1,0.15,"left","center");

//将绘制好的3D饼图加入到画布中

//输出

$graph->Stroke();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息