In many sites we will see a number of graphs, such as the percentage of three areas, such as the size of the area or the results, to introduce a PHP generated by a percentage of the fan ratio display program code, but it must first have PHPGD library support.
Copy Code code as follows:
<?php
//Fill chart Parameters
$ChartDiameter = 60; Chart diameter
$ChartData = Array (30,70);//The data that is used to generate the chart can be obtained from the database to determine that it can also be multiple but corresponding to a color array
//Convert angle to radians
function radians ($degrees) {return ($degrees * (pi ()/180.0));
//Gets the value of the X,y point at the center of the Circle (0,0)
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;
//Determination of total 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, 0x00, 0x00, 0x00);
$colorText = imagecolorallocate ($image, 0x00, 0x00, 0x00);
$colorSlice [] = Imagecolorallocate ($image, 0xFF, 0x00, 0x00);//This is the color that corresponds to the array you wrote above
$colorSlice [] = Imagecolorallocate ($image, 0x00, 0xFF, 0x00);
//Filling back border
Imagefill ($image, 0, 0, $colorBody);
//Draw each fan
$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))];
//Paint 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);
//Filled fan
$MidPoint = Round (($EndDegrees-$StartDegrees)/2) + $StartDegrees);
list ($ArcX, $ArcY) = Circle_point ($MidPoint, $ChartDiameter/2);
Imagefilltoborder ($image, floor ($ChartCenterX + $ArcX), floor ($ChartCenterY + $ArcY),
$CurrentColor, $CurrentColor);
}
//To this script has given birth to an image, what is needed is to send it to the browser, it is important to the hair to the browser, let it know is a GIF file. Otherwise, you can only see a bunch of weird garbled
.
header ("Content-type:image/png");
imagegif ($image);
?>