<?php
/*--------------------------------------------------------------
Name:饼图生成文件
author: leaxin(超级菜鸟)
version:1.0.0
说明:此文件利用php的GD库生成图饼图像文件.
使用方法:只需在包含图饼文件前提供一个名为$source_array的数组即可,数组形如$source_array("项目:数量","项目:数量")使用示例(showbingtu.php):$source_array("木材:30","板材:40","五金:50");
include("inc_bingtu.php");只需以上两行即可在网页上任何想显示饼图的地方都可以像图像文件一样插入:<img src="showbingtu.php">
------------------------------------------------------------*/
$myarray_value='';
$myarray_name='';
$j=0;
for($i=0;$i<sizeof($source_array);$i++){
    $temparray=split(":",$source_array[$i]);
$name=$temparray[0];
$value=$temparray[1];
if($value!='0'){
$myarray_value[]=$value;
$myarray_name[]=$name;
}
}$imagewidth=300;//饼图宽
$imageheight=220;//饼图长
$shadowheight=20;//饼图高
$b_s=10; //说明部分与饼图的距离
$orx=$imagewidth/2;
$ory=($imageheight-$shadowheight)/2;
$saxis=$imagewidth;
$laxis=$imageheight-$shadowheight=20;$arraylen=sizeof($myarray_value);$sum=0;
$myarray;
for($i=0;$i<$arraylen;$i++) $sum+=$myarray_value[$i];
for($i=0;$i<$arraylen;$i++) $myarray[]=round($myarray_value[$i]/$sum,3);
function gety($r,$rad){//获取椭园纵坐标
       return $r*sin($rad);
}
function getx($r,$rad){//获取随园横横坐标
return $r*cos($rad);
}
function drawsample($i,$color){
   global $b_s;
   global $myarray_name;
   global $myarray;
   global $myarray_value;
   global $black;
   global $font;
   global $idfer;
   global $imagewidth;
   $x=$imagewidth+$b_s;
   $y=30+($i-1)*15;
   imagefilledrectangle($idfer,$x,$y,$x+10,$y+10,$color);
   $string=iconv("gb2312","UTF-8",$myarray_name[$i].":".$myarray_value[$i]."(".($myarray[$i]*100)."%)");
   imagettftext($idfer,12,0,$x+15,$y+7,$black,$font,$string);}
