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版
Oct 09 PHP
PHP怎样调用MSSQL的存储过程
Oct 09 PHP
PHP安装攻略:常见问题解答(一)
Oct 09 PHP
PHP 5.3新特性命名空间规则解析及高级功能
Mar 11 PHP
drupal 代码实现URL重写
May 04 PHP
基于PHP的cURL快速入门教程 (小偷采集程序)
Jun 02 PHP
PHP 中检查或过滤IP地址的实现代码
Nov 27 PHP
php5.3 不支持 session_register() 此函数已启用的解决方法
Nov 12 PHP
深入理解PHP中mt_rand()随机数的安全
Oct 12 PHP
PHP让网站移动访问更加友好方法
Feb 14 PHP
PHP中抽象类,接口功能、定义方法示例
Feb 26 PHP
php查询内存信息操作示例
May 09 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
使用php4加速网络传输
2006/10/09 PHP
PHP检测移动设备类mobile detection使用实例
2014/04/14 PHP
php递归创建目录的方法
2015/02/02 PHP
PHP表单提交后引号前自动加反斜杠的原因及三种办法关闭php魔术引号
2015/09/30 PHP
利用php操作memcache缓存的基础方法示例
2017/08/02 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
2019/12/25 PHP
javascript实现划词标记+划词搜索功能
2007/03/06 Javascript
身份证号码前六位所代表的省,市,区, 以及地区编码下载
2007/04/12 Javascript
JavaScript(JS) 压缩 / 混淆 / 格式化 批处理工具
2010/12/10 Javascript
浅析js中取绝对值的2种方法
2013/07/09 Javascript
Javascript限制网页只能在微信内置浏览器中访问
2014/11/09 Javascript
JavaScript数组常用操作技巧汇总
2014/11/17 Javascript
JavaScript编写连连看小游戏
2015/07/07 Javascript
简单实现js间歇或无缝滚动效果
2016/06/29 Javascript
浅谈Node.js:fs文件系统模块
2016/12/08 Javascript
使用jQuery实现简单的tab框实例
2017/08/22 jQuery
vue中beforeRouteLeave实现页面回退不刷新的示例代码
2019/11/01 Javascript
Vue通过provide inject实现组件通信
2020/09/03 Javascript
Element el-button 按钮组件的使用详解
2021/02/01 Javascript
python实现多线程网页下载器
2018/04/15 Python
对python中的six.moves模块的下载函数urlretrieve详解
2018/12/19 Python
在Pycharm terminal中字体大小设置的方法
2019/01/16 Python
基于python 微信小程序之获取已存在模板消息列表
2019/08/05 Python
为什么黑客都用python(123个黑客必备的Python工具)
2020/01/31 Python
PyCharm 专业版安装图文教程
2020/02/20 Python
Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】
2020/05/29 Python
基于selenium及python实现下拉选项定位select
2020/07/22 Python
Html5 audio标签样式的修改
2016/01/28 HTML / CSS
加拿大时尚少女服装品牌:Garage
2016/10/10 全球购物
澳大利亚香水在线:Price Rite Mart
2017/12/28 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
护理专业本科生自荐信
2013/10/01 职场文书
餐饮投资计划书
2014/04/25 职场文书
讲党性心得体会
2014/09/03 职场文书
熟背这些句子,让您的英语口语突飞猛进(135句)
2019/09/06 职场文书
Python实现对齐打印 format函数的用法
2022/04/28 Python