通过库的形式来使用pChart,因此在 application/libraries/ 下面创建一个文件,命名为 Chart.php,代码如下:
<?php
class Chart {
function Chart() {
include(APPPATH."libraries/pChart/pData.class");
include(APPPATH."libraries/pChart/pChart.class");
}
function draw_line_graph($params) {
$DataSet = new pData;
$DataSet->AddPoint($params['data'],"Serie1");//需要显示的数据
$DataSet->AddPoint($params['date'],"Serie2"); //横坐标的数据
$DataSet->AddSerie("Serie1");
$DataSet->SetAbsciseLabelSerie("Serie2");
$DataSet->SetSerieName("订单总金额","Serie1");
$DataSet->SetYAxisName("RMB"); //纵坐标上显示的文字
$DataSet->SetXAxisName('横坐标:日期'); //横坐标上显示的文字
$DataSet->SetXAxisFormat("date"); //横坐标的数据类型
$Test = new pChart($params['height'],$params['width']); //图表文件的高度和宽度
$Test->setDateFormat($params['date_format']); //横坐标显示的日期格式
$Test->setColorPalette(0,255,0,0);
$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",12); //设置使用的字体及字号
$Test->setGraphArea(60,60,$params['x_area'],$params['y_area']); //图形区域的高度和宽度
$Test->drawGraphArea(252,252,252); //线的颜色
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
$Test->drawGrid(4,TRUE,230,230,230,255);
$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);
$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",8); //设置数据值所用字体及字号
$Test->writeValues($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1"); //输出每个点的数据值
$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",11); //设置使用的字体及字号
$Test->drawLegend(75,65,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",12); //设置使用的字体及字号
$Test->drawTitle(60,22,$params['title'],50,50,50,585);
$imagefile='public/temp/'.$params['filename'].'.png'; //设置生成文件的保存路径
$Test->Render($imagefile); //生成文件
return $imagefile;//返回文件名
}
}
控制器:
function test(){
$this->load->library('chart'); //载入pChart库
$params['data']=array(100,200,150,600,230,150,510); //要显示的数据
for($i=0;$i<7;$i++){
$params['date'][$i]=1250217066+$i*86400; //要显示的日期,注意这里是Unix时间戳,pChart会自动传换成你要的格式
}
$params['title']='销售报表'; //图片标题
$params['date_format']='m月j日';//设置日期格式
$params['filename']='test_image';//文件名
$params['height']=600; //高度
$params['width']=300; //宽度
$params['x_area']=560; //图形区域高度
$params['y_area']=280; //图形区域宽度
$data['chart_image']=$this->chart->draw_line_graph($params);//生成图片
$data['baseurl']=site_url();
$this->load->view('test_view.html',$data);
}
视图:test_view.html
<html>
<head><title></title></head>
<body>
<img src="<?php echo $baseurl.$chart_image;?>" />
</body>
</html>
最终生成的图表是这样的:
至于乱码原因,,,没有找到对应的的中文字体。
// Standard inclusions
include("pChart/pData.class");
include("pChart/pChart.class");
// Dataset definition
$DataSet = new pData;
//图表数据
$DataSet->AddPoint(array(1,4,-3,2,-3,3,2,1,0,7,4),"Serie1");
$DataSet->AddPoint(array(3,3,-4,1,-2,2,1,0,-1,6,3),"Serie2");
$DataSet->AddPoint(array(4,1,2,-1,-4,-2,3,2,1,2,2),"Serie3");
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie();
//数据图例
$DataSet->SetSerieName("Microsoft","Serie1");
$DataSet->SetSerieName("IBM","Serie2");
$DataSet->SetSerieName("Google","Serie3");
// Initialise the graph
$Test = new pChart(700,230);
//设置图表尺寸、样式
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->setGraphArea(50,30,680,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("Fonts/MankSans.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
// Draw the bar graph
//柱状图要使用drawBarGraph()
$Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE,80);
// Finish the graph
//制作图例、标题、字体等属性
$Test->setFontProperties("Fonts/MankSans.ttf",10);
$Test->drawLegend(596,150,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/MankSans.ttf",10);
$Test->drawTitle(50,22,"Example",50,50,50,585);
//生成图表
$imageFile = 'public/temp/test.png'; //设置生成文件的保存路径
$Test->Render($imageFile);
echo '<img src="'.$imageFile.'">';
// Standard inclusions
include("pChart/pData.class");
include("pChart/pChart.class");
// Dataset definition
$DataSet = new pData;
$DataSet->AddPoint(array("Memory","Disk","Network","Slots","CPU"),"Label");
$DataSet->AddPoint(array(6,4,7,4,5),"Serie1");
$DataSet->AddPoint(array(2,3,5,2,4),"Serie2");
$DataSet->AddSerie("Serie1");
$DataSet->AddSerie("Serie2");
$DataSet->SetAbsciseLabelSerie("Label");
$DataSet->SetSerieName("Reference","Serie1");
$DataSet->SetSerieName("Tested computer","Serie2");
// Initialise the graph
$Test = new pChart(400,400);
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawFilledRoundedRectangle(7,7,393,393,5,240,240,240);
$Test->drawRoundedRectangle(5,5,395,395,5,230,230,230);
$Test->setGraphArea(30,30,370,370);
$Test->drawFilledRoundedRectangle(30,30,370,370,5,255,255,255);
$Test->drawRoundedRectangle(30,30,370,370,5,220,220,220);
// Draw the radar graph
//要使用drawRadarAxis()生成雷达效果
$Test->drawRadarAxis($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE,20,120,120,120,230,230,230);
$Test->drawFilledRadar($DataSet->GetData(),$DataSet->GetDataDescription(),50,20);
// Finish the graph
$Test->drawLegend(15,15,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",10);
$Test->drawTitle(0,22,"Example",50,50,50,400);
$imageFile = 'public/temp/test1.png'; //设置生成文件的保存路径
$Test->Render($imageFile);
echo '<img src="'.$imageFile.'">';
}