PHP中用GD繪製餅圖,gd繪製餅_PHP教程

來源:互聯網
上載者:User

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 < 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();

如下:

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...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.