php產生扇形比例圖執行個體_php執行個體

來源:互聯網
上載者:User
我們在很多網站會看到一些圖形的百分比顯示圖,像三個地區所佔地多少或者是成績等,給大家介紹一款用php產生的扇形比例百分比顯示程式碼,不過使用它首先得有phpGD庫支援。
複製代碼 代碼如下:

<?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))];
//畫圖F
imagearc($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);
?>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.