我们在很多网站会看到一些图形的百分比显示图,像三个地区所占地多少或者是成绩等一下我来给大家介绍一款用php生成的扇形比例百分比显示程序代码.
不过使用它首先得有phpGD库支持,代码如下:
- <?php
-
- $ChartDiameter = 60;
- $ChartData = array(30,70);
-
- function radians($degrees){return($degrees*(pi()/180.0));}
-
- 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))];
-
- 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);
- }
-
- header(“Content-type: image/png”);
- imagegif($image);
- ?>
|