php
gd动态曲线 php-凯发ag旗舰厅登录网址下载
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);
}
}
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的gd动态曲线 php_php用gd实现折线图的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: php反转数字_php将字符串转换成数字