用PHP制作饼图调查表
http://www.people.com.cn/GB/channel5/569/20000623/115761.html

解决方案 »

  1.   


        Header( "Content-Type:image/gif"); 
    ///////////////////////////////////////////////////////////////////////////////////////// 
    // 
    //   程序名:            Pollbiscuit  
    // 
    //    描  述:    这是一个很小的画贺饼图的程序
    // 
    //    数  组: $Biscuit_val 包含了 Biscuit_values 的百分比。 
    //    数  组: $Biscuit_content 涉及了真正问题的答案。
    //    测  试:  
    //    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>     
        $num=count($Biscuit_val); ////////////////////////    作图区定义   ///////////////////////////////////////// 
        $img_w=300;         //图片的宽 
        $img_h=130;         //图片的高 
        $bottom=10;         //矩形的下坐标
        $left=120;         //矩形的左坐标
         
        $a=100;         //椭圆的长轴  
        $b=50;         //椭圆的短轴  
      
        $distance=10;     //矩形间的距离 
         
        $id=ImageCreate($img_w,$img_h);     //创建画图区(画布)/////////////////////////    颜色定义    /////////////////////////////////////////     $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);     //透明的颜色定义
         
        ImageFill($id,0,0,$white);             //填充透明的颜色
         
        $img_center_x=50;                 //X坐标轴在图作图区的中央坐标
        $img_center_y=$img_h/2;             //Y坐标轴在图作图区的中央坐标//////////////////////////   画椭圆    ////////////////////////// 
         
        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); /////////////////////////    画线    ////////////////////////     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); ////////////////////////   画圆饼    //////////////////////////// 
        $angle1=$angle2=0;             //角度angles 
         
        for ($i=0;$i<$num;$i++) 
        { 
            $angle2=$angle1; 
            $angle1+=$Biscuit_val[$i]*M_PI*2/100;     //计算角度 
         
        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);