php绘图扩展为,php绘图之饼图

本程序根据txt文件中的数据生成饼图。bt1.txt文件的内容如下:

Chart

a,b,c,d,e,f,g,h

23,22,8,9,12,18,33,34

其中第一行为图标名称,第二行是x轴坐标,第三行为显示的数据。

header("Content-type: image/png");

$file_name="bt1.txt";

//$input_d=array();

$ChartLabel=array();

$ChartData = array();

$all=array();

$colorSlice=array();

function radians ($degrees)

{

return($degrees * (pi()/180.0));

}

function circle_point($degrees, $diameter)

{

$x = cos(radians($degrees)) * ($diameter/2);

$y = sin(radians($degrees)) * ($diameter/2);

return (array($x, $y));

}

//打开文件

$fp=fopen($file_name,"r");

//将数据转化成数组的格式输出

$all=file($file_name);

$title=substr ( $all[0], 0, strlen($all[0])-2);

$label=substr ( $all[1], 0, strlen($all[1])-2);

//$str=trim(fgets($fp,100));

$ChartLabel=explode(",",$label);

$ChartData=explode(",",$all[2]);

fclose($fp);

// 填充图表的参数

$ChartDiameter = 350; //图表直径

$ChartFont = 2; //

$ChartFontHeight = ImageFontHeight($ChartFont);

$ChartFontWidth = ImageFontWidth($ChartFont);

//$ChartData=$input_d;

//$ChartLable=(int)$input_d;

//确定图形的大小

$ChartWidth = $ChartDiameter +400;

$ChartHeight = $ChartDiameter + 20 +(($ChartFontHeight + 2) * count($ChartData));

//确定统计的总数

for($index = 0; $index < count($ChartData); $index++)

{

$ChartTotal += $ChartData[$index];

}

$ChartCenterX = $ChartDiameter/2 + 40;

$ChartCenterY = $ChartDiameter/2 + 60;

//生成空白图形

$image = ImageCreate($ChartWidth, $ChartHeight);

//分配颜色

$colorBody = ImageColorAllocate($image, 255, 255, 255 );

$colorBorder = ImageColorAllocate($image, 255, 100, 0 );

$colorText = ImageColorAllocate($image, 0, 0, 0);

$red=253;

$green=253;

$blue=253;

for($i=0; $i

{

$red=intval(($red+$i*200)%255);

$green=intval(($blue+blue+$i*60)%255);

$blue=intval(($yellow+$i*20)%255);

$colorSlice[$i] = ImageColorAllocate($image,$red,$green,$blue);

}

//填充背境

ImageFill($image, 0, 0, $colorBody);

$Degrees = 0;

for($index = 0; $index < count($ChartData); $index++)

{

$StartDegrees = round($Degrees);

$Degrees += (($ChartData[$index]/$ChartTotal)*360);

$EndDegrees = round($Degrees);

//$CurrentColor = $colorSlice[$index%(count($colorSlice))];

$CurrentColor = $colorSlice[$index];

//画图F

ImageARC($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,

$ChartDiameter,$StartDegrees,$EndDegrees, $CurrentColor);

//画直线

list($ArcX, $ArcY) = circle_point($StartDegrees, $ChartDiameter);

ImageLine($image,$ChartCenterX,$ChartCenterY,floor($ChartCenterX + $ArcX),

floor($ChartCenterY + $ArcY),$CurrentColor);

//画直线

list($ArcX, $ArcY) = circle_point($EndDegrees, $ChartDiameter);

ImageLine($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX + $ArcX),

ceil($ChartCenterY + $ArcY),$CurrentColor);

//填充扇形

$MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);

list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);

ImageFillToBorder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),

$CurrentColor,$CurrentColor);

}

//画边框

ImageArc($image,

$ChartCenterX,

$ChartCenterY,

$ChartDiameter,

$ChartDiameter,

0,

180,

$colorBorder);

ImageArc($image,

$ChartCenterX,

$ChartCenterY,

$ChartDiameter,

$ChartDiameter,

180,

360,

$colorBorder);

ImageArc($image,

$ChartCenterX,

$ChartCenterY,

$ChartDiameter+7,

$ChartDiameter+7,

0,

180,

$colorBorder);

ImageArc($image,

$ChartCenterX,

$ChartCenterY,

$ChartDiameter+7,

$ChartDiameter+7,

180,

360,

$colorBorder);

ImageFillToBorder($image,

floor($ChartCenterX + ($ChartDiameter/2) + 2),

$ChartCenterY,

$colorBorder,

$colorBorder);

//画图例

$LineX= $ChartDiameter +80;

$NowX=0;

for($index = 0; $index < count($ChartData); $index++)

{

//$CurrentColor = $colorSlice[$index%(count($colorSlice))];

$CurrentColor = $colorSlice[$index];

//$LineY = $ChartDiameter +20+ ($index*($ChartFontHeight+2));

//$LineX= $ChartDiameter +20+ ($index*($ChartFontHeight+40));

//$LineY = $ChartDiameter +80;

//$LineX= $ChartDiameter +40;

$LineY =60+ ($index*($ChartFontHeight+6));

//$LineX=10;

//draw color box

ImageRectangle($image,

$LineX,

$LineY,

$LineX+ $ChartFontHeight,

$LineY+$ChartFontHeight,

$colorBorder);

ImageFillToBorder($image,

$LineX+2,

$LineY + 2,

$colorBorder,

$CurrentColor);

//画标签

ImageString($image,

$ChartFont,

$LineX+20,

$LineY,

"$ChartLabel[$index]:$ChartData[$index]",

$colorText);

//$wid= strlen((string)$ChartData[$index])+strlen($ChartLabel[$index])+1;

//$wid=$wid*8;

//$LineX=$LineX+$wid+10;

ImageString($image,

$ChartFont,

400,

200+($index*($ChartFontHeight+2)),

$wid,

$colorText);

}

ImageRectangle ( $image,

5,

5,

600,

450,

$colorText);

//写标题

ImageString($image,

5,

120,

20,

$title,

$colorText);

//ImageFilledEllipse ( $image, $ChartCenterX+10,$ChartCenterY+10, $ChartDiameter, $ChartDiameter, $colorText);

//输出生成的图片

ImagePNG($image);

ImageDestroy($image);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值