gd动态曲线 php_PHP用GD实现折线图
private $title; //定義標題
private $ydata; //定義Y軸數據
private $xdata; //定義X軸數據
private $seriesName; //定義每個系列數據的名稱
private $color; //定義條形圖顏色
private $bgcolor; //定義圖片背景顏色
private $width; //定義圖片的寬
private $height; //定義圖片的長
/** 構造函數
* String title 圖片標題
* Array xdata 索引數組,X軸數據
* Array ydata 索引數組,數字數組,Y軸數據
* Array series_name 索引數組,數據系列名稱*/
function __construct($title,$xdata,$ydata,$seriesName) {$this->title = $title;$this->xdata = $xdata;$this->ydata = $ydata;$this->seriesName = $seriesName;$this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4');
}/** 公有方法,設置條形圖的顏色
* Array color 顏色數組,元素取值為'#058DC7'這種形式*/
function setBarColor($color){$this->color = $color;
}/** 繪制折線圖*/
public functionpaintLineChart() {$ydataNum = $this->arrayNum($this->ydata); //取得數據分組的個數
$max = $this->arrayMax($this->ydata); //取得所有呈現數據的最大值
$max = ($max > 100)? $max : 100;$multi = $max/100; //如果最大數據是大于100的則進行縮小處理
$barHeightMulti = 2.2; //條形高縮放的比例
$lineWidth = 50;$chartLeft = (1+strlen($max))*12; //設置圖片左邊的margin
$lineY = 250; //初始化條形圖的Y的坐標
// 設置圖片的寬、高
//$this->width = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/1.6;
$margin = 10; //小矩形描述右邊margin
$recWidth = 20; //小矩形的寬
$recHeight = 15; //小矩形的高
$space = 20; //小矩形與條形圖的間距
$tmpWidth = 0;//設置圖片的寬、高
$lineChartWidth = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/1.6;//兩個系列數據以上的加上小矩形的寬
if($ydataNum > 1) {$tmpWidth = $this->arrayLengthMax($this->seriesName)*10*4/3 + $space + $recWidth + + $margin;
}$this->width = $lineChartWidth + $tmpWidth;$this->height = 300;$this->image = imagecreatetruecolor($this->width ,$this->height); //準備畫布
$this->bgcolor = imagecolorallocate($this->image,255,255,255); //圖片的背景顏色
// 設置條形圖的顏色
$color = array();foreach($this->color as $col) {$col = substr($col,1,strlen($col)-1);$red = hexdec(substr($col,0,2));$green = hexdec(substr($col,2,2));$blue = hexdec(substr($col,4,2));$color[] = imagecolorallocate($this->image ,$red, $green, $blue);
}//設置線段的顏色、字體的顏色、字體的路徑
$lineColor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc);$fontColor = imagecolorallocate($this->image, 0x95,0x8f,0x8f);$fontPath = 'font/simsun.ttc';
imagefill($this->image,0,0,$this->bgcolor); //繪畫背景
// 繪畫圖的分短線與左右邊線
for($i = 0; $i < 6; $i++) {
imageline($this->image,$chartLeft-10,$lineY-$barHeightMulti*$max/5/$multi*$i,$lineChartWidth,$lineY-$barHeightMulti*$max/5/$multi*$i,$lineColor);
imagestring($this->image,4,5,$lineY-$barHeightMulti*$max/5/$multi*$i-8,floor($max/5*$i),$fontColor);
}
imageline($this->image,$chartLeft-10,30,$chartLeft-10,$lineY,$lineColor);
imageline($this->image,$lineChartWidth-1,30,$lineChartWidth-1,$lineY,$lineColor);$style = array($lineColor,$lineColor,$lineColor,$lineColor,$lineColor,$this->bgcolor,$this->bgcolor,$this->bgcolor,$this->bgcolor,$this->bgcolor);
imagesetstyle($this->image,$style);//繪制折線圖的分隔線(虛線)
foreach($this->xdata as $key => $val) {$lineX = $chartLeft + 3 + $lineWidth*$key;
imageline($this->image,$lineX,30,$lineX,$lineY,IMG_COLOR_STYLED);
}//繪畫圖的折線
foreach($this->ydata as $key => $val) {if($ydataNum == 1) {//一個系列數據時
if($key == count($this->ydata) - 1 ) break;$lineX = $chartLeft + 3 + $lineWidth*$key;$lineY2 = $lineY-$barHeightMulti*($this->ydata[$key+1])/$multi;//畫折線
if($key == count($this->ydata) - 2) {
imagefilledellipse($this->image,$lineX+$lineWidth,$lineY2,10,10,$color[0]);
}
imageline($this->image,$lineX,$lineY-$barHeightMulti*$val/$multi,$lineX+$lineWidth,$lineY2,$color[0]);
imagefilledellipse($this->image,$lineX,$lineY-$barHeightMulti*$val/$multi,10,10,$color[0]);
}elseif($ydataNum > 1) {//多個系列的數據時
foreach($val as $ckey => $cval) {if($ckey == count($val) - 1 ) break;$lineX = $chartLeft + 3 + $lineWidth*$ckey;$lineY2 = $lineY-$barHeightMulti*($val[$ckey+1])/$multi;//畫折線
if($ckey == count($val) - 2) {
imagefilledellipse($this->image,$lineX+$lineWidth,$lineY2,10,10,$color[$key%count($this->color)]);
}
imageline($this->image,$lineX,$lineY-$barHeightMulti*$cval/$multi,$lineX+$lineWidth,$lineY2,$color[$key%count($this->color)]);
imagefilledellipse($this->image,$lineX,$lineY-$barHeightMulti*$cval/$multi,10,10,$color[$key%count($this->color)]);
}
}
}//繪畫條形圖的x坐標的值
foreach($this->xdata as $key => $val) {$lineX = $chartLeft + $lineWidth*$key + $lineWidth/3 - 20;
imagettftext($this->image,10,-65,$lineX,$lineY+15,$fontColor,$fontPath,$this->xdata[$key]);
}//兩個系列數據以上時繪制小矩形及之后文字說明
if($ydataNum > 1) {$x1 = $lineChartWidth + $space;$y1 = 20;foreach($this->seriesName as $key => $val) {
imagefilledrectangle($this->image,$x1,$y1,$x1+$recWidth,$y1+$recHeight,$color[$key%count($this->color)]);
imagettftext($this->image,10,0,$x1+$recWidth+5,$y1+$recHeight-2,$fontColor,$fontPath,$this->seriesName[$key]);$y1 += $recHeight + 10;
}
}//繪畫標題
$titleStart = ($this->width - 5.5*strlen($this->title))/2;
imagettftext($this->image,11,0,$titleStart,20,$fontColor,$fontPath,$this->title);//輸出圖片
header("Content-Type:image/png");
imagepng ($this->image );
}/** 私有方法,當數組為二元數組時,統計數組的長度
* Array arr 要做統計的數組*/
private function arrayNum($arr) {$num = 0;if(is_array($arr)) {$num++;for($i = 0; $i < count($arr); $i++){if(is_array($arr[$i])) {$num = count($arr);break;
}
}
}return $num;
}/** 私有方法,計算數組的深度
* Array arr 數組*/
private function arrayDepth($arr) {$num = 0;if(is_array($arr)) {$num++;for($i = 0; $i < count($arr); $i++){if(is_array($arr[$i])) {$num += $this->arrayDepth($arr[$i]);break;
}
}
}return $num;
}/** 私有方法,找到一組中的最大值
* Array arr 數字數組*/
private function arrayMax($arr) {$depth = $this->arrayDepth($arr);$max = 0;if($depth == 1) {rsort($arr);$max = $arr[0];
}elseif($depth > 1) {foreach($arr as $val) {if(is_array($val)) {if($this->arrayMax($val) > $max) {$max = $this->arrayMax($val);
}
}else{if($val > $max){$max = $val;
}
}
}
}return $max;
}/** 私有方法,求數組的平均值
* Array arr 數字數組*/
function arrayAver($arr) {$aver = array();foreach($arr as $val) {if(is_array($val)) {$aver = array_merge($aver,$val);
}else{$aver[] = $val;
}
}return array_sum($aver)/count($aver);
}/** 私有方法,求數組中元素長度最大的值
* Array arr 字符串數組,必須是漢字*/
private function arrayLengthMax($arr) {$length = 0;foreach($arr as $val) {$length = strlen($val) > $length ? strlen($val) : $length;
}return $length/3;
}//析構函數
function__destruct(){
imagedestroy($this->image);
}
}
總結
以上是生活随笔為你收集整理的gd动态曲线 php_PHP用GD实现折线图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python模块搜索原则_详解pytho
- 下一篇: php反转数字_PHP将字符串转换成数字