Mychart.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
然后再在控制器的方法里调用这个类使用:
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
测试代码,稍微显的有些凌乱,大体是这个思路。
在项目使用过程中,大家注意字体的调用的路径。