方讯标志
进入方讯技术中心
 
您现在的位置: 方讯网络 >> 网络编程 >> PHP >> 教程正文
  用PHP制作饼图调查表           
用PHP制作饼图调查表

在调查程序中,我们需要根据统计的数据来 生成各种图表来生动的表示调查的百分比 。在PHP在这方面也是不负众望,它中可以通过加载GD库来实现一开始。饼状图表对于查看一个值占总值的百分比是一个好的方法。现在我们就用PHP来实现一个饼形图表,给大家讲述PHP在这方面的应用。它的设计思想是:首先以用imagecreate()来生成一个空白图形,然后在空白图形中用imageare()圆弧函数先画圆弧,再画两条线连接圆心和圆弧端点(PHP图像函数不能画扇形),再用imagefilltoborder函数来填充扇形。其程序实现如下:
$#@60;?php

/*
把角度转换为弧度
*/
function radians ($degrees)
{
return($degrees * (pi()/180.0));
}
/*
** 取得在圆心为(0,0)圆上 x,y点的值
*/
function circle_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 $#@60; 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 $#@60; 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);
}

本新闻共2页,当前在第1页  1  2  


[1] [2] 下一页

 
  • 上一篇教程:

  • 下一篇教程:
  • 关闭窗口
    我们的专业服务:

    乌鲁木齐网站建设、新疆网站建设、乌鲁木齐网页设计、新疆网页设计、新疆网络公司、乌鲁木齐网络公司

    乌鲁木齐网络推广、新疆网络推广、新疆网站推广
     
    为乌鲁木齐及新疆地区的企业和政府、事业单位提供专业服务:
    0991-3849127·13579879039
    域名注册 虚拟主机
    企业邮局 网站建设
    网站推广 解决方案
    OA办公
    自动化
    虚拟全景
    多媒体
    光盘
    触摸屏
  • 此栏目下没有热点教程

  • 没有任何图片教程
       
    乌鲁木齐方讯网络技术有限公司 备案序号:新ICP备05000167号  
     
    地址:乌鲁木齐市阿勒泰路30号 电话:0991-3849127 3830367