首页 > 编程 > PHP > 正文

PHP创建3D饼形图 圆形带百分比的图表效果

2019-11-08 01:51:41
字体:
来源:转载
供稿:网友

使用php中的imagecreatetruecolor()函数、imagefilledarc()、imagecolorallocate()、imagettftext()、imagejpeg()函数以及imagedestroy()函数来实现一个类似圆饼的百分比统计图表,也就是大家所熟悉的饼形图,多用于一些Php统计系统中,用来显示数据非常直观,使用广泛。

view sourcePRint?
01<?php
02//php创建圆饼图表
03$image = imagecreatetruecolor(200,200);  //创建画布,长宽分别为200*200
04//创建圆饼中每个要显示项目所代表的颜色
05$red = imagecolorallocate($image,255,0,0);
06$blue  = imagecolorallocate($image,0,0,255);
07$yellow = imagecolorallocate($image,255,255,0);
08$violet = imagecolorallocate($image,255,0,255);
09$white = imagecolorallocate($image,255,255,255);
10$black = imagecolorallocate($image,0,0,0);
11//创建3D底层
12for($i=120;$i>100;$i--){
13    imagefilledarc($image,100,$i,200,120,0,30,$red,IMG_ARC_PIE);//IMG_ARC_PIE注释如下:
14    imagefilledarc($image,100,$i,200,120,30,80,$blue,IMG_ARC_PIE);
15    imagefilledarc($image,100,$i,200,120,80,360,$yellow,IMG_ARC_PIE);
16}
17/*
18imagefilledarc()函数在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style的值可以是下列值按位或(OR)后的值:
19IMG_ARC_PIE
20IMG_ARC_CHORD
21IMG_ARC_NOFILL
22IMG_ARC_EDGED */
23//立体效果的最上层,显示视觉效果的重要一层
24    imagearc($image,100,100,200,120,0,360,$black);//添加黑色边框,以突出3D效果
25    imagefilledarc($image,100,100,200,120,0,30,$red,IMG_ARC_PIE);
26    imagefilledarc($image,100,100,200,120,30,80,$blue,IMG_ARC_PIE);
27    imagefilledarc($image,100,100,200,120,80,360,$yellow,IMG_ARC_PIE);
28//添加百分比数据到圆饼图表中
29$str = iconv ("gbk","UTF-8","36%");//主要针对中文输入example:占用:60%;
30imagettftext($image,10,360-15,100+70,115,$white,"simhei.ttf",$str);
31imagejpeg($image);
32imagedestroy($image);
33?>
天骄国际美易购随心所欲
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表