统计图形就我们会常到的数据图形了,如果三个数组以图形显示或楼盘以图形走向我们都会要用到图形,下面我来介绍一个php LIbchart图形生成类吧,很用的有需要的朋友可参考。
简单全数字或英文的就可以直接使用下面类了(libchart类大家可自行百度下载)
<?<BR> /*<BR> update by Leo<BR> It's draw the pic of Sheet,and it will take all the num on the pic.<BR> */<BR> require "./libchart/classes/libchart.php";<BR> class drawPic{<BR> var $chart;<BR> var $style;<BR> function drawPic($style="1",$width="500",$height="250"){<BR> $this->style=$style;<BR> if($style==1){<BR> //cylinder<BR> $this->chart = new VerticalBarChart($width,$height); <BR> }else if($style==2){<BR> //line<BR> $this->chart = new LineChart($width,$height);<BR> }else if($style==3){<BR> //Lump<BR> $this->chart = new PieChart($width,$height);<BR> }else{<BR> //cross<BR> $this->chart=new HorizontalBarChart($width,$height);<BR> }<BR> }<br><br> function draw($obj){<br><br> if($this->style==1||$this->style=="1"){<BR> //cylinder<BR> $dataSet = new XYDataSet() ;<BR> $this->chart->setTitle($obj->title);//title<BR> $arr=array();<BR> $arr=$obj->dataArray;<BR> foreach($arr as $key => $val){<BR> $dataSet->addPoint ( new Point($key,$val)) ;<BR> }<BR> $this->chart->setDataSet ( $dataSet ) ; <BR> $this->chart->render();<BR> }else if($this->style==2||$this->style=="2"){<BR> //line<BR> $this->chart->setTitle($obj->title);//title<BR> $arr=array();<BR> $arr=$obj->dataArray;<BR> $i=0;<BR> $dataSet = new XYSeriesDataSet();<BR> foreach($arr as $key => $val){<BR> $serie{$i}= new XYDataSet();<BR> foreach($val as $k => $v){<BR> $serie{$i}->addPoint(new Point($k,$v));<BR> }<BR> $dataSet->addSerie($key,$serie{$i});<BR> $i=$i+1;<BR> }<BR> $this->chart->setDataSet($dataSet);<BR> $this->chart->render();<BR> }else if($style==3){<BR> //Lump<BR> $dataSet = new XYDataSet() ;<BR> $this->chart->setTitle($obj->title);//title<BR> $arr=array();<BR> $arr=$obj->dataArray;<BR> foreach($arr as $key => $val){<BR> $dataSet->addPoint ( new Point($key."($val)",$val)) ;<BR> }<BR> $this->chart->setDataSet ( $dataSet ) ; <BR> $this->chart->render();<BR> }else{<BR> //cross<BR> $dataSet = new XYDataSet();<BR> $this->chart->setTitle($obj->title);//title<BR> $arr=array();<BR> $arr=$obj->dataArray;<BR> foreach($arr as $key => $val){<BR> $dataSet->addPoint ( new Point($key,$val)) ;<BR> }<BR> $this->chart->setDataSet($dataSet); <BR> $this->chart->render();<BR> }<BR> }<br><br> }<BR> class kkk{};<BR> $n=new drawPic("4");//it will set 1 or 2 or 3 or 4<BR> $k=new kkk();<BR> $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<BR> //$k->dataArray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==3<BR> //$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)<BR> $k->title="The Sheet title";<BR> $n->draw($k);<BR>?><BR>
红色字体为调用。方法1,2,4为相同的数组。3为线性图,有可能有两条线或者多条线的比较(也可以单线)。
如果要使用中文可能会发现libchart中文乱码 了,下面找了一个办法
我们的应用主源代码如下:
<BR><?php<BR> header("content-type:image/png"); <BR> require_once('libchart/classes/libchart.php'); <br><br> $chart = new VerticalBarChart( 500 , 250 ) ; // 参数表示需要创建的图像的宽和高<BR> $dataSet = new XYDataSet() ; // 实例化一个 XY 轴数据对象</P><P> // 为这个对象增加四组数据集合, Point 对象的第一个参数表示 X 轴坐标,<BR>// 第二个表示 Y 轴坐标<BR> $str = '二月';<br><br>$str = mb_convert_encoding($str, "html-entities","utf-8" );</P><P>$dataSet -> addPoint ( new Point( "Jan 2005" , 273 )) ;<BR> $dataSet -> addPoint ( new Point( "$str" , 120 )) ;<BR> $dataSet -> addPoint ( new Point( "March 2005" , 442 )) ;<BR> $dataSet -> addPoint ( new Point( "April 2005" , 600 )) ;<BR> // 把这个数据集合传递给图形对象<BR> $chart -> setDataSet ( $dataSet ) ;<BR> // 设置图形的标题,并把它作为一个 png 文件渲染<BR> $chart -> setTitle ( "统计图" ) ;<br><br> //$chart -> render ( "demo/generated/demo1.png" ) ; <BR> // 这里需要一个路径和文件名称 <BR> //就这么简单一个像下图一样美丽的柱状图就出来了<BR> $chart -> render () ; <BR>?><BR>
标红字的地方是为了解决中文乱码的。
2、标题乱码:
默认显示中文是乱码,这是由于编码的原因,做如下修改:
首先,更改libchar/libchart/classes/view/chart/Chart.php,找到如下内容:
public function setTitle($title) { <BR> $this->plot->setTitle($title);<BR> }<BR>
更改为:
public function setTitle($title) {<BR> $title = mb_convert_encoding($title, "html-entities","utf-8" );<BR> $this->plot->setTitle($title);<BR>}<BR>
第三步:就是上面某个博客里讲到的:
1、自己写的使用Libchart 库生成图表的php 文件以utf-8编码保存
2、找几个中文字体库,比如华文行楷、宋体等等,复制到libchart fonts目录下
3、修改libchart classes目录下的text.php 文件
第47、48行
$this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf"; <BR>$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";<BR>
改为
$this->fontCondensed = dirname(__FILE__) . "/../<mark style="color:transparent">本%文来源gaodaimacom搞#^代%!码网@</mark>搞代gaodaima码fonts/你找来的中文字体"; <BR>$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/你找来的中文字体";<BR>
我修改的:
public function Text() {<BR> $baseDir = dirname(__FILE__) . "/../../../";<br><br> // Free low-res fonts based on Bitstream Vera <BR> $this->fontCondensed = $baseDir . "fonts/FANGZHENGFANGSONG.ttf";<BR> $this->fontCondensedBold = $baseDir . "fonts/FANGZHENGFANGSONG.ttf";<BR> }<BR>
FANGZHENGFANGSONG.ttf 这个文件是我找的方正仿宋简体字库,我把中文名字改成那个样子了,其实不改也是可以的。