使用PHP中的imagecreatetruecolor()函数、imagefilledarc()、imagecolorallocate()、imagettftext()、imagejpeg()函数以及imagedestroy()函数来实现一个类似圆饼的百分比统计图表,也就是大家所熟悉的饼形图,多用于一些Php统计系统中,用来显示数据非常直观,使用广泛。<?php
//php创建圆饼图表
$image = imagecreatetruecolor(200,200); //创建画布,长宽分别为200*200
//创建圆饼中每个要显示项目所代表的颜色
$red = imagecolorallocate($image,255,0,0);
$blue = imagecolorallocate($image,0,0,255);
$yellow = imagecolorallocate($image,255,255,0);
$violet = imagecolorallocate($image,255,0,255);
$white = imagecolorallocate($image,255,255,255);
$black = imagecolorallocate($image,0,0,0);
//创建3D底层
for($i=120;$i>100;$i--){
imagefilledarc($image,100,$i,200,120,0,30,$red,IMG_ARC_PIE);//IMG_ARC_PIE注释如下:
imagefilledarc($image,100,$i,200,120,30,80,$blue,IMG_ARC_PIE);
imagefilledarc($image,100,$i,200,120,80,360,$yellow,IMG_ARC_PIE);
}
/*
imagefilledarc()函数在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style的值可以是下列值按位或(OR)后的值:
IMG_ARC_PIE
IMG_ARC_CHORD
IMG_ARC_NOFILL
IMG_ARC_EDGED */
//立体效果的最上层,显示视觉效果的重要一层
imagearc($image,100,100,200,120,0,360,$black);//添加黑色边框,以突出3D效果
imagefilledarc($image,100,100,200,120,0,30,$red,IMG_ARC_PIE);
imagefilledarc($image,100,100,200,120,30,80,$blue,IMG_ARC_PIE);
imagefilledarc($image,100,100,200,120,80,360,$yellow,IMG_ARC_PIE);
//添加百分比数据到圆饼图表中
$str = iconv ("gbk","UTF-8","36%");//主要针对中文输入example:占用:60%;
imagettftext($image,10,360-15,100+70,115,$white,"simhei.ttf",$str);
imagejpeg($image);
imagedestroy($image);
?>