经常和数据打交道的避免不了生成各种图形,pChart是一个开源的图表生成库,可以生成几十种简单或者复杂或者看不懂的图标,这里的代码是生成饼形图,使用该程序PHP需要开启GD服务,另外pChart支持中文的话,修改代码中的:$Test->setFontProperties(“Fonts/simkai.ttf”,8); 并将要simkai.ttf(楷体)文件拷贝到 pChart\Fonts下。
pchart饼图实例
include("./include/pChart/class/pData.class.php");
include("./include/pChart/class/pDraw.class.php");
include("./include/pChart/class/pPie.class.php");
include("./include/pChart/class/pImage.class.php");
/* 创建一个pData对象 */
$MyData = new pData();
$MyData->addPoints(array(220,273,32,184),"ScoreA"); // 要显示的数据值
// $MyData->setSerieDescription("ScoreA","Application A"); // 设置描述??
/* 图例 */
$MyData->addPoints(array("分机错误","分机接通","分机未接(<17s)","分机未接(>17s)"),"Labels"); // 数据范围
$MyData->setAbscissa("Labels"); // 定义标签
/* 创建pChart对象,设置图形大小 */
$myPicture = new pImage(700,230,$MyData);
/* 绘制图片背景 */
$Settings = array("R"=>173, "G"=>152, "B"=>217, "Dash"=>1, "DashR"=>193, "DashG"=>172, "DashB"=>237);
$myPicture->drawFilledRectangle(0,0,700,230,$Settings);
/* 绘制背景渐变叠加 */
$Settings = array("StartR"=>209, "StartG"=>150, "StartB"=>231, "EndR"=>111, "EndG"=>3, "EndB"=>138, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
/* 添加边框图片 */
$myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));
/* 图片标题 */
$myPicture->setFontProperties(array("FontName"=>"./include/pChart/fonts/simkai.ttf","FontSize"=>12));
$myPicture->drawText(10,20,"通话统计",array("R"=>255,"G"=>255,"B"=>255));
/* 设置默认字体属性 */
$myPicture->setFontProperties(array("FontName"=>"./include/pChart/fonts/simkai.ttf","FontSize"=>12,"R"=>255,"G"=>255,"B"=>255));
/* 启用阴影,设置图形的阴影,没实际作用 只是为了美观 */
$myPicture->setShadow(TRUE,array("X"=>2,"Y"=>2,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>50));
/* 创建对象 */
$PieChart = new pPie($myPicture,$MyData);
/* 创建一个简单的饼图,即第一个饼图 */
// $PieChart->draw2DPie(120,125,array("SecondPass"=>FALSE));
/* 创建一个AA的饼图,即中间的饼图,带数据标示 */
$PieChart->draw2DPie(220,125,array("DrawLabels"=>TRUE,"LabelStacked"=>TRUE,"Border"=>TRUE));
/* 创建一个分离的饼图,即第三个饼图 */
$PieChart->draw2DPie(550,125,array("WriteValues"=>PIE_VALUE_PERCENTAGE,"DataGapAngle"=>10,"DataGapRadius"=>6,"Border"=>TRUE,"BorderR"=>255,"BorderG"=>255,"BorderB"=>255));
/* 创建文字图例 */
$myPicture->setFontProperties(array("FontName"=>"./include/pChart/fonts/simkai.ttf","FontSize"=>9));
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>20));
// $myPicture->drawText(120,200,"Single AA pass",array("DrawBox"=>TRUE,"BoxRounded"=>TRUE,"R"=>0,"G"=>0,"B"=>0,"Align"=>TEXT_ALIGN_TOPMIDDLE));
// $myPicture->drawText(440,200,"Extended AA pass / Splitted",array("DrawBox"=>TRUE,"BoxRounded"=>TRUE,"R"=>0,"G"=>0,"B"=>0,"Align"=>TEXT_ALIGN_TOPMIDDLE));
/* 创建图形图例 */
$myPicture->setFontProperties(array("FontName"=>"./include/pChart/fonts/simkai.ttf","FontSize"=>9,"R"=>255,"G"=>255,"B"=>255));
$PieChart->drawPieLegend(360,8,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
/* 渲染图片 */
$myPicture->autoOutput("./templates_c/example.draw2DPie.png");