|
本帖最后由 终结者 于 2012-7-12 09:35 编辑
因为一个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
复制代码
然后再在控制器的方法里调用这个类使用:
PHP复制代码
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
复制代码
测试代码,稍显凌乱。需要的可以测试下。知识就是共享的。如果有问题的话可以在下面跟帖提问。
请注意使用的时候,一定注意写对调用字体文件的路径。
|
评分
-
查看全部评分
|