«
php做饼图的函数

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  我是个新手,当时学php纯粹是为了应付领导哦,让我们做统计图,我本来是学asp的,突然改行很不适应,不过php功能的确很强大呀,呵呵,我都离不了他了,这不,饼图,柱型图都搞定了,可是还有很多问题我不会,还希望大家多指教呀,说了好多废话了,我还以为是在写信那<p>  我的联系方式是:</p><p>  email:qingdaohb@yeah.net</p><p>  http://qdlover.yeah.net</p><p>  老板催了,没办法,饼图函数出炉了,大家快抢呀</p><code>########<br />bimage.php3<br />########<br />/*<br />函数说明<br />$chartdata:数据,是数组元素<br />$chartfont:字号<br />$chartdiameter:决定饼的大小(要看你饿不饿了,呵呵)<br />$chartlabel:标题,也是数组元素<br />$colorslice:颜色数组,例如$tmp=array255,255,255);$colorslic=array($tmp);<br />$colorborder:边框颜色,数组<br />$colortext :文本颜色,数组<br />$colorbody:背景颜色,数组<br />$file:输出图片文件名*/<br />function bimage($chartdata,<br />$chartfont,<br />$chartdiameter ,<br />$chartlabel ,<br />$colorslice,<br />$colorbody ,<br />$colorborder,<br />$colortext ,<br />$file<br />)<br />{<br />$chartdiameter=150;<br />$chartfontheight=imagefontheight($chartfont);<br />$d1=10;$d2=20;$d3=30;$d4=40;$d5=50;<br />$chartdata=array($d1,$d2,$d3,$d4,$d5);<br />$chartlabel=array("D1","D2","D3","D4","D5");<br />$chartwidth=$chartdiameter+20;<br />$chartheight=$chartdiameter+20+(($chartfontheight+2)*count($chartdata));<br />header("content-type:image/gif");<br />$image=imagecreate($chartwidth,$chartheight);<br />$colorbody =imagecolorallocate ($image,$colorbody[0],$colorbody[1],$colorbody[2]);<br />$colortext =imagecolorallocate ($image,$colortext[0],$colortext[1],$colortext[2]);<br />$colorborder =imagecolorallocate ($image,$colorborder[0],$colorborder[1],$colorborder[2]);<br />for ($i=0;$i {<br />$t=imagecolorallocate($image,$colorslice[$i][0],$colorslice[$i][1],$colorslice[$i][2]);<br />$colorslice[$i]=$t;<br />}<br />for($i=0;$i {<br />$charttotal+=$chartdata[$i];<br />}<br />$chartcenterx=$chartdiameter/2+10;<br />$chartcentery=$chartdiameter/2+10;<br />$degrees=0;<br />for($i=0;$i {<br />$startdegrees=round($degrees);<br />$degrees+=(($chartdata[$i]/$charttotal)*360);<br />$enddegrees=round($degrees);<br />$currentcolor=$colorslice[$i%(count($colorslice))];<br />imagearc($image ,<br />$chartcenterx,<br />$chartcentery,<br />$chartdiameter,<br />$chartdiameter,<br />$startdegrees,<br />$enddegrees,<br />$currentcolor);<br />list($arcx,$arcy)=circle_point($startdegrees,$chartdiameter);<br />imageline($image,<br />$chartcenterx,<br />$chartcentery,<br />floor($chartcenterx+$arcx),<br />floor($chartcentery+$arcy),<br />$currentcolor );<br />list($arcx,$arcy)=circle_point($enddegrees,$chartdiameter);<br />imageline($image,<br />$chartcenterx,<br />$chartcentery,<br />ceil($chartcenterx+$arcx),<br />ceil($chartcentery +$arcy),<br />$currentcolor);<br />$midpoint=round((($enddegrees-$startdegrees)/2)+$startdegrees);<br />list($arcx,$arcy)= circle_point ( $midpoint, $chartdiameter/2);<br />imagefilltoborder($image,<br />floor($chartcenterx+$arcx),<br />floor($chartcentery+$arcy),<br />$currentcolor,<br />$currentcolor);<br />}<br />imagearc($image,<br />$chartcenterx,<br />$chartcentery,<br />$chartdiameter,<br />$chartdiameter,<br />0,360,<br />$colorborder);<br />imagefilltoborder ($image,<br />floor($chartcenterx +( $chartdiameter /2)+2),<br />$chartcentery ,<br />$colorborder,<br />$colorborder );<br />for ($i=0;$i {<br />$currentcolor=$colorslice[$i%(count($colorslice))];<br />$liney=$chartdiameter+20+($i*($chartfontheight+2));<br />imagerectangle ($image,<br />10,<br />$liney,<br />20+$chartfontheight,<br />$liney+$chartfontheight,<br />$colorbody);<br />imagefilltoborder($image,<br />12,<br />$liney+2,<br />$colorbody,<br />$currentcolor);<br />imagestring($image,<br />$chartfont,<br />40+$chartfontheight,<br />$liney,<br />"$chartlabel[$i]:$chartdata[$i]",<br />$colortext);<br />}<br />imagegif ($image,$file);<br />}<br />function radians($degrees)<br />{<br />return($degrees*(pi()/180.0));<br />}<br />function circle_point($degrees,$diameter)<br />{<br />$x=cos(radians($degrees))*($diameter/2);<br />$y=sin(radians($degrees))*($diameter/2);<br />return (array($x,$y));<br />}<br />?><br />###########<br />这是一个调用的例子<br />###########<br />include("bfunc.php3");<br />$chartdiameter=250;<br />$chartfont=5;<br />$d1=10;$d2=20;$d3=30;$d4=40;$d5=50;<br />$chartdata=array($d1,$d2,$d3,$d4,$d5);<br />$chartlabel=array("D1","D2","D3","D4","D5");<br />$colorbody=array(0xff,0xff,0xff);<br />$colorborder=array(0x00,0x00,0x00);<br />$colortext=array(0xff,0xff,0xff);<br />$color1=array(0xff,0x00,0x00);<br />$color2=array(0x00,0xff,0x00);<br />$color3=array(0x00,0x00,0xff);<br />$color4=array(0xff,0xff,0x00);<br />$color5=array(0xff,0x00,0xff);<br />$colorslice=array($color1 ,$color2,$color3,$color4,$color5);<br />$file="tj.gif"<br />bimage($chartdata,<br />$chartfont,<br />$chartdiameter ,<br />$chartlabel ,<br />$colorslice,<br />$colorbody ,<br />$colorborder,<br />$colortext ,<br />$file )<br />?></code></p>