这是我参考书目后编写的一个画饼图的程序,完成后发现不能显示。然后逐行检查,逐行注释,可是还是没有发现问题。仔细检查了配置,也没有发现什么问题。急求大牛帮助,小弟先在此拜谢!
程序:Chart.class.php
<?php
function radians($degrees)//将弧度转换为角度
{
return($degrees*(pi()/180.0));
}
function getRGB($color)//获取RGB值
{
$R=($color>>16)&0xff;
$G=($color>>8)&0xff;
$B=($color)&0xff;
return (array($R,$G,$B));
}
function circle_point($deg,$va,$vb)//取得在椭圆心为(0,0)的椭圆上 x,y点的值 
{
$x=cos(radians($deg))*$va;
$y=sin(radians($deg))*$vb;
return (array($x,$y));
}
class Chart{
var $a;//椭圆的长半轴
var $b;//椭圆的短半轴
var $DataArray;//每个扇形的数据
var $ColorArray;//每个扇形的颜色
function Chart($pa=100,$pb=60,$sData,$sColor)//为边缘及阴影为黑色
{
$this->a=$pa;
$this->b=$pb;
$this->DataArray=split(",",$sData);
$this->ColorArray=split(",",$sColor);
}
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 DrawChart()
{$image=imagecreate($this->a*2+40,$this->b*2+40);//建立扇形的背景图
$PieCenterX=$this->a+10;//获取椭圆圆心的X坐标
$PieCenterY=$this->b+10;//获取椭圆圆心的Y坐标
$DoubleA=$this->a*2;//获取椭圆的长轴
$DoubleB=$this->b*2;//获取椭圆的短轴
list($R,$G,$B)=getRGB(0);//获取颜色中各变量的值
$colorBorder=imagecolorallocate($image,$R,$G,$B);//定义边框的颜色
$DataNumber=count($this->DataArray);//获取数据数组的元素个数
for($i=0;$i<$DataNumber;$i++)//获取各数据的和
{$DataTotal+=$this->DataArray[$i];
}
imagefill($image,0,0,imagecolorallocate($image,0xFF,0xFF,0xFF));//填充背景图为白色
$Degrees=0;//定义角度变量
for($i=0;$i<$DataNumber;$i++)//画每个扇形
{$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]));//将颜色数组中的颜色转换为10进制
$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)=circle_point($StartDegrees,$this->a,$this->b);//获取起始$ArcX,$ArcY的值
imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX+$ArcX),floor($PieCenterY+$ArcY),$CurrentColor);//画直线floor返回不大于value的下个整数
list($ArcX,$ArcY)=circle_point($EndDegrees,$this->a,$this->b);//获取终点的坐标值
imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX+$ArcX),ceil($PieCenterY+$ArcY),$CurrentColor);//画直线ceil返回不小于value的下个整数
$MidPoint=round((($EndDegrees-$StartDegrees)/2)+$StartDegrees);//获取扇形角度的一半
list($ArcX,$ArcY)=circle_point($MidPoint,$this->a*3/4,$this->b*3/4);//获取扇形内部的某点
imagefilltoborder($image,floor($PieCenterX+$ArcX),floor($PieCenterY+$ArcY),$CurrentColor,$CurrentColor);//填充扇形
imagestring($image,2,floor($PieCenterX+$ArcX-5),floor($PieCenterY+$ArcY-5),$percent."%",$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);
    }
}
}
}
header('Content-type:image/png');
imagepng($image);
imagedestroy($image);
}
}?>
运行函数:st.php
<?php
require_once("Chart.class.php");
$pa=100;
$pb=60;
$inData="100,200,300,400,500,300";
$inColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00";
$objChart=new Chart($pa,$pb,$inData,$inColor);
$objChart->DrawChart();
?>
用其他浏览器运行时只是输出一个X,在火狐中输出时提示图像因其本身有错无法输出,错误截图在附件!
小弟再次拜谢    

