CI中使用pChart繪製圖表,支持多種圖表

由於一個codeigniter開發的項目上要用到圖表。FusionChart是flash技術,在某些狀況下可能形成沒法使用(如ipad)。所以不由想起了以前用Pchart作的圖表項目。
可是畢竟是在CI裏,有時候爲了吻合框架的特性,不得不去遷就而修改本身的代碼。
最初個人想法是把Pchart的類文件放在libraries文件裏,而後在controllers控制器裏調用,能夠老是出現各類錯誤。嘗試了不少次都是失敗,難道在CI裏用Pchart這麼難?!!!
因而去百度+谷歌上去找相關的資料。找到了N多篇相同的標題爲「在CI中使用pChart庫」這類的文章,而且內容都95%驚人的類似。看來世界的確是大同了,思想都同樣了(不太喜歡貼別人文章不代表轉載的,人家博主寫篇文章多費勁啊,你這得了便宜還賣乖,粘貼下成本身的了。).
好吧,看下思路,大體是在libraries裏多建立一個類,來調用Pchart的類,進而達到使用目的。不知道大夥有沒有測試,不過我這邊測試的時候總有問題,因而內心很着急,又嘗試了不少次,仍是失敗。因而就想本身寫算了。不過仍是運用了網絡上文章的思路。即把Pchart類包放置於libraries下。可是調用類中的方法確實本身運用以前非CI框架下的方法。
目錄架構相似於這樣:
--demoweb
  --application
    --....
    --libraries
      --pChart
--pChart的衆類文件
      --Mychart.php
    --...
  --system


  我的是把字體都和Pchart的類文件都放在pChart這個目錄裏的。等下看代碼的時候你們就會注意到了。
  下面貼出來Mychart.php(即調用的pchart的類文件),以及控制器裏方法的測試示例代碼:


Mychart.php示例代碼:php





<?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 
/**
* FOR:調用pchart類繪製圖表
*  @author  cheng.yafei
*  @version 1.0
**/
class Mychart {
  
/**
* FOR:畫圖,立體餅圖
*  @param $dataArray 詳細票數數組
*  @param $reportArr 詳細票數對應選項數組
**/
function draw3DPieImage($dataArray, $lableArray)
{
  include(dirname(__FILE__)."/pChart/pData.class");
  include(dirname(__FILE__)."/pChart/pChart.class");
 
  $datanum = $dataArray;
  $dataname = $lableArray;
  
  // Dataset definition 
  $DataSet = new pData;
  $DataSet->AddPoint($datanum,"Serie1");
  $DataSet->AddPoint($dataname,"Serie2");
  $DataSet->AddAllSeries();
  $DataSet->SetAbsciseLabelSerie("Serie2");
  
  $Test = new pChart(420,220);
  $Test->drawFilledRoundedRectangle(7,7,415,213,5,240,240,240);//灰色圓角背景
  $Test->drawRoundedRectangle(5,5,417,215,5,230,230,230);//灰色圓角框
 
  // Draw the pie chart
  $Test->setFontProperties(dirname(__FILE__)."/pChart/yahei.ttf",8);
  $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);
  $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);
  $totalStr = '(共'.array_sum($datanum).'票)';
  $Test->drawTitle(20,20,$totalStr,50,50,50,380);//標題;drawTitle(20,20,參數名,50,50,50,380)   20左邊距,
  
  $rand = rand(100,300);
  $image_name = $rand.'_3dpie.png';
  $image_file_name = dirname(dirname(dirname(__FILE__))).'/actpic/'.$image_name; 
  
  $Test->Render($image_file_name);
//  header("content-type:text/html;charset=utf-8;");
  echo dirname(dirname(dirname(__FILE__))).'/actpic'."<br>";
   echo "<h3>餅圖:</h3><img src='http://localhost/repaction/actpic/".$image_name."'><br>";
}//end function draw3DPieImage 立體餅圖
 
