php產生圖形(Libchart)執行個體_php執行個體

來源:互聯網
上載者:User

統計圖形就我們會常到的資料圖形了,如果三個數組以圖形顯示或樓盤以圖形走向我們都會要用到圖形,下面我來介紹一個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 這個檔案是我找的方正仿宋簡體字型檔,我把中文名字改成那個樣子了,其實不改也是可以的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.