Jednoduchý koláčový 3D graf
Ďalší spôsob ako generovať koláčový 3D graf z PHP.
<? Header("Content-Type:image/gif"); ///////////////////////////////////////////////////////////////////////////////////////// // // Name: Pollbiscuit // // Description: This snippet is used for drawing biscuit graph. // // Array $Biscuit_val contains the Biscuit_values of percentage added up to 100. // Array $Biscuit_content refer to the answers of the very question. // Testing: // test_biscuit.php3: // <html><body> // <h1>Poll result!</h1> // $Biscuit_val[]=99.96;$Biscuit_content[]="I donn't know!"; // $Biscuit_val[]=0.01; $Biscuit_content[]="I think so"; // $Biscuit_val[]=0.01; $Biscuit_content[]="NO,just a joke"; // $Biscuit_val[]=0.01; $Biscuit_content[]="Like it"; // <Img src=PollBiscuit.php3?<script language="php"> for($i=0;$i<count($Biscuit_val);$i++) echo "Biscuit_val[$i]=$Biscuit_val[$i]"."&"."content[$i]=\ // ".urlencode($Biscuit_content[$i])."&"; </script> border=0 > // </body></html> // Changelog: // Date Author Name Description // ----------- ----------- ------------------------------------------------ // 3/25/2000 Chriss lee(chriss.lee@sohu.com) Created ///////////////////////////////////////////////////////////////////////////////////////// $num=count($Biscuit_val); //////////////////////// Canvas define ///////////////////////////////////////// $img_w=300; //image width $img_h=130; //image height $bottom=10; //bottom for rectangle $left=120; //left for rectangle $a=100; //long axis of ellipse $b=50; //short axis of ellipse $distance=10; //distance between rectangles $id=ImageCreate($img_w,$img_h); //Create canvas ///////////////////////// Color define ///////////////////////////////////////// $black=ImageColorAllocate($id,0,0,0); $white=ImageColorAllocate($id,255,255,255); $color[]=ImageColorAllocate($id,0,255,0); $color[]=ImageColorAllocate($id,255,0,0); $color[]=ImageColorAllocate($id,0,0,255); $color[]=ImageColorAllocate($id,255,255,0); $color[]=Imagecolorallocate($id,0,255,255); $color[]=Imagecolorallocate($id,255,0,255); $color[]=Imagecolorallocate($id,125,125,125); $color[]=Imagecolorallocate($id,0,0,125); $color[]=Imagecolorallocate($id,125,0,0); $color[]=Imagecolorallocate($id,0,0,125); $color[]=Imagecolorallocate($id,125,125,0); $color[]=Imagecolorallocate($id,0,125,125); $red=$color[1]; $green=$color[0]; $blue=$color[2]; $yellow=$color[3]; $trans=ImageColorTransparent($id,$white); //transparent color define ImageFill($id,0,0,$white); //Fill transparent color $img_center_x=50; //x coordinates of image center $img_center_y=$img_h/2; //y coordinates of image center ////////////////////////// Draw ellipse ////////////////////////// ImageArc($id,$img_center_x,$img_center_y,$a,$b,0,360,$black); ImageArc($id,$img_center_x,$img_center_y+$distance,$a,$b,0,180,$black); imageline($id,$img_center_x,$img_center_y,$img_center_x+$a/2,$img_center_y,$black); ///////////////////////// Draw line to circle two arc //////////////////////// Imageline($id,$img_center_x+$a/2,$img_center_y,$img_center_x+$a/2,$img_center_y+$distance,$black); Imageline($id,$img_center_x-$a/2,$img_center_y,$img_center_x-$a/2,$img_center_y+$distance,$black); //////////////////////// Draw biscuit //////////////////////////// $angle1=$angle2=0; //angles for ($i=0;$i<$num;$i++) { $angle2=$angle1; $angle1+=$Biscuit_val[$i]*M_PI*2/100; //caculator angles if ($i!=$num-1) imageline($id,$img_center_x,$img_center_y,$img_center_x+$a*cos($angle1)/2,$img_center_y-$b*sin($angle1)/2,$black); imagefill($id,$img_center_x+$a*cos(($angle1+$angle2)/2)/4,$img_center_y-$b*sin(($angle1+$angle2)/2)/4,$color[$i]); if ($angle1>M_PI) { imageline($id,$img_center_x+$a*cos($angle1)/2,$img_center_y-$b*sin($angle1)/2,$img_center_x+$a*cos($angle1)/2,$img_center_y-$b*sin($angle1)/2+$distance,$black); if ($angle2>=M_PI) imagefill($id,$img_center_x+$a*cos(($angle1+$angle2)/2)/2,$img_center_y-$b*sin(($angle1+$angle2)/2)/2+$distance/2,$color[$i]); else imagefill($id,$img_center_x+$a*cos(($angle1+M_PI)/2)/2,$img_center_y-$b*sin(($angle1+M_PI)/2)/2+$distance/2,$color[$i]); } imagefilledrectangle($id,$left,$bottom-5,$left+10,$bottom,$color[$i]); $string=sprintf("%.2f",$Biscuit_val[$i]); $string.="%"." (".stripslashes($Biscuit_content[$i]).")"; imagestring($id,2,$left,$bottom-2,$string,$black); $bottom+=20; } ImageGif($id); Imagedestroy($id); ?>