您好,欢迎访问一九零五行业门户网

PHP中利用GD实现的柱状图

php中利用gd实现的柱状图,自己写的一个画柱状图的类,上代码。
1 title = $title; 20 $this->xdata = $xdata; 21 $this->ydata = $ydata; 22 $this->color = array('#058dc7', '#50b432', '#ed561b', '#dddf00', '#24cbe5', '#64e572', '#ff9655', '#fff263', '#6af9c4'); 23 } 24 25 /* 26 * 公有方法,设置条形图的颜色 27 * array color 颜色数组,元素取值为'#058dc7'这种形式 28 */ 29 function setbarcolor($color){ 30 $this->color = $color; 31 } 32 33 /* 34 * 公有方法,画条形图 35 */ 36 function mkbarchart(){ 37 $ydatanum = $this->arraynum($this->ydata); // 取得数据分组的个数 38 $max = $this->arraymax($this->ydata); // 取得所有呈现数据的最大值 39 $multi = ($max > 100)? $max/100 : 1; // 如果最大数据是大于100的则进行缩小处理,获取 40 $barheightmulti = 2.2; // 条形高缩放的比例 41 $barwidth = (16 - 2*($ydatanum - 1)) > 10 ? (16 - 2*($ydatanum - 1)) : 10; // 条的宽 42 $barspace = 16; // 条之间的间距 43 $chartleft = (1+strlen($max))*12; // 设置图片左边的margin 44 45 $bary = 250; // 初始化条形图的y的坐标 46 // 设置图片的宽、高 47 $this->width = ($ydatanum*$barwidth + $barspace)*count($this->xdata) + $chartleft; 48 $this->height = 300; 49 $this->image = imagecreatetruecolor($this->width ,$this->height); // 准备画布 50 $this->bgcolor = imagecolorallocate($this->image,255,255,255); // 图片的背景颜色 51 52 // 设置条形图的颜色 53 $color = array(); 54 foreach($this->color as $col) { 55 $col = substr($col,1,strlen($col)-1); 56 $red = hexdec(substr($col,0,2)); 57 $green = hexdec(substr($col,2,2)); 58 $blue = hexdec(substr($col,4,2)); 59 $color[] = imagecolorallocate($this->image ,$red, $green, $blue); 60 } 61 62 // 设置线段的颜色、字体的颜色、字体的路径 63 $linecolor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc); 64 $fontcolor = imagecolorallocate($this->image, 0x95,0x8f,0x8f); 65 $fontpath = 'font/simsun.ttc'; 66 67 imagefill($this->image,0,0,$this->bgcolor); // 绘画背景 68 69 // 绘画图的分短线与左右边线 70 for($i = 0; $i image,$chartleft-10,$bary-$barheightmulti*$max/5/$multi*$i,$this->width,$bary-$barheightmulti*$max/5/$multi*$i,$linecolor); 72 imagestring($this->image,4,5,$bary-$barheightmulti*$max/5/$multi*$i-8,floor($max/5*$i),$fontcolor); 73 } 74 imageline($this->image,$chartleft-10,30,$chartleft-10,$bary,$linecolor); 75 imageline($this->image,$this->width-1,30,$this->width-1,$bary,$linecolor); 76 77 // 绘画图的条形 78 foreach($this->ydata as $key => $val) { 79 if($ydatanum == 1) { 80 // 一个系列数据时 81 $barx = $chartleft + 3 + ($barwidth+$barspace)*$key; 82 imagefilledrectangle($this->image,$barx,$bary-$barheightmulti*$val/$multi,$barx+$barwidth,$bary,$color[$key%count($this->color)]); 83 }elseif($ydatanum > 1) { 84 // 多个系列的数据时 85 $cbarspace = $barspace + $barwidth*($ydatanum-1); 86 foreach($val as $ckey => $cval) { 87 $barx = $chartleft + 3 + $barwidth*$key + $ckey*($cbarspace+$barwidth); 88 imagefilledrectangle($this->image,$barx,$bary-$barheightmulti*$cval/$multi,$barx+$barwidth,$bary,$color[$key%count($this->color)]); 89 } 90 } 91 92 } 93 94 // 绘画条形图的x坐标的值 95 foreach($this->xdata as $key => $val) { 96 $barx = $chartleft + ($ydatanum*$barwidth+$barspace)*$key + $ydatanum*$barwidth/3; 97 imagettftext($this->image,10,-45,$barx,$bary+15,$fontcolor,$fontpath,$this->xdata[$key]); 98 } 99 100 // 绘画标题101 $titlestart = ($this->width - 5.5*strlen($this->title))/2;102 imagettftext($this->image,11,0,$titlestart,20,$fontcolor,$fontpath,$this->title);103 104 // 输出图片105 header(content-type:image/png);106 imagepng ( $this->image );107 }108 109 /*110 * 私有方法,当数组为二元数组时,统计数组的长度 111 * array arr 要做统计的数组112 */113 private function arraynum($arr) {114 $num = 0;115 if(is_array($arr)) {116 $num++;117 for($i = 0; $i arraydepth($arr);151 $max = 0;152 if($depth == 1) {153 rsort($arr);154 $max = $arr[0]; 155 }elseif($depth > 1) {156 foreach($arr as $val) {157 if(is_array($val)) {158 if($this->arraymax($val) > $max) {159 $max = $this->arraymax($val);160 }161 }else{ 162 if($val > $max){163 $max = $val;164 }165 } 166 } 167 }168 return $max;169 }170 171 function arrayaver($arr) {172 $aver = array();173 foreach($arr as $val) {174 if(is_array($val)) {175 $aver = array_merge($aver,$val);176 }else{177 $aver[] = $val;178 }179 }180 return array_sum($aver)/count($aver);181 182 }183 // 析构函数184 function __destruct(){185 imagedestroy($this->image);186 }187 }188 ?>
这个类可以实现画一个系列的柱状图和多个系列的柱状图,如下:
一个系列的柱状图
多个系列的柱状图
其它类似信息

推荐信息