<?php require("config.php");?>
<?php

//公用函数

//把角度转换为弧度
function deg2Arc($degrees) {
return($degrees * (pi()/180.0));
}

//RGB
function getRGB($color){
$R=($color>>16) & 0xff;
$G=($color>>8) & 0xff;
$B=($color) & 0xff;
return (array($R,$G,$B));
}

// 取得在椭圆心为（0，0）的椭圆上 x,y点的值
function pie_point($deg,$va,$vb){
$x= cos(deg2Arc($deg)) * $va;
$y= sin(deg2Arc($deg)) * $vb;
return (array($x, $y));
}


//3D饼图类

class Pie3d{

var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray; //每个扇形的数据
//var $TextArray;//投票文字说明
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
//为边缘及阴影为黑色
var $ImgBg; //图形背景
var $ImgFg; //图形前景色
var $ImgFg1; //图形前景色的阴影色
var $DoubleColor;//是否开启阴影功能
function Pie3d($pa=100,$pb=60,$sData="100,100,300,400,500,150", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,336699",$sText="one,two,three,four,five,six",$bg="ffffff",$fg="000000",$fg1="999999",$doublecolor="on")
{
$this->a=$pa;
$this->b=$pb;
$this->DataArray=split(",",$sData);
//$this->TextArray=split(",",$sText);
$this->ColorArray=split(",",$sColor);
$this->ImgBg=$bg;
$this->ImgFg=$fg;
$this->ImgFg1=$fg1;
$this->DoubleColor=$doublecolor;
}

function setA($v){
$this->a=$v;
}

function getA(){
return $this->a;
}

function setB($v){
$this->b=$v;
}

function getB(){
return $this->b;
}

function setDataArray($v){
$this->DataArray=split(",",$v);
}

function getDataArray($v){
return $this->DataArray;
}

function setColorArray($v){
$this->ColorArray=split(",",$v);
}

function getColorArray(){
return $this->ColorArray;
}


function DrawPie(){
$image=imagecreate($this->a*2+50,$this->b*2+50);
$PieCenterX=$this->a+5;
$PieCenterY=$this->b+5;
$DoubleA=$this->a*2;
$DoubleB=$this->b*2;
list($R,$G,$B)=getRGB(hexdec($this->ImgFg));
$colorBorder=imagecolorallocate($image,$R,$G,$B);
list($R,$G,$B)=getRGB(hexdec($this->ImgFg1));
$colorBorder1=imagecolorallocate($image,$R,$G,$B);
$DataNumber=count($this->DataArray);
//填充背境
list($R,$G,$B)=getRGB(hexdec($this->ImgBg));
imagefill($image, 0, 0, imagecolorallocate($image, $R,$G,$B));
//$DataTotal
for($i=0;$i<$DataNumber;$i++) $DataTotal+=$this->DataArray[$i]; //算出数据和

if(!$DataTotal)
 {
  imagestring($image, 5, 40, 30, "No Poll Data", $colorBorder);
  header("Content-type: image/PNG");
  imagePNG($image);
  imagedestroy($image);
  exit;
 }
if(!$DataTotal) $DataTotal=0.001;

/*
** 画每一个扇形
*/
$Degrees = 0;
for($i = 0; $i < $DataNumber; $i++){
if(!$this->DataArray[$i]) $this->DataArray[$i]=0.0001;
$StartDegrees = round($Degrees);
$Degrees += (($this->DataArray[$i]/$DataTotal)*360);
$EndDegrees = round($Degrees);
$percent = number_format($this->DataArray[$i]/$DataTotal*100, 1);
list($R,$G,$B)=getRGB(hexdec($this->ColorArray[$i]));
$CurrentColor=imagecolorallocate($image,$R,$G,$B);
if ($R>60 and $R<256) $R=$R-60;
if ($G>60 and $G<256) $G=$G-60;
if ($B>60 and $B<256) $B=$B-60;
$CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
//画扇形弧
imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);
//画直线
list($ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);
//画直线
list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);
//填充扇形
$MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);

imagefilltoborder($image,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY), $CurrentColor,$CurrentColor);
if($this->DoubleColor=="on") imagestring($image,3,floor($PieCenterX + $ArcX-15)-1,floor($PieCenterY + $ArcY-5)-1,$percent."%",$colorBorder1);
imagestring($image,3,floor($PieCenterX + $ArcX-15),floor($PieCenterY + $ArcY-5),$percent."%",$colorBorder);
/*
//画方块,由于不支持中文,所以屏蔽了
imagefilledrectangle($image,$this->a*2+20,$i*30,$this->a*2+40,$i*30+14,$CurrentColor);

$str=gb2utf8("$this->TextArray[$i]");
imagechar($image, 2, $this->a*2+60, $i*30, $str, $colorBorder);
*/


