欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP生成图表pChart的示例解析

程序员文章站 2022-03-18 14:33:53
pchart是一个开源的图表生成库,主要涉及3个class:pchart.class, pdata.class, pcache.class,可生成20多种简单或复杂的图表,支持png,jpg,gif通...

pchart是一个开源的图表生成库,主要涉及3个class:pchart.class, pdata.class, pcache.class,可生成20多种简单或复杂的图表,支持png,jpg,gif通用图片格式。数据源可以来自于database,csv,当然也可以手写。使用该程序php需要开启gd服务,先来看看pchart的工作流程:

PHP生成图表pChart的示例解析

主要分为三步:

  1. * 读取用于生成图表数据(数据库、文件)
  2. * 设计图表样式(圆角、底色等)
  3. * 制作标签、题目、图例并生成图表

下面看一个简单的柱状图表:

PHP生成图表pChart的示例解析

代码如下:

<?php
 // standard inclusions 
 include("pchart/pdata.class");
 include("pchart/pchart.class");

 // dataset definition 
 $dataset = new pdata;
 //图表数据
 $dataset->addpoint(array(1,4,-3,2,-3,3,2,1,0,7,4),"serie1");
 $dataset->addpoint(array(3,3,-4,1,-2,2,1,0,-1,6,3),"serie2");
 $dataset->addpoint(array(4,1,2,-1,-4,-2,3,2,1,2,2),"serie3");
 $dataset->addallseries();
 $dataset->setabsciselabelserie();
 //数据图例
 $dataset->setseriename("microsoft","serie1");
 $dataset->setseriename("ibm","serie2");
 $dataset->setseriename("google","serie3");

 // initialise the graph
 $test = new pchart(700,230);
 //设置图表尺寸、样式
 $test->setfontproperties("fonts/tahoma.ttf",8);
 $test->setgrapharea(50,30,680,200);
 $test->drawfilledroundedrectangle(7,7,693,223,5,240,240,240);
 $test->drawroundedrectangle(5,5,695,225,5,230,230,230);
 $test->drawgrapharea(255,255,255,true);
 $test->drawscale($dataset->getdata(),$dataset->getdatadescription(),scale_normal,150,150,150,true,0,2,true);
 $test->drawgrid(4,true,230,230,230,50);

 // draw the 0 line
 $test->setfontproperties("fonts/manksans.ttf",6);
 $test->drawtreshold(0,143,55,72,true,true);

 // draw the bar graph
 //柱状图要使用drawbargraph()
 $test->drawbargraph($dataset->getdata(),$dataset->getdatadescription(),true,80);


 // finish the graph
 //制作图例、标题、字体等属性
 $test->setfontproperties("fonts/manksans.ttf",10);
 $test->drawlegend(596,150,$dataset->getdatadescription(),255,255,255);
 $test->setfontproperties("fonts/manksans.ttf",10);
 $test->drawtitle(50,22,"example",50,50,50,585);
 
 //生成图表
 $imagefile = "example12.png";
 $test->render($imagefile);
 echo '<img src="'.$imagefile.'">';
?>

这个是雷达效果的:

PHP生成图表pChart的示例解析

代码:

<?php
 // standard inclusions 
 include("pchart/pdata.class");
 include("pchart/pchart.class");

 // dataset definition 
 $dataset = new pdata;
 $dataset->addpoint(array("memory","disk","network","slots","cpu"),"label");
 $dataset->addpoint(array(6,4,7,4,5),"serie1");
 $dataset->addpoint(array(2,3,5,2,4),"serie2");
 $dataset->addserie("serie1");
 $dataset->addserie("serie2");
 $dataset->setabsciselabelserie("label");


 $dataset->setseriename("reference","serie1");
 $dataset->setseriename("tested computer","serie2");

 // initialise the graph
 $test = new pchart(400,400);
 $test->setfontproperties("fonts/tahoma.ttf",8);
 $test->drawfilledroundedrectangle(7,7,393,393,5,240,240,240);
 $test->drawroundedrectangle(5,5,395,395,5,230,230,230);
 $test->setgrapharea(30,30,370,370);
 $test->drawfilledroundedrectangle(30,30,370,370,5,255,255,255);
 $test->drawroundedrectangle(30,30,370,370,5,220,220,220);

 // draw the radar graph
 //要使用drawradaraxis()生成雷达效果
 $test->drawradaraxis($dataset->getdata(),$dataset->getdatadescription(),true,20,120,120,120,230,230,230);
 $test->drawfilledradar($dataset->getdata(),$dataset->getdatadescription(),50,20);

 // finish the graph
 $test->drawlegend(15,15,$dataset->getdatadescription(),255,255,255);
 $test->setfontproperties("fonts/tahoma.ttf",10);
 $test->drawtitle(0,22,"example",50,50,50,400);
 
 $imagefile = "example8.png";
 $test->render($imagefile);
 echo '<img src="'.$imagefile.'">';
?>

再看几个其他的效果

1,饼图:

PHP生成图表pChart的示例解析

2, 双座标曲线图:

PHP生成图表pChart的示例解析

3, 层叠柱状图:

PHP生成图表pChart的示例解析

4, 多图表:

PHP生成图表pChart的示例解析

图表的种类已经相当丰富了,具体图表设置请参考

http://pchart.sourceforge.net/documentation.php?topic=pchart

::源代码下载:::

http://xiazai.jb51.net/202007/yuanma/pchart_jb51.rar

到此这篇关于php生成图表pchart的示例解析的文章就介绍到这了,更多相关php生成图表pchart内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: PHP 图表 pChart