解决方案 »

  1.   

    疯了,不是有FLASH+JS的那种曲线图吗
    用这个你不疯了
      

  2.   

    <?php
    function radians($degrees)//将弧度转换为角度
    {
    return($degrees*(pi()/180.0));
    }
    function getRGB($color)//获取RGB值
    {
    $R=($color>>16)&0xff;
    $G=($color>>8)&0xff;
    $B=($color)&0xff;
    return (array($R,$G,$B));
    }
    function circle_point($deg,$va,$vb)//取得在椭圆心为(0,0)的椭圆上 x,y点的值
    {
    $x=cos(radians($deg))*$va;
    $y=sin(radians($deg))*$vb;
    return (array($x,$y));
    }
    class Chart{
    var $a;//椭圆的长半轴
    var $b;//椭圆的短半轴
    var $DataArray;//每个扇形的数据
    var $ColorArray;//每个扇形的颜色
    function Chart($pa=100,$pb=60,$sData,$sColor)//为边缘及阴影为黑色
    {
    $this->a=$pa;
    $this->b=$pb;
    $this->DataArray=split(",",$sData);
    $this->ColorArray=split(",",$sColor);
    }
    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 DrawChart()
    {$image=imagecreate($this->a*2+40,$this->b*2+40);//建立扇形的背景图
    $PieCenterX=$this->a+10;//获取椭圆圆心的X坐标
    $PieCenterY=$this->b+10;//获取椭圆圆心的Y坐标
    $DoubleA=$this->a*2;//获取椭圆的长轴
    $DoubleB=$this->b*2;//获取椭圆的短轴
    list($R,$G,$B)=getRGB(0);//获取颜色中各变量的值
    $colorBorder=imagecolorallocate($image,$R,$G,$B);//定义边框的颜色
    $DataNumber=count($this->DataArray);//获取数据数组的元素个数
    for($i=0;$i <$DataNumber;$i++)//获取各数据的和
    {$DataTotal+=$this->DataArray[$i];
    }
    imagefill($image,0,0,imagecolorallocate($image,0xFF,0xFF,0xFF));//填充背景图为白色
    $Degrees=0;//定义角度变量
    for($i=0;$i <$DataNumber;$i++)//画每个扇形
    {$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]));//将颜色数组中的颜色转换为10进制
    $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)=circle_point($StartDegrees,$this->a,$this->b);//获取起始$ArcX,$ArcY的值
    imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX+$ArcX),floor($PieCenterY+$ArcY),$CurrentColor);// 画直线floor返回不大于value的下个整数
    list($ArcX,$ArcY)=circle_point($EndDegrees,$this->a,$this->b);//获取终点的坐标值
    imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX+$ArcX),ceil($PieCenterY+$ArcY),$CurrentColor);// 画直线ceil返回不小于value的下个整数
    $MidPoint=round((($EndDegrees-$StartDegrees)/2)+$StartDegrees);//获取扇形角度的一半
    list($ArcX,$ArcY)=circle_point($MidPoint,$this->a*3/4,$this->b*3/4);//获取扇形内部的某点
    imagefilltoborder($image,floor($PieCenterX+$ArcX),floor($PieCenterY+$ArcY),$CurrentColor,$CurrentColor);//填充扇形
    imagestring($image,2,floor($PieCenterX+$ArcX-5),floor($PieCenterY+$ArcY-5),$percent."%",$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);
        }
    }
    }
    }
    header('Content-type:image/png');
    imagepng($image);
    imagedestroy($image);
    }
    }Header("Content-type: image/png");  //设置页面为png图像
    $pa=100;
    $pb=60;
    $inData="100,200,300,400,500,300";
    $inColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00";
    $objChart=new Chart($pa,$pb,$inData,$inColor);
    $objChart->DrawChart();
    ?> 
    漏了 Header("Content-type: image/png");  //设置页面为png图像 这个在我这边是显示正常的。