php生成图形(Libchart)实例


Posted in PHP onNovember 06, 2013

统计图形就我们会常到的数据图形了,如果三个数组以图形显示或楼盘以图形走向我们都会要用到图形,下面我来介绍一个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 这个文件是我找的方正仿宋简体字库,我把中文名字改成那个样子了,其实不改也是可以的。
PHP 相关文章推荐
php查看session内容的函数
Aug 27 PHP
一些需要禁用的PHP危险函数(disable_functions)
Feb 23 PHP
PHP查询数据库中满足条件的记录条数(两种实现方法)
Jan 29 PHP
php实现从上传文件创建缩略图的方法
Apr 02 PHP
PHP+jQuery+Ajax实现用户登录与退出
Apr 27 PHP
PHP获取客户端及服务器端IP的封装类
Jul 21 PHP
PHP创建单例后台进程的方法示例
May 23 PHP
php 可变函数使用小结
Jun 12 PHP
PHP时间日期增减操作示例【date strtotime实现加一天、加一月等操作】
Dec 21 PHP
Laravel框架实现的批量删除功能示例
Jan 16 PHP
PHP标准库(PHP SPL)详解
Mar 16 PHP
浅谈php调用python文件
Mar 29 PHP
php ZipArchive压缩函数详解实例
Nov 06 #PHP
php根据分类合并数组的方法实例详解
Nov 06 #PHP
php foreach循环中使用引用的问题
Nov 06 #PHP
php用正则表达式匹配中文实例详解
Nov 06 #PHP
php引用传值实例详解学习
Nov 06 #PHP
php二维数组排序详解
Nov 06 #PHP
PHP Curl多线程原理实例详解
Nov 06 #PHP
You might like
如何使用纯PHP实现定时器任务(Timer)
2015/07/31 PHP
php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错
2015/12/07 PHP
使用PHP如何实现高效安全的ftp服务器(二)
2015/12/30 PHP
php版微信小店调用api示例代码
2016/11/12 PHP
Javascript 学习书 推荐
2009/06/13 Javascript
jQuery实现跟随鼠标运动图层效果的方法
2015/02/02 Javascript
jQuery实现鼠标滑过Div层背景变颜色的方法
2015/02/17 Javascript
JavaScript事件类型中UI事件详解
2016/01/14 Javascript
javascript移动开发中touch触摸事件详解
2016/03/18 Javascript
Bootstrap模态框调用功能实现方法
2016/09/19 Javascript
jQuery EasyUI 获取tabs的实例解析
2016/12/06 Javascript
EasyUi 打开对话框后控件赋值及赋值后不显示的问题解决办法
2017/01/19 Javascript
JS实现中国公民身份证号码有效性验证
2017/02/20 Javascript
JS实现分页浏览横向图片(类轮播)实例代码
2017/11/06 Javascript
Vue中控制v-for循环次数的实现方法
2018/09/26 Javascript
微信小程序实现星级评价效果
2018/12/28 Javascript
详解vue-cli项目开发/生产环境代理实现跨域请求
2019/07/23 Javascript
Python利用QQ邮箱发送邮件的实现方法(分享)
2017/06/09 Python
在pandas中一次性删除dataframe的多个列方法
2018/04/10 Python
tensorflow学习笔记之mnist的卷积神经网络实例
2018/04/15 Python
Python用于学习重要算法的模块pygorithm实例浅析
2018/08/16 Python
Python求一批字符串的最长公共前缀算法示例
2019/03/02 Python
python使用MQTT给硬件传输图片的实现方法
2019/05/05 Python
django框架用户权限中的session缓存到redis中的方法
2019/08/06 Python
Python3视频转字符动画的实例代码
2019/08/29 Python
解决Tensorboard 不显示计算图graph的问题
2020/02/15 Python
基于Tensorflow的MNIST手写数字识别分类
2020/06/17 Python
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
2020/11/12 Python
html5 css3 动态气泡按钮实例演示
2012/12/02 HTML / CSS
财政专业求职信范文
2014/02/19 职场文书
房产协议书范本
2014/10/18 职场文书
加强作风建设演讲稿
2014/10/24 职场文书
外贸英文求职信范文
2015/03/19 职场文书
入党宣誓仪式主持词
2015/06/29 职场文书
优秀班干部主要事迹材料
2015/11/04 职场文书
使用canvas对video视频某一刻截图功能
2021/09/25 HTML / CSS