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
在項目使用過程當中,你們注意字體的調用的路徑。數組