php统计图形LIbchart类使用实例

统计图形就我们会常到的数据图形了,如果三个数组以图形显示或楼盘以图形走向我们都会要用到图形,下面我来介绍一个php LIbchart图形生成类吧,很用的有需要的朋友可参考.

简单全数字或英文的就可以直接使用下面类了,libchart类大家可自行百度下载:

  1. <?php
  2. /*
  3. update by Leo
  4. It's draw the pic of Sheet,and it will take all the num on the pic.
  5. */
  6. require "./libchart/classes/libchart.php";
  7. class drawPic{
  8. var $chart;
  9. var $style;
  10. function drawPic($,$width="500",$height="250"){
  11. $this->style=$style;
  12. if($style==1){
  13. //cylinder
  14. $this->chart = new VerticalBarChart($width,$height);
  15. }else if($style==2){
  16. //line
  17. $this->chart = new LineChart($width,$height);
  18. }else if($style==3){
  19. //Lump
  20. $this->chart = new PieChart($width,$height);
  21. }else{
  22. //cross
  23. $this->chart=new HorizontalBarChart($width,$height);
  24. }
  25. }
  26. function draw($obj){
  27. if($this->|$this->1"){
  28. //cylinder
  29. $dataSet = new XYDataSet() ;
  30. $this->chart->setTitle($obj->title);//title
  31. $arr=array();
  32. $arr=$obj->dataArray;
  33. foreach($arr as $key => $val){
  34. $dataSet->addPoint ( new Point($key,$val)) ;
  35. }
  36. $this->chart->setDataSet ( $dataSet ) ;
  37. $this->chart->render();
  38. }else if($this->|$this->2"){
  39. //line
  40. $this->chart->setTitle($obj->title);//title
  41. $arr=array();
  42. $arr=$obj->dataArray;
  43. $i=0;
  44. $dataSet = new XYSeriesDataSet();
  45. foreach($arr as $key => $val){
  46. $serie{$i}= new XYDataSet();
  47. foreach($val as $k => $v){
  48. $serie{$i}->addPoint(new Point($k,$v));
  49. }
  50. $dataSet->addSerie($key,$serie{$i});
  51. $i=$i+1;
  52. }
  53. $this->chart->setDataSet($dataSet);
  54. $this->chart->render();
  55. }else if($style==3){
  56. //Lump
  57. $dataSet = new XYDataSet() ;
  58. $this->chart->setTitle($obj->title);//title
  59. $arr=array();
  60. $arr=$obj->dataArray;
  61. foreach($arr as $key => $val){
  62. $dataSet->addPoint ( new Point($key."($val)",$val)) ;
  63. }
  64. $this->chart->setDataSet ( $dataSet ) ;
  65. $this->chart->render();
  66. }else{
  67. //cross
  68. $dataSet = new XYDataSet();
  69. $this->chart->setTitle($obj->title);//title
  70. $arr=array();
  71. $arr=$obj->dataArray;
  72. foreach($arr as $key => $val){
  73. $dataSet->addPoint ( new Point($key,$val)) ;
  74. }
  75. $this->chart->setDataSet($dataSet);
  76. $this->chart->render();
  77. }
  78. }
  79. }
  80. class kkk{};
  81. $n=new drawPic("4");//it will set 1 or 2 or 3 or 4
  82. $k=new kkk();
  83. $k->dataArray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==1 or style=2 or style=4//开源代码phpfensi.com
  84. //$k->dataArray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==3
  85. //$k->dataArray=array("yi"=>array("2000"=>"30","2001"=>"40","2002"=>"50","2004"=>"60"),"er"=>array("2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90"),"san"=>array("33"=>"12","45"=>"56","89"=>"45","86"=>"49"));//style==2 and the years will show first array to block.(it will be show 2000 2001 2002 2004)
  86. $k->title="The Sheet title";
  87. $n->draw($k);
  88. ?>

方法1,2,4为相同的数组,3为线性图,有可能有两条线或者多条线的比较,也可以单线,如果要使用中文可能会发现libchart中文乱码 了,下面找了一个办法.

我们的应用主源代码如下:

  1. <?php
  2. header("content-type:image/png");
  3. require_once('libchart/classes/libchart.php');
  4. $chart = new VerticalBarChart( 500 , 250 ) ; // 参数表示需要创建的图像的宽和高
  5. $dataSet = new XYDataSet() ; // 实例化一个 XY 轴数据对象
  6. // 为这个对象增加四组数据集合, Point 对象的第一个参数表示 X 轴坐标,
  7. // 第二个表示 Y 轴坐标
  8. $str = '二月';
  9. $str = mb_convert_encoding($str, "html-entities","utf-8" );
  10. $dataSet -> addPoint ( new Point( "Jan 2005" , 273 )) ;
  11. $dataSet -> addPoint ( new Point( "$str" , 120 )) ;
  12. $dataSet -> addPoint ( new Point( "March 2005" , 442 )) ;
  13. $dataSet -> addPoint ( new Point( "April 2005" , 600 )) ;
  14. // 把这个数据集合传递给图形对象
  15. $chart -> setDataSet ( $dataSet ) ;
  16. // 设置图形的标题,并把它作为一个 png 文件渲染
  17. $chart -> setTitle ( "统计图" ) ;
  18. //$chart -> render ( "demo/generated/demo1.png" ) ;
  19. // 这里需要一个路径和文件名称
  20. //就这么简单一个像下图一样美丽的柱状图就出来了
  21. $chart -> render () ;
  22. ?>

标红字的地方是为了解决中文乱码的.

2、标题乱码:

默认显示中文是乱码,这是由于编码的原因,做如下修改:首先,更改libchar/libchart/classes/view/chart/Chart.php,找到如下内容,代码如下:

public function setTitle($title) {

$this->plot->setTitle($title);
}

更改为如下代码:

  1. public function setTitle($title) {
  2. $title = mb_convert_encoding($title, "html-entities","utf-8" );
  3. $this->plot->setTitle($title);
  4. }

第三步:就是上面某个博客里讲到的:

1、自己写的使用Libchart 库生成图表的php 文件以utf-8编码保存.

2、找几个中文字体库,比如华文行楷、宋体等等,复制到libchart fonts目录下.

3、修改libchart classes目录下的text.php 文件.

第47、48行,代码如下:

  1. $this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf";
  2. $this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";

改为:

$this->fontCondensed = dirname(__FILE__) . "/../fonts/你找来的中文字体";

$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/你找来的中文字体";

我修改的代码如下:

  1. public function Text() {
  2. $baseDir = dirname(__FILE__) . "/../../../";
  3. // Free low-res fonts based on Bitstream Vera <http://dejavu.sourceforge.net/wiki/>
  4. $this->fontCondensed = $baseDir . "fonts/FANGZHENGFANGSONG.ttf";
  5. $this->fontCondensedBold = $baseDir . "fonts/FANGZHENGFANGSONG.ttf";
  6. }

FANGZHENGFANGSONG.ttf 这个文件是我找的方正仿宋简体字库,我把中文名字改成那个样子了,其实不改也是可以的.

主要是基本操作,然后又疑问的地方,按照上面说的做了,应该就是已经可以了,再次谢谢提供这些方法的热心网友,大家多多交流呀.