在调查程序中,我们需要根据统计的数据来生成各种图表来生动的表示调查的百分比。在PHP在这方面也是不负众望,它中可以通过加载GD库来实现一开始。饼状图表对于查看一个值占总值的百分比是一个好的方法。现在我们就用PHP来实现一个饼形图表,给大家讲述PHP在这方面的应用。它的设计思想是:首先以用imagecreate()来生成一个空白图形,然后在空白图形中用imageare()圆弧函数先画圆弧,再画两条线连接圆心和圆弧端点(PHP图像函数不能画扇形),再用imagefilltoborder函数来填充扇形。其程序实现如下:
<?php
/*
把角度转换为弧度
*/
functionradians($degrees)
{
return($degrees*(pi()/180.0));
}
/*
**取得在圆心为(0,0)圆上x,y点的值
*/
functioncircle_point($degrees,$diameter)
{
$x=cos(radians($degrees))*($diameter/2);
$y=sin(radians($degrees))*($diameter/2);
return(array($x,$y));
}
//填充图表的参数
$ChartDiameter=200;//图表直径
$ChartFont=2;//图表字体
$ChartFontHeight=imagefontheight($ChartFont);//图表字体的大小
$ChartData=array("75","45");//用于生成图表的数据,可通过数据库来取得来确定
//$ChartLabel=array("yes","no");//数据对应的名称
//确定图形的大小
$ChartWidth=$ChartDiameter+20;
$ChartHeight=$ChartDiameter+20+
(($ChartFontHeight+2)*count($ChartData));
//确定统计的总数
for($index=0;$index<count($ChartData);$index++)
{
$ChartTotal+=$ChartData[$index];
}
$ChartCenterX=$ChartDiameter/2+10;
$ChartCenterY=$ChartDiameter/2+10;
//生成空白图形
$image=imagecreate($ChartWidth,$ChartHeight);
//分配颜色
$colorBody=imagecolorallocate($image,0xFF,0xFF,0xFF);
$colorBorder=imagecolorallocate($image,0x00,0x00,0x00);
$colorText=imagecolorallocate($image,0x00,0x00,0x00);
$colorSlice[]=imagecolorallocate($image,0xFF,0x00,0x00);
$colorSlice[]=imagecolorallocate($image,0x00,0xFF,0x00);
//填充背境
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))];
//画图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);
//画图例
for($index=0;$index<count($ChartData);$index++)
{
$CurrentColor=$colorSlice[$index%(count($colorSlice))];
$LineY=$ChartDiameter+20+($index*($ChartFontHeight+2));
//drawcolorbox
imagerectangle($image,
10,
$LineY,
10+$ChartFontHeight,
$LineY+$ChartFontHeight,
$colorBorder);
imagefilltoborder($image,
12,
$LineY+2,
$colorBorder,
$CurrentColor);
//画标签
imagestring($image,
$ChartFont,
20+$ChartFontHeight,
$LineY,
"$ChartLabel[$index]:$ChartData[$index]",
$colorText);
}
//到此脚本已经生了一幅图像的,现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码
header("Content-type:image/gif");
//输出生成的图片
imagegif($image);
?>
保存为chart.php,运行程序其结果如图1.
但这是在服务器端生在GIF图片,我们要在HTML文件中应用就需要如下格式来调用它:
<?php
echo"<imgsrc='chart.php'>"
?>
注:运行环境为apache_1_3_12+php-4.0RC1+win98,windows平台下.在PHP中图像函数都是在GD库中完成,GD库实际是处理GIF格式的免费软件。要加载GD扩展才能使用php4的GD库可以到www.phpuser.com下载。解压COPYphp_gd.dll文件到PHP的执行目录,然后编辑php.ini配置文件,找到配置文件中;extension=php_gd.dll"这行去掉";"号,如果没有发现则在配置文件的'DynamicExtensions'后增加一行extension=php_gd.dl。最后运行phpinfo()函数,你就可以看到支持信息。
相关推荐
一段关于用PHP制作饼图的代码,希望大家能看看,希望对大家有用
excel表怎么制作复合饼图 excel表制作复合饼图方法.docx
php饼图 jquery饼图 highcharts
自己整理的一个C#winform demo 使用zedgraph制作饼图 条形图和折线图
用Excel制作饼图、直方图
饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图饼图...
flash 制作的饼图,非常精美!!好东西共同分享
教你用EXCEL制作多种饼图,包括动态饼图.母子饼图,双子饼图等
复合饼图-Excel图表制作心得.docx
WPF实现扇形统计图,单独控件,不需要添加dll。新手推荐。
VML饼图,PHP版。 VML只有IE支持。 演示: http://www.liuyuanjun.com/test/chart0.php
JFreeChar制作饼图和柱状图 需要jcommon-1.0.0-rc1.jar包和jfreechart-1.0.0-rc1.jar包
Excel表格简单的制作及饼图的插入PPT课件.pptx
flash饼图flash饼图flash饼图...
使用asp制作直方图、饼图 VML 画统计 柱状、饼图、折线 Powered by MicroSystem.cn
一个关于饼图制作的demo,使用eCharts插件进行配置,根据数据的来源进行页面展示的图表文件
android 饼图 饼图 饼图 自定义饼图
python获取微信好友所在地以及性别并制作饼图分析表
饼图 复制代码 代码如下: <?php //+————————+ //| pie3dfun.PHP//公用函数 | //+————————+ define(“ANGLE_STEP”, 3); //定义画椭圆弧时的角度步长 define(“FONT_USED”, “C:\WINDOWS\Fonts\...
Excel中复合饼图的制作.pdf 学习资料 复习资料 教学资源