选择显示字体大小

用php制作饼图调查表

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

/*
把角度转换为弧度
*/
function radians (&#36;degrees)
{
return(&#36;degrees * (pi()/180.0));
}
/*
** 取得在圆心为(0,0)圆上 x,y点的值
*/
function circle_point(&#36;degrees, &#36;diameter)
{
&#36;x = cos(radians(&#36;degrees)) * (&#36;diameter/2);
&#36;y = sin(radians(&#36;degrees)) * (&#36;diameter/2);

return (array(&#36;x, &#36;y));
}
// 填充图表的参数
&#36;chartdiameter = 200; //图表直径
&#36;chartfont = 2; //图表字体
&#36;chartfontheight = imagefontheight(&#36;chartfont);//图表字体的大小
&#36;chartdata = array( "75","45");//用于生成图表的数据,可通过数据库来取得来确定
//&#36;chartlabel = array("yes", "no"); //数据对应的名称

//确定图形的大小
&#36;chartwidth = &#36;chartdiameter + 20;
&#36;chartheight = &#36;chartdiameter + 20 +
((&#36;chartfontheight + 2) * count(&#36;chartdata));

//确定统计的总数
for(&#36;index = 0; &#36;index < count(&#36;chartdata); &#36;index++)
{
&#36;charttotal += &#36;chartdata[&#36;index];
}

&#36;chartcenterx = &#36;chartdiameter/2 + 10;
&#36;chartcentery = &#36;chartdiameter/2 + 10;


//生成空白图形
&#36;image = imagecreate(&#36;chartwidth, &#36;chartheight);

//分配颜色
&#36;colorbody = imagecolorallocate(&#36;image, 0xff, 0xff, 0xff);
&#36;colorborder = imagecolorallocate(&#36;image, 0x00, 0x00, 0x00);
&#36;colortext = imagecolorallocate(&#36;image, 0x00, 0x00, 0x00);

&#36;colorslice[] = imagecolorallocate(&#36;image, 0xff, 0x00, 0x00);
&#36;colorslice[] = imagecolorallocate(&#36;image, 0x00, 0xff, 0x00);


//填充背境
imagefill(&#36;image, 0, 0, &#36;colorbody);


/*
** 画每一个扇形
*/
&#36;degrees = 0;
for(&#36;index = 0; &#36;index < count(&#36;chartdata); &#36;index++)
{
&#36;startdegrees = round(&#36;degrees);
&#36;degrees += ((&#36;chartdata[&#36;index]/&#36;charttotal)*360);
&#36;enddegrees = round(&#36;degrees);

&#36;currentcolor = &#36;colorslice[&#36;index%(count(&#36;colorslice))];

//画图f
imagearc(&#36;image,&#36;chartcenterx,&#36;chartcentery,&#36;chartdiameter,
&#36;chartdiameter,&#36;startdegrees,&#36;enddegrees, &#36;currentcolor);

//画直线
list(&#36;arcx, &#36;arcy) = circle_point(&#36;startdegrees, &#36;chartdiameter);
imageline(&#36;image,&#36;chartcenterx,&#36;chartcentery,floor(&#36;chartcenterx + &#36;arcx),
floor(&#36;chartcentery + &#36;arcy),&#36;currentcolor);
//画直线
list(&#36;arcx, &#36;arcy) = circle_point(&#36;enddegrees, &#36;chartdiameter);
imageline(&#36;image,&#36;chartcenterx,&#36;chartcentery,ceil(&#36;chartcenterx + &#36;arcx),
ceil(&#36;chartcentery + &#36;arcy),&#36;currentcolor);

//填充扇形
&#36;midpoint = round(((&#36;enddegrees - &#36;startdegrees)/2) + &#36;startdegrees);
list(&#36;arcx, &#36;arcy) = circle_point(&#36;midpoint, &#36;chartdiameter/2);
imagefilltoborder(&#36;image,floor(&#36;chartcenterx + &#36;arcx),floor(&#36;chartcentery + &#36;arcy),
&#36;currentcolor,&#36;currentcolor);
}

//画边框
imagearc(&#36;image,
&#36;chartcenterx,
&#36;chartcentery,
&#36;chartdiameter,
&#36;chartdiameter,
0,
180,
&#36;colorborder);

imagearc(&#36;image,
&#36;chartcenterx,
&#36;chartcentery,
&#36;chartdiameter,
&#36;chartdiameter,
180,
360,
&#36;colorborder);


imagearc(&#36;image,
&#36;chartcenterx,
&#36;chartcentery,
&#36;chartdiameter+7,
&#36;chartdiameter+7,
0,
180,
&#36;colorborder);

imagearc(&#36;image,
&#36;chartcenterx,
&#36;chartcentery,
&#36;chartdiameter+7,
&#36;chartdiameter+7,
180,
360,
&#36;colorborder);


imagefilltoborder(&#36;image,
floor(&#36;chartcenterx + (&#36;chartdiameter/2) + 2),
&#36;chartcentery,
&#36;colorborder,
&#36;colorborder);


//画图例
for(&#36;index = 0; &#36;index < count(&#36;chartdata); &#36;index++)
{
&#36;currentcolor = &#36;colorslice[&#36;index%(count(&#36;colorslice))];
&#36;liney = &#36;chartdiameter + 20 + (&#36;index*(&#36;chartfontheight+2));

//draw color box
imagerectangle(&#36;image,
10,
&#36;liney,
10 + &#36;chartfontheight,
&#36;liney+&#36;chartfontheight,
&#36;colorborder);

imagefilltoborder(&#36;image,
12,
&#36;liney + 2,
&#36;colorborder,
&#36;currentcolor);

//画标签
imagestring(&#36;image,
&#36;chartfont,
20 + &#36;chartfontheight,
&#36;liney,
"&#36;chartlabel[&#36;index]: &#36;chartdata[&#36;index]",
&#36;colortext);
}

//到此脚本 已经生了一幅图像的,现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个gif文件。不然的话你只能看到一堆奇怪的乱码

header("content-type: image/gif");
//输出生成的图片
imagegif(&#36;image);
?>
保存为chart.php,运行程序其结果如图1.
但这是在服务器端生在gif图片,我们要在html文件中应用就需要如下格式来调用它:
<?php
echo "<img src='chart.php' > "
?>

  注:运行环境为apache_1_3_12+php-4.0rc1+win98,windows平台下. 在php中图像函数都是在gd库中完成,gd库实际是处理gif格式的免费软件。要加载gd扩展才能使用php4的gd库可以到www.phpuser.com下载。解压copy php_gd.dll文件到php的执行目录,然后编辑php.ini配置文件,找到配置文件中;extension=php_gd.dll"这行 去掉";"号,如果没有发现则在配置文件的'dynamic extensions' 后增加一行extension=php_gd.dl。最后运行phpinfo()函数,你就可以看到支持信息

原作者:清风
来源:不祥

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons