drawPieImg()函数包含8个参数,$title为饼状图的标题;$dataArr为需要显示的数据数组;$labelArr为对应数据的标签分类数组;$colorArr为对应数据的绘图颜色数组,这4个参数是必须的,对于不同的系统应用传递相应的参数即可。接下来的4个参数,负责设置要生成的饼状图的大小,如果不设置则使用系统默认值。程序按照床底数组数据的大小,从0度开始绘制,方向按照顺时针方向依次绘制对应数据占据的扇面大小。
<?php<BR> //变量定义,画椭圆弧时的角度大小<BR> define("ANGLELENGTH",3);<BR> /**<BR> * 绘制图片<BR> * @param $title 3D图的标题<BR> * @param $dataArr 显示的数据数组<BR> * @param $labelArr 对应数据的标签分类数组<BR> * @param $colorArr 对应绘图颜色的数组<BR> * @param $a 画布的基准宽度<BR> * @param $b 画布的基准高度<BR> * @param $v 3D柱的高度<BR> * @param $font 字体大小<BR> * @return 绘制成功的图片访问路径<BR> */<BR> function drawPieImg($title, $dataArr, $labelArr, $colorArr, $a=250, $b=120, $v=20, $font=10){<BR> $ox = 5+$a;<BR> $oy = 5+$b;<BR> $fw = imagefontwidth($font);<BR> $fh = imagefontheight($font);<BR> $n = count($dataArr);//计算数组长度<BR> $w = 10+$a*2;<BR> $h = 10+$b*2+$v+($fh+2)*$n;<BR> //创建画板<BR> $img = imagecreate($w, $h);<BR> //转RGB为索引色<BR> for($i=0; $i<$n; $i++)<BR> $colorArr[$i] = drawIndexColor($img,$colorArr[$i]);//为图像$img分配颜色<BR> $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);<BR> $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);<BR> //填充背景色<BR> imagefill($img, 0, 0, $clrbk);<BR> //求和<BR> $tot = 0;<BR> for($i=0; $i<$n; $i++)<BR> $tot += $dataArr[$i];<BR> //每个分类的起始角度大小<BR> $sd = 0;<BR> //每个分类所占据的角度大小<BR> $ed = 0;<BR> $ly = 10+$b*2+$v;<BR> for($i=0; $i<$n; $i++){<BR> $sd = $ed;<BR> $ed += $dataArr[$i]/$tot*360;<BR> //画3d扇面<BR> draw3DSector($img, $ox, $oy+20, $a, $b, $v, $sd, $ed, $colorArr[$i]);<BR> //画标签<BR> imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $colorArr[$i]);<BR> imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt);<BR> //中文转码<BR> $str = iconv("GB2312", "UTF-8", $labelArr[$i]);<BR> imagettftext($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "D:/wamp/www/source/font/simhei.ttf", $str.":".$dataArr[$i]."(".(round(10000*($dataArr[$i]/$tot))/100)."%)");<BR> $ly += $fh+2;<BR> }<BR> //绘制图片标题<BR> imagettftext($img, 15, 0, 5, 15, $clrt, "D:/wamp/www/source/font/simhei.ttf", iconv("GB2312", "UTF-8",$title));<BR> //输出图形<BR> header("Content-type: image/png");<BR> //输出生成的图片<BR> $imgFileName = "./".time().".png";<BR> imagepng($img,$imgFileName);<BR> return $imgFileName;<BR> }<BR> /**<BR> * 绘制3d扇面<BR> */<BR> function draw3DSecto<div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>r($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) {<BR> drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);<BR> if($sd<180){<BR> list($red, $green, $blue) = drawDarkColor($img, $clr);<BR> //为图像分配颜色<BR> $clr=imagecolorallocate($img, $red, $green, $blue);<BR> if($ed>180)<BR> $ed = 180;<BR> list($sx, $sy) = getExy($a,$b,$sd);<BR> $sx += $ox;<BR> $sy += $oy;<BR> list($ex, $ey) = getExy($a, $b, $ed);<BR> $ex += $ox;<BR> $ey += $oy;<BR> imageline($img, $sx, $sy, $sx, $sy+$v, $clr);<BR> imageline($img, $ex, $ey, $ex, $ey+$v, $clr);<BR> drawArc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr);<BR> list($sx, $sy) = getExy($a, $b, ($sd+$ed)/2);<BR> $sy += $oy+$v/2;<BR> $sx += $ox;<BR> imagefill($img, $sx, $sy, $clr);<BR> }<BR> }<BR> /**<BR> * 绘制椭圆弧<BR> */<BR> function drawArc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){<BR> $n = ANGLELENGTH >0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;<BR> $d = $sd;<BR> list($x0,$y0) = getExy($a,$b,$d);<BR> for($i=0; $i<$n; $i++){<BR> $d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);<BR> list($x, $y) = getExy($a, $b, $d);<BR> imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);<BR> $x0 = $x;<BR> $y0 = $y;<BR> }<BR> }<BR> /**<BR> * 绘制扇面<BR> */<BR> function drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) {<BR> $n = ANGLELENGTH > 0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;<BR> $d = $sd;<BR> list($x0,$y0) = getExy($a, $b, $d);<BR> imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);<BR> for($i=0; $i<$n; $i++) {<BR> $d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);<BR> list($x, $y) = getExy($a, $b, $d);<BR> imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);<BR> $x0 = $x;<BR> $y0 = $y;<BR> }<BR> imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);<BR> list($x, $y) = getExy($a/2, $b/2, ($d+$sd)/2);<BR> imagefill($img, $x+$ox, $y+$oy, $clr);<BR> }<BR> /**<BR> * 根据$clr颜色获取对应的柱的阴影色<BR> * @param $img 图像<BR> * @param $clr 颜色<BR> * @return rgb颜色数组<BR> */<BR> function drawDarkColor($img,$clr){<BR> $rgb = imagecolorsforindex($img,$clr);<BR> return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);<BR> }<BR> /**<BR> * 求角度$d对应的椭圆上的点坐标<BR> *<BR> * @param $a 横坐标<BR> * @param $b 纵坐标<BR> * @param $d 角度<BR> * @return 对应椭圆点坐标<BR> */<BR> function getExy($a, $b, $d){<BR> $d = deg2rad($d);<BR> return array(round($a*cos($d)), round($b*sin($d)));<BR> }<BR> /**<BR> * 为图像分配RGB索引色<BR> */<BR> function drawIndexColor($img, $clr){<BR> $red = ($clr>>16) & 0xff;<BR> $green = ($clr>>8)& 0xff;<BR> $blue = ($clr) & 0xff;<BR> return imagecolorallocate($img, $red, $green, $blue);<BR> }<BR>//测试示例<BR>$title = "动物园动物种类分布情况";<BR>$dataArr = array(20, 10, 20, 20, 10, 20, 30, 10); //测试数据数组<BR>$labelArr = array("大象", "长颈鹿", "鳄鱼", "鸵鸟", "老虎", "狮子", "猴子", "斑马");//标签<BR>$colorArr = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); //对应颜色数组<BR>$result = drawPieImg($title, $dataArr,$labelArr,$colorArr);<BR>echo "";<BR>?><BR>