您好,登錄后才能下訂單哦!
本篇內容主要講解“php怎么生成扇形比例圖”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“php怎么生成扇形比例圖”吧!
代碼如下:
<?php//填充圖表的參數$ChartDiameter = 60; //圖表直徑$ChartData = array(30,70);//用于生成圖表的數據,可通過數據庫來取得來確定也可以多個不過和顏色數組對應//把角度轉換為弧度function radians($degrees){return($degrees*(pi()/180.0));}//取得在圓心為(0,0)圓上 x,y點的值function circle_point($degrees,$diameter){$x=cos(radians($degrees))*($diameter/2);$y=sin(radians($degrees))*($diameter/2);return (array($x,$y));}//確定圖形的大小$ChartWidth = $ChartDiameter + 20;$ChartHeight = $ChartDiameter + 20;//確定統計的總數$ChartTotal = “”;for($index = 0;$index < count($ChartData);$index++){$ChartTotal += $ChartData[$index];}$ChartCenterX = $ChartDiameter/2 + 10;$ChartCenterY = $ChartDiameter/2 + 10;//生成空白圖形$image = imagecreate($ChartWidth, $ChartHeight);//分配顏色$colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);$colorBorder = imagecolorallocate($image, 0×00, 0×00, 0×00);$colorText = imagecolorallocate($image, 0×00, 0×00, 0×00);$colorSlice[] = imagecolorallocate($image, 0xFF, 0×00, 0×00);//這里是和你上面寫的數組對應的顏色$colorSlice[] = imagecolorallocate($image, 0×00, 0xFF, 0×00);//填充背境imagefill($image, 0, 0, $colorBody);//畫每一個扇形$Degrees = 0;for($index = 0; $index < count($ChartData); $index++){$StartDegrees = round($Degrees);$Degrees += (($ChartData[$index]/$ChartTotal)*360);$EndDegrees = round($Degrees);$CurrentColor = $colorSlice[$index%(count($colorSlice))];//畫圖Fimagearc($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,$ChartDiameter,$StartDegrees,$EndDegrees, $CurrentColor);//畫直線list($ArcX, $ArcY) = circle_point($StartDegrees, $ChartDiameter);imageline($image,$ChartCenterX,$ChartCenterY,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),$CurrentColor);//畫直線list($ArcX, $ArcY) = circle_point($EndDegrees, $ChartDiameter);imageline($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX + $ArcX),ceil($ChartCenterY + $ArcY),$CurrentColor);//填充扇形$MidPoint = round((($EndDegrees – $StartDegrees)/2) + $StartDegrees);list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),$CurrentColor,$CurrentColor);}//到此腳本 已經生了一幅圖像的,現在需要的是把它發到瀏覽器上,重要的一點是要將標頭發給瀏覽器,讓它知道是一個GIF文件。不然的話你只能看到一堆奇怪的亂碼header(“Content-type: image/png”);imagegif($image);?>
到此,相信大家對“php怎么生成扇形比例圖”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。