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

php怎么绘制饼图?

php怎么绘制饼图?
在php中,可以使用gd绘制饼图。
gd库是php处理图形的扩展库,gd库提供了一系列用来处理图片的api,使用gd库可以处理图片,或者生成图片,也可以给图片加水印。
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();
效果图如下:
gd库的主要用途
在网站上gd库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等。在php处理图像,可使用gd库,而gd库开始时是支持gif的,但由于gif使用了有版权争议的lzw算法,会引起法律问题,于是从 gd 库 1.6 版起所有的 gif 支持都移除了,但是又在 gd 库 2.0.28 版起又加了回来。如果使用二者之间版本的 gd 库时 gif 相关函数不可用。
更多相关知识,请访问 !!
其它类似信息

推荐信息