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

首頁 > 語言 > PHP > 正文

php生成圖形(Libchart)實例

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

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

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

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

  1. <? 
  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 
  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. ?> 

紅色字體為調用。方法1,2,4為相同的數組。3為線性圖,有可能有兩條線或者多條線的比較(也可以單線)。

如果要使用中文可能會發現libchart中文亂碼 了,下面找了一個辦法

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

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

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

2、標題亂碼:

默認顯示中文是亂碼,這是由于編碼的原因,做如下修改:

首先,更改libchar/libchart/classes/view/chart/Chart.php,找到如下內容:

  1. public function setTitle($title) {            
  2.     $this->plot->setTitle($title); 

更改為:

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

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

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"

改為代碼如下:

  1. $this->fontCondensed = dirname(__FILE__) . "/../fonts/你找來的中文字體";  
  2. $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 這個文件是我找的方正仿宋簡體字庫,我把中文名字改成那個樣子了,其實不改也是可以的。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梅河口市| 亳州市| 库尔勒市| 区。| 社会| 定襄县| 安岳县| 奎屯市| 浑源县| 西充县| 昭平县| 华亭县| 宜都市| 麻江县| 上林县| 米脂县| 哈巴河县| 洛隆县| 东辽县| 平乡县| 墨江| 台南县| 海伦市| 贡觉县| 额济纳旗| 台州市| 高雄县| 汽车| 辛集市| 四会市| 无极县| 娱乐| 米泉市| 孝义市| 海林市| 大同县| 陆河县| 桃园县| 吴川市| 综艺| 鄂温|