下面是一个二维统计图展示:
$xy2=get_xy_factors((($to-$from)/2)+$from);
$x2=floor($rx+($xy2[0]*$r/3));
$y2=floor($ry+($xy2[1]*$r/3));
imagefilltoborder($id,$x2,$y2,$color,$color);
上面【$r/3】什么意思,为什么要用椭圆的宽度除以3呢,在这之上两个不都是除以2的吗???<?php
function roundoff($v)
//对$v取整,四舍五入
{
if($v-floor($v)>=0.5)
{
return(ceil($v));//对$v取整,四舍五入
}
else
{
return(floor($v));//对$v取整,去掉$v后面的小数
}
}
function get_xy_factors($degrees)
//根据角度$degrees获得拼图圆周上点的坐标
{
$x=cos((3.1415926*$degrees)/doubleval(180));
$y=sin((3.1415926*$degrees)/doubleval(180));
return (array($x,$y));
}
function draw_pie($id,$rx,$ry,$r,$from,$to,$color)
//绘制拼图
{
imagearc($id,$rx,$ry,$r,$r,$from,$to,$color);

$axy2=get_xy_factors($from);
$ax2=floor($rx+($axy2[0]*$r/2)); //求圆周上起始点的横坐标
$ay2=floor($ry+($axy2[1]*$r/2)); //求圆周上起始点的纵坐标
imageline($id,$rx,$ry,$ax2,$ay2,$color); //圆周上起始点到圆心的直线

$bxy2=get_xy_factors($to); 
$bx2=ceil($rx+($bxy2[0]*$r/2)); //求圆周上终点的横坐标 
$by2=ceil($ry+($bxy2[1]*$r/2)); //求圆周上终点的纵坐标
imageline($id,$rx,$ry,$bx2,$by2,$color); //圆周上终点到圆心的直线

$xy2=get_xy_factors((($to-$from)/2)+$from);
$x2=floor($rx+($xy2[0]*$r/3));
$y2=floor($ry+($xy2[1]*$r/3));
imagefilltoborder($id,$x2,$y2,$color,$color);

}

function draw_legends($id,$lex,$ley,$ler,$les,$color,$balack)
//绘制拼图说明信息
{
imagefilledrectangle($id,$lex,$ley,$lex+$ler,$ley+$ler,$color);
imagerectangle($id,$lex,$ley,$lex+$ler,$ley+$ler,$balack);
imagestring($id,3,$lex+$ler+5,$ley,$les,$color);
}

function display($a,$num)
{
$id=imagecreate(600,400);
$angle=30;
$rx=120;
$ry=130;

$r=120;
$color=imagecolorallocate($id,255,255,255);
$balack=imagecolorallocate($id,0,0,0);

$colors=array(imagecolorallocate($id,255,0,0),
imagecolorallocate($id,0,255,0),
imagecolorallocate($id,0,0,255),
imagecolorallocate($id,0,255,255),
imagecolorallocate($id,255,0,255));

/**/
$les=array("choice 1","choice 2","choice 3","choice 4","choice 5");
/**/

$lex=210;
$ley=50;
$ler=13;

$sum=0;
for($i=0;$i < $num;$i++)
$sum=$sum+$a[$i];

$from=$angle;
$to=$from+roundoff(360*$a[0]/$sum);

imagerectangle($id,0,0,440,240,$colors[0]);
imagestring($id,5,180,10,"my test",$colors[0]);
for($i=0;$i < $num;$i++)
{
$les[$i].=sprintf("<%s",$a[$i]);
$les[$i].=sprintf("(%.2f%%)",($a[$i]*100/doubleval($sum)));
draw_legends($id,$lex,$ley,$ler,$les[$i],$colors[$i],$balack);
$ley=$ley+20;
}

for($i=0;$i < $num;$i++)
{
draw_pie($id,$rx,$ry,$r,$from,$to,$colors[$i]);
$from=$to+1;
$to=$from+roundoff(360*$a[$i+1]/$sum);
}

$tot="TOTAL:";
$tot.=sprintf("%s",$sum);
imagestring($id,5,210,200,$tot,$colors[0]);

imagepng($id);
imagedestroy($id);
}
header("content-tye: image/png");
$a[0]=15;
$a[1]=6;
$a[2]=8;
$a[3]=11;
$a[4]=9; $num=5;

display($a,$num);
?>