/*******************************
*   FOR:畫圖,分離的餅圖
*  @param $dataArray 詳細票數數組
*  @param $reportArr 詳細票數對應選項數組
********************************/
function drawDepartPieImage($dataArray, $lableArray)
{  
   include(dirname(__FILE__)."/pChart/pData.class");
    include(dirname(__FILE__)."/pChart/pChart.class");
  
   $datanum = $dataArray;
   $dataname = $lableArray;
   
   // Dataset definition 
   $DataSet = new pData;
   $DataSet->AddPoint($datanum,"Serie1");//投票數字
   $DataSet->AddPoint($dataname,"Serie2");//對應文本
   $DataSet->AddAllSeries();
   $DataSet->SetAbsciseLabelSerie("Serie2");
  
   // Initialise the graph
   $Test = new pChart(370,220);//(長,寬)畫布長寬
   $Test->drawFilledRoundedRectangle(7,7,353,213,5,240,240,240);//灰色圓角背景
   $Test->drawRoundedRectangle(5,5,355,215,5,230,230,230);//灰色圓角框
   // Draw the pie chart
   $Test->setFontProperties(dirname(__FILE__)."/pChart/yahei.ttf",8);
   $Test->AntialiasQuality = 0;
   $Test->drawFlatPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),120,100,60,PIE_PERCENTAGE,10);//畫餅圖
   $Test->drawPieLegend(230,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);//畫選項圖
   $totalStr = '(共'.array_sum($datanum).'票)';
   $Test->drawTitle(20,20,$totalStr,50,50,50,380);//標題;drawTitle(20,20,參數名,50,50,50,380)   20左邊距,  
   
   $rand = rand(100,300);
   $image_name = $rand.'_departpie.png';
   $image_file_name = dirname(dirname(dirname(__FILE__))).'/actpic/'.$image_name; 
   
   $Test->Render($image_file_name);
   
   echo "<h3>離散餅圖:</h3><img src='http://localhost/repaction/actpic/".$image_name."'><br>";
}// end drawDepartPieImage
/*******************************
*  FOR:畫圖,柱狀圖
*  @param $dataArray 詳細票數數組
*  @param $reportArr 詳細票數對應選項數組
********************************/
function drawBarImage($dataArray, $lableArray)
{
   include(dirname(__FILE__)."/pChart/pData.class");
    include(dirname(__FILE__)."/pChart/pChart.class");
  
   // Dataset definition    
   $DataSet = new pData;
   foreach($dataArray as $k=>$v){
   $DataSet->AddPoint($v, "Serie".$k);
   $DataSet->SetSerieName($lableArray[$k], "Serie".$k);
   }
   $DataSet->AddAllSeries();
   $DataSet->SetAbsciseLabelSerie();
  
   // Initialise the graph
   $Test = new pChart(700,230);
   $Test->setFontProperties(dirname(__FILE__)."/pChart/yahei.ttf",8);
   $Test->setGraphArea(50,30,550,200);
   $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
   $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
   $Test->drawGraphArea(255,255,255,TRUE);
   $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
   $Test->drawGrid(4,TRUE,230,230,230,50);
  
   // Draw the 0 line
   $Test->setFontProperties(dirname(__FILE__)."/pChart/yahei.ttf",6);
   $Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  
   // Draw the bar graph
   $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  
   // Finish the graph
   $Test->setFontProperties(dirname(__FILE__)."/pChart/yahei.ttf",8);
//   $Test->drawLegend(596,150,$DataSet->GetDataDescription(),255,255,255);
   $Test->drawLegend(580,30,$DataSet->GetDataDescription(),255,255,255);//(左邊距,上邊距,描述....)
   $Test->setFontProperties(dirname(__FILE__)."/pChart/yahei.ttf",10);
   $Test->drawTitle(50,22,"投票",50,50,50,585);
     
     $rand = rand(100,300);
   $image_name = $rand.'_zhuzhuang.png';
   $image_file_name = dirname(dirname(dirname(__FILE__))).'/actpic/'.$image_name; 
   
   $Test->Render($image_file_name);
   
   echo "<h3>離散餅圖:</h3><img src='http://localhost/repaction/actpic/".$image_name."'><br>";
    
   
}//end function drawBarImage 柱狀圖
 
}//END class

而後再在控制器的方法裏調用這個類使用:html

function test(){
     $this->load->library('mychart'); //載入pChart庫
     
 $dataArray = array('54','95','158','241','19');
 $lableArray = array('成龍','施瓦辛格','龍格爾','史泰龍','李連杰');
 
 $this->mychart->draw3DPieImage($dataArray,$lableArray);
   
 }//end function
 
 function test2(){
     $this->load->library('mychart'); //載入pChart庫
     
 $dataArray = array('555','95','158','241','19');
 $lableArray = array('黃家駒','平克弗洛伊德','譚詠麟','張學友','劉德華');
 
 $this->mychart->drawDepartPieImage($dataArray,$lableArray);
   
 }//end function
 function test3(){
     $this->load->library('mychart'); //載入pChart庫
     
 $dataArray = array('369','95','158','241','19');
 $lableArray = array('黃家駒','平克弗洛伊德','譚詠麟','張學友','劉德華');
 
 $this->mychart->drawBarImage($dataArray,$lableArray);
   
 }//end function

測試代碼,稍微顯的有些凌亂,大致是這個思路。web

 

在項目使用過程當中,你們注意字體的調用的路徑。數組

相關文章
相關標籤/搜索