您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 锦州分类信息网,免费分类信息发布

PHP中用GD绘制饼图,gd绘制饼_PHP教程

2024/5/15 9:47:46发布21次查看
php中用gd绘制饼图,gd绘制饼php中用gd绘制饼图,绘制的类见代码:
1 class chart{ 2 private $image; // 定义图像 3 private $title; // 定义标题 4 private $ydata; // 定义y轴数据 5 private $xdata; // 定义x轴数据 6 private $color; // 定义条形图颜色 7 private $bgcolor; // 定义图片背景颜色 8 private $width; // 定义图片的宽 9 private $height; // 定义图片的长 10 11 /* 12 * 构造函数 13 * string title 图片标题 14 * array xdata 索引数组,x轴数据 15 * array ydata 索引数组,数字数组,y轴数据 16 */ 17 function __construct($title,$xdata,$ydata) { 18 $this->title = $title; 19 $this->xdata = $xdata; 20 $this->ydata = $ydata; 21 $this->color = array('#058dc7', '#50b432', '#ed561b', '#dddf00', '#24cbe5', '#64e572', '#ff9655', '#fff263', '#6af9c4'); 22 } 23 24 /* 25 * 公有方法,设置条形图的颜色 26 * array color 颜色数组,元素取值为'#058dc7'这种形式 27 */ 28 function setbarcolor($color){ 29 $this->color = $color; 30 } 31 32 /* 33 * 绘制饼图 34 */ 35 function mkpiechart() { 36 $sum = array_sum($this->ydata); // 获取ydata所有元素之和 37 $start = 0; // 弧的开始角度 38 $end = 0; // 弧的结束角度 39 $piewidth = 300; // 椭圆的长轴 40 $pieheight = 220; // 椭圆的短轴 41 $space = 40; // 椭圆与小矩形的间距 42 $margin = 20; // 图片的边距 43 $recwidth = 20; // 小矩形的宽 44 $recheight = 15; // 小矩形的高 45 $titleheight = 50; // 标题区域的高 46 // 图片自适应宽与高 47 $this->width = $piewidth + $this->arraylengthmax($this->xdata)*10*4/3 + $space + $recwidth +$margin; 48 $this->height = (($pieheight > count($this->xdata)*25 ) ? $pieheight : count($this->xdata)*25) + $titleheight; 49 // 椭圆中心的坐标 50 $cx = $piewidth/2+$margin; 51 $cy = $pieheight/2+$titleheight; 52 53 $this->image = imagecreatetruecolor($this->width ,$this->height); // 准备画布 54 $this->bgcolor = imagecolorallocate($this->image,255,255,255); // 图片的背景颜色 55 imagefill($this->image,0,0,$this->bgcolor); // 填充背景 56 57 // 设置条形图的颜色 58 $color = array(); 59 foreach($this->color as $col) { 60 $col = substr($col,1,strlen($col)-1); 61 $red = hexdec(substr($col,0,2)); 62 $green = hexdec(substr($col,2,2)); 63 $blue = hexdec(substr($col,4,2)); 64 $color[] = imagecolorallocate($this->image ,$red, $green, $blue); 65 } 66 67 // 设置线段的颜色、字体的颜色、字体的路径 68 $linecolor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc); 69 $fontcolor = imagecolorallocate($this->image, 0x95,0x8f,0x8f); 70 $fontpath = 'font/simsun.ttc'; 71 72 // 绘制扇形弧 73 for($i = 0; $i ydata as $key => $val) { 75 $end += 360*$val/$sum; 76 imagefilledarc($this->image,$cx,$cy-$i,$piewidth,$pieheight, $start,$end,$color[$key%count($this->color)],img_arc_pie); 77 $start = $end; 78 } 79 } 80 81 // 绘制小矩形及之后文字说明 82 $x1 = $piewidth+$space; 83 $y1 = $titleheight ; 84 foreach($this->ydata as $key => $val) { 85 imagefilledrectangle($this->image,$x1,$y1,$x1+$recwidth,$y1+$recheight,$color[$key%count($this->color)]); 86 imagettftext($this->image,10,0,$x1+$recwidth+5,$y1+$recheight-2,$fontcolor,$fontpath,$this->xdata[$key]); 87 $y1 += $recheight + 10; 88 } 89 90 // 绘画标题 91 $titlestart = ($this->width - 5.5*strlen($this->title))/2; 92 imagettftext($this->image,11,0,$titlestart,20,$fontcolor,$fontpath,$this->title); 93 94 // 输出图片 95 header(content-type:image/png); 96 imagepng($this->image); 97 } 98 99 /*100 * 私有方法,求数组中元素长度最大的值 101 * array arr 字符串数组,必须是汉字102 */103 private function arraylengthmax($arr) {104 $length = 0;105 foreach($arr as $val) {106 $length = strlen($val) > $length ? strlen($val) : $length;107 }108 return $length/3;109 } 110 111 // 析构函数112 function __destruct(){113 imagedestroy($this->image);114 }115 }
测试代码如下:
1 $xdata = array('测试一','测试二','测试三','测试四','测试五','测试六','测试七','测试八','测试九');2 $ydata = array(89,90,90,23,35,45,56,23,56);3 $img = new chart($title,$xdata,$ydata);4 $img->mkpiechart();
效果图如下:
http://www.bkjia.com/phpjc/1063518.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1063518.htmltecharticlephp中用gd绘制饼图,gd绘制饼 php中用gd绘制饼图,绘制的类见代码: 1 class chart{ 2 private $image ; // 定义图像 3 private $title ; // 定义标题 4 priv...
锦州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录