PHP生成图表pChart的示例解析

这篇文章主要介绍了PHP生成图表pChart的示例解析,文中通过解析pChartd 工作流程和不同图表的代码展示,讲解的非常详细,对大家的学习或工作具有一定的参考价值,有需要的来和小编一起学习把吧。

pChart是一个开源的图表生成库,主要涉及3个Class:pChart.class, pData.class, pCache.class,可生成20多种简单或复杂的图表,支持PNG,JPG,GIF通用图片格式。数据源可以来自于Database,CSV,当然也可以手写。使用该程序PHP需要开启GD服务,先来看看pChart的工作流程:

PHP生成图表pChart的示例解析

主要分为三步:

* 读取用于生成图表数据(数据库、文件)

* 设计图表样式(圆角、底色等)

* 制作标签、题目、图例并生成图表

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

PHP生成图表pChart的示例解析

代码如下:

  1. <?php
  2. // Standard inclusions
  3. include("pChart/pData.class");
  4. include("pChart/pChart.class");
  5. // Dataset definition
  6. $DataSet = new pData;
  7. //图表数据
  8. $DataSet->AddPoint(array(1,4,-3,2,-3,3,2,1,0,7,4),"Serie1");
  9. $DataSet->AddPoint(array(3,3,-4,1,-2,2,1,0,-1,6,3),"Serie2");
  10. $DataSet->AddPoint(array(4,1,2,-1,-4,-2,3,2,1,2,2),"Serie3");
  11. $DataSet->AddAllSeries();
  12. $DataSet->SetAbsciseLabelSerie();
  13. //数据图例
  14. $DataSet->SetSerieName("Microsoft","Serie1");
  15. $DataSet->SetSerieName("IBM","Serie2");
  16. $DataSet->SetSerieName("Google","Serie3");
  17. // Initialise the graph
  18. $Test = new pChart(700,230);
  19. //设置图表尺寸、样式
  20. $Test->setFontProperties("Fonts/tahoma.ttf",8);
  21. $Test->setGraphArea(50,30,680,200);
  22. $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
  23. $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
  24. $Test->drawGraphArea(255,255,255,TRUE);
  25. $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
  26. $Test->drawGrid(4,TRUE,230,230,230,50);
  27. // Draw the 0 line
  28. $Test->setFontProperties("Fonts/MankSans.ttf",6);
  29. $Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  30. // Draw the bar graph
  31. //柱状图要使用drawBarGraph()
  32. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE,80);
  33. // Finish the graph
  34. //制作图例、标题、字体等属性
  35. $Test->setFontProperties("Fonts/MankSans.ttf",10);
  36. $Test->drawLegend(596,150,$DataSet->GetDataDescription(),255,255,255);
  37. $Test->setFontProperties("Fonts/MankSans.ttf",10);
  38. $Test->drawTitle(50,22,"Example",50,50,50,585);
  39. //生成图表
  40. $imageFile = "example12.png";
  41. $Test->Render($imageFile);
  42. echo '<img src="'.$imageFile.'">';
  43. ?>

这个是雷达效果的:

PHP生成图表pChart的示例解析

代码:

  1. <?php
  2. // Standard inclusions
  3. include("pChart/pData.class");
  4. include("pChart/pChart.class");
  5. // Dataset definition
  6. $DataSet = new pData;
  7. $DataSet->AddPoint(array("Memory","Disk","Network","Slots","CPU"),"Label");
  8. $DataSet->AddPoint(array(6,4,7,4,5),"Serie1");
  9. $DataSet->AddPoint(array(2,3,5,2,4),"Serie2");
  10. $DataSet->AddSerie("Serie1");
  11. $DataSet->AddSerie("Serie2");
  12. $DataSet->SetAbsciseLabelSerie("Label");
  13. $DataSet->SetSerieName("Reference","Serie1");
  14. $DataSet->SetSerieName("Tested computer","Serie2");
  15. // Initialise the graph
  16. $Test = new pChart(400,400);
  17. $Test->setFontProperties("Fonts/tahoma.ttf",8);
  18. $Test->drawFilledRoundedRectangle(7,7,393,393,5,240,240,240);
  19. $Test->drawRoundedRectangle(5,5,395,395,5,230,230,230);
  20. $Test->setGraphArea(30,30,370,370);
  21. $Test->drawFilledRoundedRectangle(30,30,370,370,5,255,255,255);
  22. $Test->drawRoundedRectangle(30,30,370,370,5,220,220,220);
  23. // Draw the radar graph
  24. //要使用drawRadarAxis()生成雷达效果
  25. $Test->drawRadarAxis($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE,20,120,120,120,230,230,230);
  26. $Test->drawFilledRadar($DataSet->GetData(),$DataSet->GetDataDescription(),50,20);
  27. // Finish the graph
  28. $Test->drawLegend(15,15,$DataSet->GetDataDescription(),255,255,255);
  29. $Test->setFontProperties("Fonts/tahoma.ttf",10);
  30. $Test->drawTitle(0,22,"Example",50,50,50,400);
  31. $imageFile = "example8.png";
  32. $Test->Render($imageFile);
  33. echo '<img src="'.$imageFile.'">';
  34. ?>

再看几个其他的效果

1,饼图:

PHP生成图表pChart的示例解析

2, 双座标曲线图:

PHP生成图表pChart的示例解析

3, 层叠柱状图:

PHP生成图表pChart的示例解析

4, 多图表:

PHP生成图表pChart的示例解析

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

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