当前位置:Gxlcms > PHP教程 > PHP顶用GD绘制饼图

PHP顶用GD绘制饼图

时间:2021-07-01 10:21:17 帮助过:6人阅读

PHP中用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 < 10; $i++) { 74             foreach($this->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();

效果图如下:

人气教程排行