php生成图形(Libchart)实例
统计图形就我们会常到的数据图形了,如果三个数组以图形显示或楼盘以图形走向我们都会要用到图形,下面我来介绍一个php libchart图形生成类吧,很用的有需要的朋友可参考。
简单全数字或英文的就可以直接使用下面类了(libchart类大家可自行百度下载)
<?
/*
update by leo
it's draw the pic of sheet,and it will take all the num on the pic.
*/
require "./libchart/classes/libchart.php";
class drawpic{
var $chart;
var $style;
function drawpic($style="1",$width="500",$height="250"){
$this->style=$style;
if($style==1){
//cylinder
$this->chart = new verticalbarchart($width,$height);
}else if($style==2){
//line
$this->chart = new linechart($width,$height);
}else if($style==3){
//lump
$this->chart = new piechart($width,$height);
}else{
//cross
$this->chart=new horizontalbarchart($width,$height);
}
}
function draw($obj){
if($this->style==1||$this->style=="1"){
//cylinder
$dataset = new xydataset() ;
$this->chart->settitle($obj->title);//title
$arr=array();
$arr=$obj->dataarray;
foreach($arr as $key => $val){
$dataset->addpoint ( new point($key,$val)) ;
}
$this->chart->setdataset ( $dataset ) ;
$this->chart->render();
}else if($this->style==2||$this->style=="2"){
//line
$this->chart->settitle($obj->title);//title
$arr=array();
$arr=$obj->dataarray;
$i=0;
$dataset = new xyseriesdataset();
foreach($arr as $key => $val){
$serie{$i}= new xydataset();
foreach($val as $k => $v){
$serie{$i}->addpoint(new point($k,$v));
}
$dataset->addserie($key,$serie{$i});
$i=$i+1;
}
$this->chart->setdataset($dataset);
$this->chart->render();
}else if($style==3){
//lump
$dataset = new xydataset() ;
$this->chart->settitle($obj->title);//title
$arr=array();
$arr=$obj->dataarray;
foreach($arr as $key => $val){
$dataset->addpoint ( new point($key."($val)",$val)) ;
}
$this->chart->setdataset ( $dataset ) ;
$this->chart->render();
}else{
//cross
$dataset = new xydataset();
$this->chart->settitle($obj->title);//title
$arr=array();
$arr=$obj->dataarray;
foreach($arr as $key => $val){
$dataset->addpoint ( new point($key,$val)) ;
}
$this->chart->setdataset($dataset);
$this->chart->render();
}
}
}
class kkk{};
$n=new drawpic("4");//it will set 1 or 2 or 3 or 4
$k=new kkk();
$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
//$k->dataarray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==3
//$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)
$k->title="the sheet title";
$n->draw($k);
?>
红色字体为调用。方法1,2,4为相同的数组。3为线性图,有可能有两条线或者多条线的比较(也可以单线)。
如果要使用中文可能会发现libchart中文乱码 了,下面找了一个办法
我们的应用主源代码如下:
<?php
header("content-type:image/png");
require_once('libchart/classes/libchart.php');
$chart = new verticalbarchart( 500 , 250 ) ; // 参数表示需要创建的图像的宽和高
$dataset = new xydataset() ; // 实例化一个 xy 轴数据对象
// 为这个对象增加四组数据集合, point 对象的第一个参数表示 x 轴坐标,
// 第二个表示 y 轴坐标
$str = '二月';
$str = mb_convert_encoding($str, "html-entities","utf-8" );
$dataset -> addpoint ( new point( "jan 2005" , 273 )) ;
$dataset -> addpoint ( new point( "$str" , 120 )) ;
$dataset -> addpoint ( new point( "march 2005" , 442 )) ;
$dataset -> addpoint ( new point( "april 2005" , 600 )) ;
// 把这个数据集合传递给图形对象
$chart -> setdataset ( $dataset ) ;
// 设置图形的标题,并把它作为一个 png 文件渲染
$chart -> settitle ( "统计图" ) ;
//$chart -> render ( "demo/generated/demo1.png" ) ;
// 这里需要一个路径和文件名称
//就这么简单一个像下图一样美丽的柱状图就出来了
$chart -> render () ;
?>
标红字的地方是为了解决中文乱码的。
2、标题乱码:
默认显示中文是乱码,这是由于编码的原因,做如下修改:
首先,更改libchar/libchart/classes/view/chart/chart.php,找到如下内容:
public function settitle($title) {
$this->plot->settitle($title);
}
更改为:
public function settitle($title) {
$title = mb_convert_encoding($title, "html-entities","utf-8" );
$this->plot->settitle($title);
}
第三步:就是上面某个博客里讲到的:
1、自己写的使用libchart 库生成图表的php 文件以utf-8编码保存
2、找几个中文字体库,比如华文行楷、宋体等等,复制到libchart fonts目录下
3、修改libchart classes目录下的text.php 文件
第47、48行
$this->fontcondensed = dirname(__file__) . "/../fonts/dejavusanscondensed.ttf";
$this->fontcondensedbold = dirname(__file__) . "/../fonts/dejavusanscondensed-bold.ttf";
改为
$this->fontcondensed = dirname(__file__) . "/../fonts/你找来的中文字体";
$this->fontcondensedbold = dirname(__file__) . "/../fonts/你找来的中文字体";
我修改的:
public function text() {
$basedir = dirname(__file__) . "/../../../";
// free low-res fonts based on bitstream vera <http://dejavu.sourceforge.net/wiki/>
$this->fontcondensed = $basedir . "fonts/fangzhengfangsong.ttf";
$this->fontcondensedbold = $basedir . "fonts/fangzhengfangsong.ttf";
}
fangzhengfangsong.ttf 这个文件是我找的方正仿宋简体字库,我把中文名字改成那个样子了,其实不改也是可以的。