JPGRAPH生成统计图

一 30th, 2008

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 配置文件

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

jpgraph/src/jpgraph.php
  替换 $font_family=FF_FONT1 为 $font_family=FF_SIMSUN
  如果和我一样使用utf-8编码,那么简单的修改一下 jpgraph_gb2312.php 中的

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

 直接看例字:

jpgraph/line.php 

 
/**
 * http://www.zhaipeng.cn
 * 2008-1-30
 * JPGRAPH 生成X-Y线形统计图
*/
include(’src/jpgraph.php’); //Graph类
include(’src/jpgraph_line.php’); //LinePlot 类
$data = array(19 , 23 , 34 ,36, 50 , 60 , 65, 70 , 78); //模拟数据
$graph = new Graph($width = 400 , $height = 300); //创建新的Graph对象
$graph->SetScale(”textlin”); //设置刻度模式
$graph->img->SetMargin(30 , 30 , 80 , 30) ; //设置图表边界
$graph->title->Set(”简体中文 繁體中文 test”) ; //设置图表标题
//$graph->title->SetFont(FF_SIMSUN,FS_BOLD); // 设置中文字体
$lineplot = new LinePlot($data); //创建新的LinePlot对象
$lineplot->SetLegend(”数据1);//设置图例文字
$graph->subtitle->SetFont(FF_SIMSUN);
$graph->subsubtitle->SetFont(FF_SIMSUN);
$lineplot->SetColor(”red”); //设置曲线颜色
$graph->Add($lineplot); //在统计图上绘制曲线
$data2 = array(20 ,30 ,45 , 23 , 45 , 69 , 60 , 79 , 80);
$lineplot = new LinePlot($data2); //创建新的LinePlot对象
$lineplot->SetLegend(”数据2);//设置图例文字
$lineplot->SetColor(”blue”); //设置曲线颜色
$graph->Add($lineplot); //在统计图上绘制曲线
$graph->Stroke() ; //输出图像

jpgraph/line.php 
PHP生成柱形图

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

JPGRAPH 生成饼形图

JPGRAPH 生成饼形图

/**
 * http://www.zhaipeng.cn
 * 2008-1-30
 * JPGRAPH 生成饼形图
 */
 include(’src/jpgraph.php’);
 include(’src/jpgraph_pie.php’);
$data = array(18 ,23, 26 , 27 , 48 , 25 , 49); //模拟数据
$graph = new PieGraph(400 , 300);
$graph->SetShadow();$graph->title->Set("饼形图");
$pieplot = new PiePlot($data);
$graph->Add($pieplot);
$graph->Stroke();

JPGRAPH 生成3D饼图

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

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

标签:
  1. discovery
    一 31st, 200808:43

    好东东。。支持博主,希望以后可以看到更多原创的东东。俺是菜鸟。

  2. sunyu
    四 14th, 200900:14

    你的东西我看了,写得很好,是非常好,可是我把你的复制出来,却不能显示中文.特别是
    $graph->subtitle->SetFont(FF_SIMSUN);
    $graph->subsubtitle->SetFont(FF_SIMSUN);
    看不懂,我把subtitle改成lengend时,却可以显示中文了,你的标题的字体设置也注释掉了。我现在正在测试你其他的。

  3. sunyu
    四 14th, 200900:16

    可能是subsubtitle的问题。

  4. simaopig
    十一 11th, 200917:40

    写的很通俗,一会儿下载完试一下。哈。

    PS:这个下载速度真慢啊。。