国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php統計圖形LIbchart類使用實例

2024-09-04 11:44:29
字體:
來源:轉載
供稿:網友

統計圖形就我們會常到的數據圖形了,如果三個數組以圖形顯示或樓盤以圖形走向我們都會要用到圖形,下面我來介紹一個php LIbchart圖形生成類吧,很用的有需要的朋友可參考.

簡單全數字或英文的就可以直接使用下面類了,libchart類大家可自行百度下載:

  1. <?php 
  2.  /* 
  3.   update by Leo 
  4.   It's draw the pic of Sheet,and it will take all the num on the pic. 
  5.  */ 
  6.  require "./libchart/classes/libchart.php"
  7.  class drawPic{ 
  8.   var $chart
  9.   var $style
  10.   function drawPic($style="1",$width="500",$height="250"){ 
  11.    $this->style=$style
  12.    if($style==1){ 
  13.     //cylinder 
  14.     $this->chart = new VerticalBarChart($width,$height);  
  15.    }else if($style==2){ 
  16.     //line 
  17.     $this->chart = new LineChart($width,$height); 
  18.    }else if($style==3){ 
  19.     //Lump 
  20.     $this->chart = new PieChart($width,$height); 
  21.    }else
  22.     //cross 
  23.     $this->chart=new HorizontalBarChart($width,$height); 
  24.    } 
  25.   } 
  26.    
  27.   function draw($obj){ 
  28.     
  29.    if($this->style==1||$this->style=="1"){ 
  30.     //cylinder 
  31.     $dataSet = new XYDataSet() ; 
  32.     $this->chart->setTitle($obj->title);//title 
  33.     $arr=array(); 
  34.     $arr=$obj->dataArray; 
  35.     foreach($arr as $key => $val){ 
  36.      $dataSet->addPoint ( new Point($key,$val)) ; 
  37.     } 
  38.     $this->chart->setDataSet ( $dataSet ) ;  
  39.     $this->chart->render(); 
  40.    }else if($this->style==2||$this->style=="2"){ 
  41.     //line 
  42.     $this->chart->setTitle($obj->title);//title 
  43.     $arr=array(); 
  44.     $arr=$obj->dataArray; 
  45.     $i=0; 
  46.     $dataSet = new XYSeriesDataSet(); 
  47.     foreach($arr as $key => $val){ 
  48.      $serie{$i}= new XYDataSet(); 
  49.      foreach($val as $k => $v){ 
  50.       $serie{$i}->addPoint(new Point($k,$v)); 
  51.      } 
  52.      $dataSet->addSerie($key,$serie{$i}); 
  53.      $i=$i+1; 
  54.     } 
  55.     $this->chart->setDataSet($dataSet); 
  56.     $this->chart->render(); 
  57.    }else if($style==3){ 
  58.     //Lump 
  59.     $dataSet = new XYDataSet() ; 
  60.     $this->chart->setTitle($obj->title);//title 
  61.     $arr=array(); 
  62.     $arr=$obj->dataArray; 
  63.     foreach($arr as $key => $val){ 
  64.      $dataSet->addPoint ( new Point($key."($val)",$val)) ; 
  65.     } 
  66.     $this->chart->setDataSet ( $dataSet ) ;  
  67.     $this->chart->render(); 
  68.    }else
  69.     //cross 
  70.     $dataSet = new XYDataSet(); 
  71.     $this->chart->setTitle($obj->title);//title 
  72.     $arr=array(); 
  73.     $arr=$obj->dataArray; 
  74.     foreach($arr as $key => $val){ 
  75.      $dataSet->addPoint ( new Point($key,$val)) ; 
  76.     } 
  77.     $this->chart->setDataSet($dataSet);  
  78.     $this->chart->render(); 
  79.    } 
  80.   } 
  81.    
  82.  } 
  83.  class kkk{}; 
  84.  $n=new drawPic("4");//it will set 1 or 2 or 3 or 4 
  85.  $k=new kkk(); 
  86.  $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//開源代碼Vevb.com 
  87.  //$k->dataArray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==3 
  88.  //$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) 
  89.  $k->title="The Sheet title"
  90.  $n->draw($k); 
  91.  
  92. ?> 

方法1,2,4為相同的數組,3為線性圖,有可能有兩條線或者多條線的比較,也可以單線,如果要使用中文可能會發現libchart中文亂碼 了,下面找了一個辦法.

