在前面的JpGraph下载及使用 中,我对JpGraph项目进行了大概的介绍,并对一个简单样例程序进行了说明,下面是我经过1天的编写调试,整理出来的已经封装了部分常见常用函数的类,感觉自己这个写的还是比较规范的,呵呵。下面是代码,想学php的可以深入看一下,并不太难,不过由于时间关系,我并没有把所有的图形对象进行封装,如果有机会以后会出一个更全的类。下面是具体的代码内容:<?php/** *  ECSHOP Jpgraph图表类库 *  ============================================================== *  利用开源的jpgraph库实现对各类型数据以图表,走势图的形式表现出来。 *  ============================================================== *  $Author: 红心草 ([email protected]) $ *  $WebSite: http://www.hongxincao.com *  $id: cls_jpgraph.php 2010-10-03 14:20 hongxincao $**/class cls_jpgraph{ var $db = NULL; var $ydata = array(); var $width = 350; var $height = 250; var $graph = ''; //图形对象 var $piegraph = ''; //丙状图形 var $lineplot = ''; //线性对象 var $barpot = ''; //柱状对象 var $gbplat = ''; //柱状组对象 var $txt = ''; //文本对象 var $p1 = ''; //丙状图对象 var $scale = ''; //图表类型? (textlin,textlog,intlin) var $yscale = '';   // (log,) var $xgrid = false; //x轴网格显示 var $ygrid = false; //y轴网格显示 var $title = ''; //标题 var $subtitle = ''; //子标题(一般为日期) var $xaxis = ''; //x轴名称 var $yaxis = ''; //y轴名称 /*margin position*/ var $left = 0; var $right = 0; var $top = 0; var $bottom = 0; /** *构造函数 */ function cls_jpgraph($width=350,$height=250) { $this->width = $width; $this->height = $height; $this->graph = new Graph($this->width,$this->height); } /** * 图表类库的构造函数 * */ /* function __construct($parms,$width,$height) { cls_jpgraph($parms,$width,$height); } */ /*图片基本信息设置*/ function set_jpgraph($scale='intlin',$title='',$subtitle='',$bgcolor='',$xaxis='',               $yaxis='',$xgrid=false,$ygrid=false,$margin='') { $this->scale = $scale; $this->title = $title; $this->subtitle = $subtitle; $this->xaxis = $xaxis; $this->yaxis = $yaxis; $this->xgrid = $xgrid; $this->ygrid = $ygrid; if(!empty($scale)) { $this->graph->SetScale($this->scale); } else { $this->graph->SetScale('intlin'); } $this->graph->xgrid->Show($this->xgrid,$this->xgrid); $this->graph->ygrid->Show($this->ygrid,$this->ygrid); if(!empty($bgcolor)) { $this->graph->SetMarginColor($bgcolor); } /*如果手工设置了图片位置*/ if(is_array($margin)) { while(list($key,$val) = each($margin)) { $this->$key = $val; } $this->graph->SetMargin($this->left,$this->right,$this->top,$this->bottom); } if(!empty($this->title)) { $this->graph->title->Set($this->title); } if(!empty($this->subtitle)) { $this->graph->subtitle->Set($this->subtitle); } else { $this->graph->subtitle->Set('('.date('Y-m-d').')'); //默认子标题设置为当前日期 } if(!empty($this->xaxis)) { $this->graph->xaxis->title->Set($this->xaxis); } if(!empty($this->yaxis)) { $this->graph->yaxis->title->Set($this->yaxis); } } /*创建线性关系图表(linear plot)*/ function create_lineplot($parms,$color='black',$weight=1) { $this->ydata = $parms; $this->lineplot = new LinePlot($this->ydata); $this->lineplot->SetColor($color); $this->lineplot->SetWeight($weight); return $this->lineplot; } /*创建柱状图表(bar pot)*/ function create_barpot($parms,$color='black',$width='0') { $this->ydata = $parms; $this->barpot = new BarPlot($this->ydata); $this->barpot->SetFillColor($color); if(!empty($width)) { $this->barpot->SetWidth($width); } return $this->barpot; } /*创建数据柱状图表组*/ function create_bargroup($plotarr,$width='0.8') { $this->gbplot = new GroupBarPlot($plotarr); $this->gbplot->SetWidth($width); return $this->gbplot; } /*创建文本内容*/ function create_text($str,$postion='',$color='black') { $this->txt = new Text($str); if(is_array($postion)) { while(list($key,$val) = each($postion)) { $this->$key = $val; } $this->txt->SetPos($this->left,$this->top); } else { $this->txt->SetPos(10,20); } $this->txt->SetColor($color); $this->graph->Add($this->txt); } /*创建丙状图表*/ function create_pie($parms,$title,$type='3D',$size='0.5',$center='0.5',$width='350',$height='250') { $this->width = $width; $this->height = $height; $this->piegraph = new PieGraph(300,200); $this->piegraph->SetShadow(); $this->piegraph->title->Set($title); if('3D' != $type) { $this->p1 = new PiePlot($parms); $this->piegraph->Add($this->p1); } else { $this->p1 = new PiePlot3D($parms); $this->p1->SetSize($size); $this->p1->SetCenter($center);// $this->p1->SetLegends($gDateLocale->GetShortMonth()); $this->piegraph->Add($this->p1); } $this->piegraph->Stroke(); } function get_auth_code($length=4) { $spam = new AntiSpam(); $chars = $spam->Rand($length); if( $spam->Stroke() === false ) { return false; } return $chars; } /*完成图形创建并显示*/ function display($obj) { $this->graph->Add($obj); $this->graph->Stroke(); }}?>上面的类文件对JpGraph库文件进行了封装,减低了JpGraph库程序调用的难度,对于创建图形只需要简单的几行代码就可以实现了,具体大家可以仔细读一下代码(JpGraph库文件需要单独下载,如果你不清楚下载地址的话,可以查看我的上一篇文章:JpGraph下载及使用 )。本来还想继续写点内容,发现光代码就够多了,在这篇文章中就不多说了。在下一篇文章中,我会写一个具体的测试程序来详细介绍说明该类文件的调用,如果有想详细了解JpGraph库使用的话,可以随时关注我的红心草博客 。文章来源:红心草博客原文地址:http://www.hongxincao.com/archives/330.html