PHP中用GD绘制饼图
2015-10-23 11:38
573 查看
PHP中用GD绘制饼图,绘制的类见代码:
测试代码如下:
效果图如下:
Class Chart{ private $image; // 定义图像 private $title; // 定义标题 private $ydata; // 定义Y轴数据 private $xdata; // 定义X轴数据 private $color; // 定义条形图颜色 private $bgcolor; // 定义图片背景颜色 private $width; // 定义图片的宽 private $height; // 定义图片的长 /* * 构造函数 * String title 图片标题 * Array xdata 索引数组,X轴数据 * Array ydata 索引数组,数字数组,Y轴数据 */ function __construct($title,$xdata,$ydata) { $this->title = $title; $this->xdata = $xdata; $this->ydata = $ydata; $this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'); } /* * 公有方法,设置条形图的颜色 * Array color 颜色数组,元素取值为'#058DC7'这种形式 */ function setBarColor($color){ $this->color = $color; } /* * 绘制饼图 */ function mkPieChart() { $sum = array_sum($this->ydata); // 获取ydata所有元素之和 $start = 0; // 弧的开始角度 $end = 0; // 弧的结束角度 $pieWidth = 300; // 椭圆的长轴 $pieHeight = 220; // 椭圆的短轴 $space = 40; // 椭圆与小矩形的间距 $margin = 20; // 图片的边距 $recWidth = 20; // 小矩形的宽 $recHeight = 15; // 小矩形的高 $titleHeight = 50; // 标题区域的高 // 图片自适应宽与高 $this->width = $pieWidth + $this->arrayLengthMax($this->xdata)*10*4/3 + $space + $recWidth +$margin; $this->height = (($pieHeight > count($this->xdata)*25 ) ? $pieHeight : count($this->xdata)*25) + $titleHeight; // 椭圆中心的坐标 $cx = $pieWidth/2+$margin; $cy = $pieHeight/2+$titleHeight; $this->image = imagecreatetruecolor($this->width ,$this->height); // 准备画布 $this->bgcolor = imagecolorallocate($this->image,255,255,255); // 图片的背景颜色 imagefill($this->image,0,0,$this->bgcolor); // 填充背景 // 设置条形图的颜色 $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'; // 绘制扇形弧 for($i = 0; $i < 10; $i++) { foreach($this->ydata as $key => $val) { $end += 360*$val/$sum; imagefilledarc($this->image,$cx,$cy-$i,$pieWidth,$pieHeight, $start,$end,$color[$key%count($this->color)],IMG_ARC_PIE); $start = $end; } } // 绘制小矩形及之后文字说明 $x1 = $pieWidth+$space; $y1 = $titleHeight ; foreach($this->ydata 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->xdata[$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 arrayLengthMax($arr) { $length = 0; foreach($arr as $val) { $length = strlen($val) > $length ? strlen($val) : $length; } return $length/3; } // 析构函数 function __destruct(){ imagedestroy($this->image); } }
测试代码如下:
$xdata = array('测试一','测试二','测试三','测试四','测试五','测试六','测试七','测试八','测试九'); $ydata = array(89,90,90,23,35,45,56,23,56); $Img = new Chart($title,$xdata,$ydata); $Img->mkPieChart();
效果图如下:
相关文章推荐
- PHP学习笔记5:代码重用和函数
- 解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
- PHP 性能分析
- ThinkPHP Call to undefined function Think\imagecreate解决
- 在PHP中对查询出得数据库数据进行json编码
- PHP 性能分析第三篇: 性能调优实战
- php获取浏览器内核版本
- PHP 性能分析第三篇: 性能调优实战
- php实现简单的会话
- 谈PHP发展前景和就业解析 -兄弟连IT教育
- php 5.3.10 error日志时间bug
- 对PHP5中__set和__get的理解
- PHP中rawurlencode和urlencode、JS中encodeURI与encodeURIComponent 的区别
- ubuntu下php运行环境配置
- thinkphp ajax 前后端代码展示
- php mysql读写分离
- php vbs加密还原
- php Mcrypt
- 浅谈php7的重大新特性
- PHP开发环境思考