JPGRAPH生成统计图

 

JPGRAPH 生成统计图真的很方便,

而且有很多样式可以选.下边有四种使用实例(统计图,柱形图,饼形图,3D饼干形图)

直接进入主题:

1.下载jpgraph   http://www.aditus.nu/jpgraph/

  有php4.x 版和 php5.x 版 我下载的是php5.x版

2 .整理 :

   jpgraph/src

   下边的几个实例文件分别在 jpgraph/ 下

3. 配置:

   因为我要用到中文,所以要针对中文做些修改.

    jpgraph/src/jpg-config.inc.php 配置文件

   DEFINE(’CHINESE_TTF_FONT’,'bkai00mp.ttf’);  //字体文件,它会自己找windows 或 linux系统中找 fonts目录 也可以自己指定位置 

   jpgraph/src/jpgraph.php

  替换 $font_family=FF_FONT1 为 $font_family=FF_SIMSUN

  如果和我一样使用utf-8编码,那么简单的修改一下 jpgraph_gb2312.php 中的

 function gb2utf8($gb) {

      return $gb // 新加一行  , 如果是使用gb2312编码,这里看名字就知道什么意思了.

     ………

 直接看例字:

jpgraph/line.php 

  1. <?php
  2.  
  3. /**
  4.  
  5.  * http://www.zhaipeng.cn
  6.  
  7.  * 2008-1-30
  8.  
  9.  * JPGRAPH 生成X-Y线形统计图
  10.  
  11. */
  12.  
  13. include(’src/jpgraph.php’); //Graph类
  14.  
  15. include(’src/jpgraph_line.php’); //LinePlot 类
  16.  
  17. $data = array(19 , 23 , 34 ,36, 50 , 60 , 65, 70 , 78); //模拟数据
  18.  
  19. $graph = new Graph($width = 400 , $height = 300); //创建新的Graph对象
  20.  
  21. $graph->SetScale(”textlin”); //设置刻度模式
  22.  
  23. $graph->img->SetMargin(30 , 30 , 80 , 30) ; //设置图表边界
  24.  
  25. $graph->title->Set(”简体中文 繁體中文 test”) ; //设置图表标题
  26.  
  27. //$graph->title->SetFont(FF_SIMSUN,FS_BOLD); // 设置中文字体
  28.  
  29. $lineplot = new LinePlot($data); //创建新的LinePlot对象
  30.  
  31. $lineplot->SetLegend(”数据1″);//设置图例文字
  32.  
  33. $graph->subtitle->SetFont(FF_SIMSUN);
  34.  
  35. $graph->subsubtitle->SetFont(FF_SIMSUN);
  36.  
  37. $lineplot->SetColor(”red”); //设置曲线颜色
  38.  
  39. $graph->Add($lineplot); //在统计图上绘制曲线
  40.  
  41. $data2 = array(20 ,30 ,45 , 23 , 45 , 69 , 60 , 79 , 80);
  42.  
  43. $lineplot = new LinePlot($data2); //创建新的LinePlot对象
  44.  
  45. $lineplot->SetLegend(”数据2″);//设置图例文字
  46.  
  47. $lineplot->SetColor(”blue”); //设置曲线颜色
  48.  
  49. $graph->Add($lineplot); //在统计图上绘制曲线
  50.  
  51. $graph->Stroke() ; //输出图像
  52.  
  53. ?>

jpgraph/line.php 
PHP生成柱形图

  1. <?php
  2.  
  3. /**
  4.  * http://www.zhaipeng.cn
  5.  * 2008-1-30
  6.  * JPGRAPH 生成柱形图
  7.  */
  8. include('src/jpgraph.php');
  9. include('src/jpgraph_bar.php'); $data = array(18 ,23, 26 , 27 , 48 , 25 , 49); //模拟数据$graph = new Graph(400 , 300);
  10. $graph->SetScale("textlin"); //设置刻度模式$graph->SetShadow(); //设置阴影
  11. $graph->img->SetMargin(40 , 30 , 20 , 40) ;//设置边距$barplot = new BarPlot($data);$barplot->SetFillColor('blue') ; // 设置颜色
  12. $barplot->value->Show(); //设置显示数字
  13. $graph->Add($barplot); //将柱形图添加到图像中//设置标题和X-Y轴标题
  14. $graph->title->Set('测试柱形图');
  15. $graph->xaxis->title->Set("月份");
  16. $graph->yaxis->title->Set("总金额(元)");
  17.  
  18. /**
  19. * 设置字体,因为修改过jpgraph.php 所以可以不使用
  20. $graph->title->SetFont(FF_SIMSUN , FS_BOLD);
  21. $graph->yaxis->title->SetFont(FF_SIMSUN , FS_BOLD);
  22. $graph->xaxis->title->SetFont(FF_SIMSUN , FS_BOLD);
  23. */
  24.  
  25. $graph->Stroke();
  26. ?>

JPGRAPH 生成饼形图

JPGRAPH 生成饼形图

  1. <?php
  2. /**
  3.  * http://www.zhaipeng.cn
  4.  * 2008-1-30
  5.  * JPGRAPH 生成饼形图
  6.  */
  7.  include(’src/jpgraph.php’);
  8.  include(’src/jpgraph_pie.php’);$data = array(18 ,23, 26 , 27 , 48 , 25 , 49); //模拟数据$graph = new PieGraph(400 , 300);
  9.  $graph->SetShadow();$graph->title->Set("饼形图");
  10.  
  11. $pieplot = new PiePlot($data);
  12.  $graph->Add($pieplot);
  13.  
  14. $graph->Stroke();
  15. ?>

JPGRAPH 生成3D饼图

  1. <?php
  2. /**
  3.  * http://www.zhaipeng.cn
  4.  * 2008-1-30
  5.  * JPGRAPH 生成3D饼图
  6.  */
  7. include(’src/jpgraph.php’);
  8. include(’src/jpgraph_pie.php’);
  9. include(’src/jpgraph_pie3d.php’); $data = array(18 ,23, 26 , 27 , 48 , 25 , 49 , 50 , 45 , 23 , 20 ,30); //模拟数据
  10. $month = array('一月','二月','三月','四月' , '五月' , '六月' , '七月' , '八月' , '九月','十月','十一月','十二月');$graph = new PieGraph(400 , 300);
  11. $graph->SetShadow();
  12.  
  13. $graph->title->Set("3D饼图");
  14.  
  15. $pieplot = new PiePlot3D($data);
  16. $pieplot->SetCenter(0.4) ; //设置饼图的中心位置
  17. $pieplot->SetLegends($month); //设置图例
  18.  
  19. $graph->Add($pieplot);
  20.  
  21. $graph->Stroke('3d.jpg');
  22.  
  23. ?>

还有更多的例子在src/Examples    完!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值