//画阴影
if ($StartDegrees>=0 and $StartDegrees<=180){
if($EndDegrees<=180){
for($k = 1; $k < 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);
}else{
for($k = 1; $k < 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
}

}
}

/*到此脚本已经生了一幅图像了
**现在需要的是把它发到浏览器上，重要的一点是要将标头发给浏览器，让它知道是一个PNG文件。不然的话你只能看到一堆奇怪的乱码
*/
//输出生成的图片
header("Content-type: image/PNG");
imagePNG($image);
imagedestroy($image);
}//End drawPie()
}//End class

############
//如果是条形图
  function DrawPie($w,$h,$d,$c,$bg,$fg,$fg1,$doublecolor)
   {
       $image=imagecreate($w*2+50,$h*3);
       $sum=array_sum($d);
       if(!$sum) $sum=0.001;
       list($R,$G,$B)=getRGB(hexdec($fg));
       $colorBorder=imagecolorallocate($image,$R,$G,$B);
       list($R,$G,$B)=getRGB(hexdec($fg1));
       $CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
       list($R,$G,$B)=getRGB(hexdec($bg));
       imagefill($image, 0, 0, imagecolorallocate($image, $R,$G,$B));//设置背景色
           if($sum<1)
           {
           imagestring($image, 5, 40, 30, "No Poll Data", $colorBorder);
           header("Content-type: image/PNG");
           imagePNG($image);
           imagedestroy($image);
           exit;
           }
       for($i=0;$i<sizeof($d);$i++)
        {
         if(!$d[$i]) $d[$i]=0.0001;
         list($R,$G,$B)=getRGB(hexdec($c[$i]));
         $CurrentColor=imagecolorallocate($image,$R,$G,$B);
         $width=number_format($d[$i]/$sum*100,1);
         $w=$w;
         $tmp=ceil($h/4);
         $width1=$i*$h*(2/5);
         imagefilledrectangle($image,$width1+$tmp+2,$width1+2,$width+$w+2,$width1,$CurrentDarkColor);
         imagefilledrectangle($image,$width+$w,$width1,$width+$w+2,$width1+$tmp,$CurrentDarkColor);
         imagefilledrectangle($image,0,$width1,$width+$w,$width1+$tmp,$CurrentColor);
        if($doublecolor=="on") imagestring($image, 5, $width+$w+4, $width1-1, $width."%", $CurrentDarkColor);
         imagestring($image, 5, $width+$w+5, $width1, $width."%", $colorBorder);
        }
           header("Content-type: image/PNG");
           imagePNG($image);
           imagedestroy($image);

   }
###########
//实现

$farr=file($polldata);
$flag=false;
for($i=1;$i<sizeof($farr);$i++)
 {
//   if($i==0) continue;
   $farr[$i]=trim($farr[$i]);
   $tmp=explode("^_^",$farr[$i]);
   $polltype[]=$tmp[0];
   $pollnum[]=$tmp[1];
   $pollcolor[]=$tmp[2];
   $flag=true;
 }
 $type=join(",",$polltype);
 $num=join(",",$pollnum);
 $color=join(",",$pollcolor);
 if(!$flag)
  {
    $polltype=array('one','two','three','four','five');
    $pollnum=array('1','1','1','1','1');
    $pollcolor=array('ffff00','abcded','339988','33ddcc','9988aa');
  }
##################################
//显示投票的方式
if($HTTP_GET_VARS["ShowType"]=="b") $out="b";
else $out="a";
if($out=="a")
 {

  $objp = new Pie3d();
  $objp->Pie3d($width,$height,$num,$color,$type,$bg,$fg,$fg1,$doublecolor);
  $objp->DrawPie();

 }
else
 {
  DrawPie($width,$height,$pollnum,$pollcolor,$bg,$fg,$fg1,$doublecolor);
 }

?>