在调查程序中,我们需要根据统计的数据来 生成各种图表来生动的表示调查的百分比 。在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
//画边框
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 $#@60; count($ChartData); $index++)
{
$CurrentColor = $colorSlice[$index%(count($colorSlice))];
$LineY = $ChartDiameter + 20 + ($index*($ChartFontHeight+2));
//draw color box
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);
?$#@62;
保存为chart.php,运行程序其结果如图1.
但这是在服务器端生在GIF图片,我们要在HTML文件中应用就需要如下格式来调用它:
$#@60;?php
echo "$#@60;img src=chart.php $#@62; "
?$#@62;
注:运行环境为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()函数,你就可以看到支持信息。
本新闻共2页,当前在第2页 1 2

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