我們的應用主源代碼如下:

  1. <?php 
  2.  
  3.    header("content-type:image/png");   
  4.  
  5.    require_once('libchart/classes/libchart.php');  
  6.     
  7.    $chart = new VerticalBarChart( 500 , 250 ) ; // 參數表示需要創建的圖像的寬和高 
  8.  
  9.    $dataSet = new XYDataSet() ; // 實例化一個 XY 軸數據對象 
  10.  
  11.  
  12.      // 為這個對象增加四組數據集合, Point 對象的第一個參數表示 X 軸坐標, 
  13. // 第二個表示 Y 軸坐標 
  14.  
  15.    $str = '二月'
  16.      
  17. $str = mb_convert_encoding($str"html-entities","utf-8" ); 
  18.  
  19.  
  20. $dataSet -> addPoint ( new Point( "Jan 2005" , 273 )) ; 
  21.  
  22.     $dataSet -> addPoint ( new Point( "$str" , 120 )) ; 
  23.  
  24.     $dataSet -> addPoint ( new Point( "March 2005" , 442 )) ; 
  25.  
  26.     $dataSet -> addPoint ( new Point( "April 2005" , 600 )) ; 
  27.  
  28.      // 把這個數據集合傳遞給圖形對象 
  29.  
  30.     $chart -> setDataSet ( $dataSet ) ; 
  31.  
  32.     // 設置圖形的標題,并把它作為一個 png 文件渲染 
  33.  
  34.     $chart -> setTitle ( "統計圖" ) ; 
  35.      
  36.     //$chart -> render ( "demo/generated/demo1.png" ) ;  
  37.     // 這里需要一個路徑和文件名稱  
  38.     //就這么簡單一個像下圖一樣美麗的柱狀圖就出來了 
  39.  
  40.     $chart -> render () ;  
  41.  
  42. ?> 

標紅字的地方是為了解決中文亂碼的.

2、標題亂碼:

默認顯示中文是亂碼,這是由于編碼的原因,做如下修改:首先,更改libchar/libchart/classes/view/chart/Chart.php,找到如下內容,代碼如下:

public function setTitle($title) {           

            $this->plot->setTitle($title);
        }

更改為如下代碼:

  1. public function setTitle($title) { 
  2.         $title = mb_convert_encoding($title"html-entities","utf-8" ); 
  3.  
  4.            $this->plot->setTitle($title); 
  5.  

第三步:就是上面某個博客里講到的:

1、自己寫的使用Libchart 庫生成圖表的php 文件以utf-8編碼保存.

2、找幾個中文字體庫,比如華文行楷、宋體等等,復制到libchart fonts目錄下.

3、修改libchart classes目錄下的text.php 文件.

第47、48行,代碼如下:

  1. $this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf";  
  2. $this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf"

改為:

$this->fontCondensed = dirname(__FILE__) . "/../fonts/你找來的中文字體"; 

$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/你找來的中文字體";

我修改的代碼如下:

  1. public function Text() { 
  2.     $baseDir = dirname(__FILE__) . "/../../../"
  3.    
  4.     // Free low-res fonts based on Bitstream Vera <http://dejavu.sourceforge.net/wiki/> 
  5.    $this->fontCondensed = $baseDir . "fonts/FANGZHENGFANGSONG.ttf"
  6.     $this->fontCondensedBold = $baseDir . "fonts/FANGZHENGFANGSONG.ttf"
  7.    } 

FANGZHENGFANGSONG.ttf 這個文件是我找的方正仿宋簡體字庫,我把中文名字改成那個樣子了,其實不改也是可以的.

主要是基本操作,然后又疑問的地方,按照上面說的做了,應該就是已經可以了,再次謝謝提供這些方法的熱心網友,大家多多交流呀.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 信丰县| 武宁县| 饶阳县| 云和县| 萍乡市| 赤城县| 休宁县| 莎车县| 台中市| 息烽县| 连州市| 济南市| 同江市| 宜宾县| 若羌县| 穆棱市| 嵊州市| 宕昌县| 福清市| 寿宁县| 神农架林区| 凤阳县| 那曲县| 博野县| 青神县| 神木县| 吉木萨尔县| 涡阳县| 宁城县| 长汀县| 竹溪县| 阿瓦提县| 德江县| 当阳市| 西青区| 珠海市| 香港 | 铅山县| 禹州市| 崇左市| 大英县|