基本上常用的图形都有了,出来的图片也还不错。不过拿来用,还是要改几个地方,主要是修改pchart类的Render方法,其默认是直接生成图片文件,将这个方法最后一行,imagepng函数第二个参数去掉,然后在这个函数前加一句:
header(“Content-type: image/png”);
这样就直接输出图片到浏览器,而不是存为文件了。
其次是中文的支持,因为其使用的默认字体库是tahoma.ttf,这个不支持中文,所以还要找个支持中文的字体,比如simhei.ttf,你的系统下字体目录里肯定有的,直接拷贝到pchart项目目录的Fonts目录里就可以了。
根据其提供的example,改写了几个图形,(thinkphp封装)供参考:
/*
饼图:3D效果
输入参数:get[datas],逗号分隔的数据
get[labels],逗号分隔的标题
get[title],标题
*/
public function pie3dgram(){
$datas = explode(‘,’,$_GET[‘datas’]);
$labels = explode(‘,’,$_GET[‘labels’]);
Vendor(‘Chart.pData’);
Vendor(‘Chart.pChart’);
$DataSet = new pData;
$DataSet->AddPoint($datas,”Serie1″);
$DataSet->AddPoint($labels,”Serie2″);
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie(“Serie2”);
// Initialise the graph
$Test = new pChart(420*2,250*2);
$Test->drawFilledRoundedRectangle(7,7,413*2,243*2,5,240,240,240);
$Test->drawRoundedRectangle(5,5,415*2,245*2,5,230,230,230);
$Test->createColorGradientPalette(195,204,56,223,110,41,5);
// Draw the pie chart
$Test->setFontProperties(“Public/font/simhei.ttf”,8);
$Test->AntialiasQuality = 0;
$Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),180*2,130*2,110*2,PIE_PERCENTAGE_LABEL,FALSE,50,20,5);
$Test->drawPieLegend(330*2,15*2,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);
// Write the title
$Test->setFontProperties(“Public/font/simhei.ttf”,10);
$Test->drawTitle(10*2,20*2,$_GET[‘title’],100,100,100);
$Test->Render(“”);
}
/*
矩形图:2D效果
输入参数:get[datas],逗号分隔的数据
get[labels],逗号分隔的标题
get[title],标题
*/
public function histogram(){
$datas = explode(‘,’,$_GET[‘datas’]);
$labels = explode(‘,’,$_GET[‘labels’]);
Vendor(‘Chart.pData’);
Vendor(‘Chart.pChart’);
$DataSet = new pData;
foreach($datas as $k => $v){
$DataSet->AddPoint(array($v),”Serie”.($k+1));
}
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie();
foreach($datas as $k => $v){
$DataSet->SetSerieName($labels[$k],”Serie”.($k+1));
}
$Test = new pChart(700,230);
$Test->setFontProperties(“Public/font/simhei.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(“Public/font/simhei.ttf”,6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
// Draw the bar graph
$Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE,80);
// Finish the graph
$Test->setFontProperties(“Public/font/simhei.ttf”,8);
$Test->drawLegend(570,30,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties(“Public/font/simhei.ttf”,10);
$Test->drawTitle(50,22,$_GET[‘title’],50,50,50,585);
$Test->Render(“”);
}
/*
线形图:
输入参数:get[datas],逗号分隔的数据
get[labels],逗号分隔的标题
get[title],标题
*/
public function linegram(){
$datas = explode(‘,’,$_GET[‘datas’]);
$labels = explode(‘,’,$_GET[‘labels’]);
Vendor(‘Chart.pData’);
Vendor(‘Chart.pChart’);
$max = 0;
$min = 0;
foreach($datas as $v){
$max = max($max,$v);
$min = min($min,$v);
}
$DataSet = new pData;
$DataSet->AddPoint($datas,”Serie1″);
$DataSet->AddPoint($labels,”Serie4″);
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie(“Serie4”);
$DataSet->SetSerieName(“”,”Serie1″);
// Initialise the graph
$Test = new pChart(650,230);
$Test->reportWarnings(“GD”);
$Test->setFixedScale($min-1,$max+1,5);
$Test->setFontProperties(“Public/font/simhei.ttf”,8);
$Test->setGraphArea(65,30,570,185);
$Test->drawFilledRoundedRectangle(7,7,643,223,5,240,240,240);
$Test->drawRoundedRectangle(5,5,645,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,1);
$Test->drawGrid(4,TRUE,230,230,230,50);
$DataSet->RemoveSerie(“Serie4”);
$DataSet->RemoveSerie(“Serie3”);
$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
// Draw the line graph
$Test->setLineStyle(1,6);
$DataSet->RemoveAllSeries();
// Write values on Serie3
$Test->setFontProperties(“Public/font/simhei.ttf”,8);
$Test->writeValues($DataSet->GetData(),$DataSet->GetDataDescription(),”Serie1″);
$Test->setFontProperties(“Public/font/simhei.ttf”,10);
$Test->drawTitle(60,22,$_GET[‘title’],50,50,50,585);
$Test->Render(“”);
}