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数组总结篇(一)
Sep 30 PHP
细谈php中SQL注入攻击与XSS攻击
Jun 10 PHP
php多层数组与对象的转换实例代码
Aug 05 PHP
php遍历目录与文件夹的多种方法详解
Nov 14 PHP
PHP通过插入mysql数据来实现多机互锁实例
Nov 05 PHP
php实现点击可刷新验证码
Nov 07 PHP
WordPress中编写自定义存储字段的相关PHP函数解析
Dec 25 PHP
thinkphp命名空间用法实例详解
Dec 30 PHP
Laravel框架中Blade模板的用法示例
Aug 30 PHP
PHP实现求连续子数组最大和问题2种解决方法
Dec 26 PHP
PHP使用反向Ajax技术实现在线客服系统详解
Jul 01 PHP
PHP过滤器 filter_has_var() 函数用法实例分析
Apr 23 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中MVC模式的模板引擎开发经验分享
2011/03/23 PHP
PHP中单例模式的使用场景与使用方法讲解
2019/03/18 PHP
用JavaScript对JSON进行模式匹配(Part 1-设计)
2010/07/17 Javascript
jquery里的each使用方法详解
2010/12/22 Javascript
Js,alert出现乱码问题的解决方法
2013/06/19 Javascript
jquery动态增加删减表格行特效
2015/11/20 Javascript
基于jquery实现日历签到功能
2020/09/11 Javascript
将html页面保存成图片,图片写入pdf的实现方法(推荐)
2016/09/17 Javascript
浅析 NodeJs 的几种文件路径
2017/06/07 NodeJs
Vue this.$router.push(参数)实现页面跳转操作
2020/09/09 Javascript
[01:43]3.19DOTA2发布会 三代刀塔人第三代
2014/03/25 DOTA
用python实现批量重命名文件的代码
2012/05/25 Python
在Python中使用SimpleParse模块进行解析的教程
2015/04/11 Python
python中的闭包用法实例详解
2015/05/05 Python
Python实现定时备份mysql数据库并把备份数据库邮件发送
2018/03/08 Python
python爬虫获取小区经纬度以及结构化地址
2018/12/30 Python
Python Pillow Image Invert
2019/01/22 Python
Python实现网页截图(PyQT5)过程解析
2019/08/12 Python
Python实现Restful API的例子
2019/08/31 Python
python变量的作用域是什么
2020/05/26 Python
Python面向对象特殊属性及方法解析
2020/09/16 Python
python+appium+yaml移动端自动化测试框架实现详解
2020/11/24 Python
使用css3制作登录表单的步骤
2014/04/07 HTML / CSS
html5 更新图片颜色示例代码
2014/07/29 HTML / CSS
阿迪达斯印尼官方网站:adidas印尼
2020/02/10 全球购物
俄罗斯园林植物网上商店:Garshinka
2020/07/16 全球购物
土建资料员岗位职责
2014/01/04 职场文书
车间统计员岗位职责
2014/01/05 职场文书
cf战队收人广告词
2014/03/14 职场文书
会计专业求职信
2014/08/10 职场文书
通知书大全
2015/04/27 职场文书
关于环保的广播稿
2015/12/17 职场文书
小学英语听课心得体会
2016/01/14 职场文书
2016年大学生社区服务活动总结
2016/04/06 职场文书
详解Django中 render() 函数的使用方法
2021/04/22 Python
Mysql案例刨析事务隔离级别
2021/09/25 MySQL