ThinkPHP3.2应用pChart扩展类

pChart是一个开源的图表生成库,主要涉及3个Class:pData.class.php、pDraw.class.php、pImage.class.php,可生成20多种简单或复杂的图表,支持PNG,JPG,GIF通用图片格式。

ThinkPHP3.2导入pChart很简单,只需将pChart文件复制到ThinkPHP的Library就可以了。具体操作:将pChart解压,解压后的文件夹PChart整体复制到ThinkPHP/Library/Org/Util/路径下。

在ThinkPHP控制器加入一下三行import语句,程序就可以调用pChart类。注意:实例化时对象名前加“\"。

import("Org.Util.PChart.class.pDraw");
import("Org.Util.PChart.class.pImage");
import("Org.Util.PChart.class.pData");

$pImage=new \pImage($width,$height,$pData);

pData.class.php、pDraw.class.php、pImage.class.php在PChart\class\路径下。

下面写一个例子:生成一张走势图

定义常量: 

define("WIDTH",512);
define("IMGCHART",COMMON_PATH."img/basic.png");

 

public function index(){
		$this->pimg();
		$this->display();
	}
private function pimg(){
		import("Org.Util.PChart.class.pDraw");
		import("Org.Util.PChart.class.pImage");
		import("Org.Util.PChart.class.pData");
		$mean=5;//移动平均个数
		$priceModel=M('price');
		$last=$priceModel->max('pdate');
		$limit="1";
		list($y,$m,$d)=explode("-",$last);
        $newDate=strval($y-$limit)."-".$m."-".$d;
		$rArr=$priceModel->where("pdate>'{$newDate}'")->order('pdate')->field('pdate,price')->select();
		$retArr=array();
		$meanArr=array();
		$sum=0;
		foreach($rArr as $r){
			$priceInt=intval($r['price']);
			array_push($meanArr,$priceInt);
			$sum+=$priceInt;
			if(count($meanArr)>$mean){
				$sum-=array_shift($meanArr);
				array_push($retArr,array("pdate"=>$r['pdate'],"price"=>strval(floor($sum/$mean))));
			}
		}//计算移动平均值
		$pricesList=array_column($retArr,'price');
		$dateArr=array_column($retArr,'pdate');
		$xDate=array();
		$firstXValue=substr($dateArr[0],2,5);

		$secondXValue=nextMonth($firstXValue);
		$i=0;
		foreach($dateArr as $v){
			$noDay=substr($v,2,5);
			if($noDay==$firstXValue||$noDay==$secondXValue){
				if($i==0){
					array_push($xDate,VOID);
				}
				else{
					($noDay==$firstXValue)?array_push($xDate,$noDay):array_push($xDate,$firstXValue);
				}
			}	
			else{
				$firstXValue=nextMonth($secondXValue);
				$secondXValue=nextMonth($firstXValue);
				$i++;
			}
		}
		$myData=new \pData();
		$myData->addPoints($pricesList,'price');
		$myData->addPoints($xDate,'pdate');
		$myData->setAxisName('price',"Price of Metal");
		$myData->setAbscissa("pdate");
		$myData->setPalette("price",array("R"=>100,"G"=>138,"B"=>179));
		$myData->setAxisColor(0,array("R"=>100,"G"=>90,"B"=>79));
		$myData->setSerieWeight("price",0.5);
		$myPicture=new \pImage(WIDTH,232,$myData);//高度232
		$myPicture->setGraphArea(48,52,WIDTH-25,192);
		$myPicture->setFontProperties(array("FontName"=>COMMON_PATH."img/txzhj-W3.ttf","FontSize"=>12));
		$scaleFormat=array(
			"LabelingMethod"=>LABELING_DIFFERENT,
			"SkippedTickAlpha"=>0,
			"GridR"=>180,
			"GridG"=>180,
			"GridB"=>180,
			"GridTicks"=>0,
			"SkippedAxisAlpha"=>0,
			"DrawXLines"=>FALSE,
		);
		$myPicture->drawScale($scaleFormat);
		$myPicture->setFontProperties(array("FontName"=>COMMON_PATH."img//bgctj2.0.TTF","FontSize"=>14));

		$myPicture->drawSplineChart(array(
			"VoidTicks"=>0,
			"DisplayColor"=>DISPLAY_MANUAL,
		));
		$myPicture->Render(IMGCHART);
	}

运行结果: 

ThinkPHP3.2应用pChart扩展类_第1张图片

你可能感兴趣的:(ThinkPHP3.2应用pChart扩展类)