$idfer=imagecreate($imagewidth+150,$imageheight);
$white=imagecolorallocate($idfer,255,255,255);
$brdcolor=imagecolorallocate($idfer,142,142,142);
$brown=imagecolorallocate($idfer,205,205,205);
$black=imagecolorallocate($idfer,0,0,0);
//饼图预设分块颜色
$fill0=imagecolorallocate($idfer,255,140,0);
$fill1=imagecolorallocate($idfer,255,192,203);
$fill2=imagecolorallocate($idfer,208,32,144);
$fill3=imagecolorallocate($idfer,160,32,240);
$fill4=imagecolorallocate($idfer,255,218,185);
$fill5=imagecolorallocate($idfer,240,255,255);
$fill6=imagecolorallocate($idfer,99,184,255);
$fill7=imagecolorallocate($idfer,240,244,255);
$fill8=imagecolorallocate($idfer,46,139,87);
$fill9=imagecolorallocate($idfer,255,193,193);
$fill10=imagecolorallocate($idfer,139,139,0);
$fill11=imagecolorallocate($idfer,205,205,85);
$fill12=imagecolorallocate($idfer,112,128,144);
$fill13=imagecolorallocate($idfer,219,128,144);
$fill14=imagecolorallocate($idfer,139,125,128);
//....可依上继续添加
$font="./simkai.ttf";
$angle=-20;//画图饼---------------------------------------------------------------------------------------------------------
imagefill($idfer,0,0,$white);
imagearc($idfer,$orx,$ory,$saxis,$laxis,0,360,$brdcolor);//画椭圆
imagearc($idfer,$orx,$ory+$shadowheight,$saxis,$laxis,0,180,$brdcolor);//画
imageline($idfer,$orx-$saxis/2,$ory,$orx-$saxis/2,$ory+$shadowheight,$brdcolor);//圆椭
imageline($idfer,$orx+$saxis/2,$ory,$orx+$saxis/2,$ory+$shadowheight,$brdcolor);//柱面
imagefilltoborder($idfer,$orx,$ory+$laxis/2+$shadowheight/2,$brdcolor,$brown);//填充椭圆柱面
imagettftext($idfer,12,0,$orx-40,$ory+$laxis/2+$shadowheight/2,$black,$font,"total:".$sum);//在椭圆柱面上标明总量
imageline($idfer,$orx,$ory,$orx+$saxis/2,$ory,$brdcolor);//画饼图的第一条分界线
//---------------------------------------------------------------------------------------------------------------//根据数组将图饼分块------------------------------------------------------------------------------------------
$total=0;
for($i=0;$i<$arraylen;$i++){
   $total+=$myarray[$i];
   $x1=$orx+getx($saxis/2,$total*2*pi());
   $y1=$ory-gety($laxis/2,$total*2*pi());
   $xmid=$orx+getx($saxis/2.2,($total-$myarray[$i]/2)*2*pi());
   $ymid=$ory-gety($laxis/2.2,($total-$myarray[$i]/2)*2*pi());
   if($i!=($arraylen-1))
   imageline($idfer,$orx,$ory,$x1,$y1,$brdcolor);
   $fill="fill".$i;
   imagefilltoborder($idfer,$xmid,$ymid,$brdcolor,$$fill);
   drawsample($i,$$fill);
}
//------------------------------------------------------------------------------------------------------------------------
/*  此段为将百分比直接标注在饼图上
for($i=0;$i<$arraylen;$i++){
   $total+=$myarray[$i];
   $z=($i%2)?3.5:2.5;
   $xmid=$orx+getx($saxis/$z,($total-$myarray[$i]/2)*2*pi());
   $ymid=$ory-gety($laxis/$z,($total-$myarray[$i]/2)*2*pi());
   imagettftext($idfer,15,$angle,$xmid,$ymid,$black,$font,($myarray[$i]*100)."%");
}
*/
//显示图像,并释放资源
header("Content-type:image/png");
imagepng($idfer);
imagedestroy($idfer);
?>

解决方案 »

  1.   

    $source_array("木材:30","板材:40","五金:50");
    既然是数组那上面这种数组赋值方式对吗?
      

  2.   

    "木材:30" 是字符串呀
    我测试是通过的
    前提是
    在php.ini里面
    打开
    php_gd.dll和php_iconv.dll
      

  3.   

    生成图例见:http://www.firsbay.com/showbingtu.png
      

  4.   

    效果不错哦。兄弟们快来看哦!帮你UP UP UP。我是觉得数组不是应该。$source_array = array("木材:30","板材:40","五金:50");
      

  5.   

    一套绘图库(主要是统计图):http://www.aditus.nu/jpgraph/
      

  6.   

    呵呵, allkill(傲气游) 你还没看明白吗?
    $source_array = array("木材:30","板材:40","五金:50") 这是原始数组
    画图的时候我将它分别生成了
     $myarray_name $myarray_value 两个数组分别用来保存 项目和该项目的数据
    据实这样做是为了用的时候方便
    如:
    $sql="select item ,value from table1";
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)) $source_array[]=$row[0].":".$row[1];这样就可以生成$source_array了
      

  7.   

    leaxin(超级莱鸟) 
    你的意图我明白,程序我也看差不多了。我的意思你也没有明白。我的意思是开头注释那段的$source_array("木材:30","板材:40","五金:50");是不是应该是$source_array = array("木材:30","板材:40","五金:50");
      

  8.   

    呵呵,是我错了,是像你那样的
    allkill(傲气游)是个好同志,:O)