Home >Backend Development >PHP Tutorial >Example of generating a sector proportion chart in php_PHP tutorial

Example of generating a sector proportion chart in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:29765browse

We will see some graphic percentage displays on many websites, such as the area occupied by three regions or scores, etc. I would like to introduce to you a program code for sector-shaped percentage display generated by PHP. However, to use it, you must first have phpGD. Library support.

Copy code The code is as follows:

//Fill the parameters of the chart
$ChartDiameter = 60; //Chart diameter
$ChartData = array(30,70);//The data used to generate the chart can be determined by obtaining it from the database, or there can be multiple but corresponding to the color array
// Convert angles to radians
function radians($degrees){return($degrees*(pi()/180.0));}
//Get the x, y point on the circle with the center at (0, 0) The value of
function circle_point($degrees,$diameter){$x=cos(radians($degrees))*($diameter/2);$y=sin(radians($degrees))*($diameter/ 2);return (array($x,$y));}
//Determine the size of the graphic
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20;
//Determine the total number of statistics
$ChartTotal = “”;
for($index = 0;$index < count($ChartData);$index++){
$ChartTotal += $ChartData[$ index];
}
$ChartCenterX = $ChartDiameter/2 + 10;
$ChartCenterY = $ChartDiameter/2 + 10;
//Generate blank graphics
$image = imagecreate($ ChartWidth, $ChartHeight);
//Assign color
$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);//This is the color corresponding to the array you wrote above
$colorSlice[] = imagecolorallocate($image, 0×00, 0xFF, 0×00);
//Fill the background
imagefill($image, 0, 0, $colorBody);
//Draw each sector
$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))];
//Draw F
imagearc($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,$ChartDiameter, $StartDegrees,$EndDegrees, $CurrentColor);
//Draw a straight line
list($ArcX, $ArcY) = circle_point($StartDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ ChartCenterY,floor($ChartCenterX + $ArcX),
floor($ChartCenterY + $ArcY),$CurrentColor);
//Draw a straight line
list($ArcX, $ArcY) = circle_point($EndDegrees , $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX + $ArcX),
ceil($ChartCenterY + $ArcY),$CurrentColor);
//Filling Sector
$MidPoint = round((($EndDegrees – $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
//At this point the script has been generated What you need now is to send it to the browser. The important point is to send the header to the browser so that it knows it is a GIF file. Otherwise, you will only see a bunch of strange garbled characters
header(“Content-type: image/png”);
imagegif($image);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825089.htmlTechArticleWe will see some graphic percentage displays on many websites, such as how much land is occupied by three regions or Scores, etc., I will introduce to you a sector-shaped percentage display program generated by php...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn