这篇文章主要为大家详细介绍了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);
?>
我们的应用主源代码如下:
代码如下:
<?php
/* 四海网 www.q1010.com */
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 () ;
?>
代码如下:
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);
}
代码如下:
$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";
}
本文来自:http://www.q1010.com/173/14644-0.html
注:关于php生成图形(Libchart)的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。