PChart使用经验

本文介绍了如何利用PChart库来创建直观的图形展示测试结果。首先从数据库获取数据,然后通过简单步骤生成PChart图表,适合对图表展示有兴趣的开发者参考。

        前几天写了个web,来统计测试结果,因为数字没有图形来的直观,所以网上搜索了下发现PChart是个好东西,易于上手也比较好看。现在的PCahrt已经到2.1.3了,大概10多个class类。下面几个步骤来生成pchart图表,有兴趣的朋友一起研究研究。

1. 先写个函数来fetch data 从数据库。

$q=$_GET["q"];
my_chart($q);
function my_chart($TestLab_Name)
{
   include("../MySql_Class.php");
   $result=mysql_query("select OS_Build_Number,
       Sum(case when TestCase_Status='Pass' then 1 else 0 end)as TestCase_Pass,
       Sum(case when TestCase_Status='Fail' then 1 else 0 end)as TestCase_Fail
       from qtp_report.hpdm_automation_testresult
       where TestLab_Name='$TestLab_Name' group by OS_Build_Number");
   while($row = mysql_fetch_array($result))
   {
       /* Push the results of the query in an array */
   	   $OS_Build_Number[]=$row['OS_Build_Number'];
       $TestCase_Pass[]=$row['TestCase_Pass'];
       $TestCase_Fail[]=$row['TestCase_Fail'];
   }
    makeachart($OS_Build_Number,$TestCase_Pass,$TestCase_Fail,$TestLab_Name);
}
2.在把参数传递给PChart的生成函数:
function makeachart($OS_Build_Number,$TestCase_Pass,$TestCase_Fail,$TestLab_Name)
{    
/* CAT:Stacked chart */ 

 /* pChart library inclusions */ 
 include("../class/pData.class.php"); 
 include("../class/pDraw.class.php"); 
 include("../class/pImage.class.php"); 

 /* Create and populate the pData object */ 
 $MyData = new pData();   
 $MyData->addPoints($TestCase_Pass,"Pass"); 
 $MyData->addPoints($TestCase_Fail,"Fail"); 
 $MyData->setSerieTicks("Probe 2",4); 
 $MyData->setAxisName(0,"TestCaseStatus"); 
 $MyData->addPoints($OS_Build_Number,"Labels"); 
 $MyData->setSerieDescription("Labels","Months"); 
 $MyData->setAbscissa("Labels"); 

 /* Create the pChart object */ 
 $myPicture = new pImage(700,230,$MyData); 

 /* Draw the background */ 
 $Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107); 
 $myPicture->drawFilledRectangle(0,0,700,230,$Settings); 

 /* Overlay with a gradient */ 
 $Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "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"=>80)); 

 /* Add a border to the picture */ 
 $myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0)); 
  
 /* Write the picture title */  
 $myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6)); 
 $myPicture->drawText(10,13,"drawStackedBarChart() - draw a stacked bar chart",array("R"=>255,"G"=>255,"B"=>255)); 

 /* Write the chart title */  
 $myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11)); 
 $myPicture->drawText(250,55,$TestLab_Name,array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); 

 /* Draw the scale and the 1st chart */ 
 $myPicture->setGraphArea(60,60,450,190); 
 $myPicture->drawFilledRectangle(60,60,450,190,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10)); 
 $myPicture->drawScale(array("DrawSubTicks"=>TRUE,"Mode"=>SCALE_MODE_ADDALL)); 
 $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/pf_arma_five.ttf","FontSize"=>6)); 
 $myPicture->drawStackedBarChart(array("DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO,"Rounded"=>TRUE,"Surrounding"=>60)); 
 $myPicture->setShadow(FALSE); 

 /* Draw the scale and the 2nd chart */ 
 $myPicture->setGraphArea(500,60,670,190); 
 $myPicture->drawFilledRectangle(500,60,670,190,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10)); 
 $myPicture->drawScale(array("Pos"=>SCALE_POS_TOPBOTTOM,"Mode"=>SCALE_MODE_ADDALL,"DrawSubTicks"=>TRUE)); 
 $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); 
 $myPicture->drawStackedBarChart(); 
 $myPicture->setShadow(FALSE); 

 /* Write the chart legend */ 
 $myPicture->drawLegend(510,205,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL)); 

 /* Render the picture (choose the best way) */ 
 $myPicture->autoOutput("pictures/example.drawStackedBarChart.png"); 
}
?>  

3.生成图表


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值