Php generates a slice scale chart instance. On many websites, we can see a percentage chart of some images, such as how much the three regions occupy or the scores, we will introduce you to the slice proportion display process generated by using php. on many websites, we will see a graph showing the percentage of some images, such as the number of occupied areas in three regions or the score, we will introduce a program code for displaying the percentage of slices generated by using php. However, you must first use the phpGD Library.
The code is as follows:
// Fill in the chart parameters
$ ChartDiameter = 60; // Chart diameter
$ ChartData = array (); // used to generate chart data. it can be obtained through the database or multiple can match the color array.
// Converts degrees to radians
Function radians ($ degrees) {return ($ degrees * (pi ()/180.0 ));}
// Obtain the value of x and y on the circle (0, 0) with the center of the circle
Function circle_point ($ degrees, $ diameter) {$ x = cos (radians ($ degrees) * ($ diameter/2); $ y = sin (radians ($ degrees )) * ($ diameter/2); return (array ($ x, $ y ));}
// Determine the image size
$ 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 a blank image
$ 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); // The color corresponding to the array you wrote above
$ ColorSlice [] = imagecolorallocate ($ image, 0 × 00, 0xFF, 0 × 00 );
// Fill in the background
Imagefill ($ image, 0, 0, $ colorBody );
// Draw each slice
$ 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)];
// Drawing 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 );
// Fill the slice
$ MidPoint = round ($ EndDegrees-$ StartDegrees)/2) + $ StartDegrees );
List ($ ArcX, $ ArcY) = circle_point ($ MidPoint, $ ChartDiameter/2 );
Imagefilltoborder ($ image, floor ($ ChartCenterX + $ ArcX), floor ($ ChartCenterY + $ ArcY ),
$ CurrentColor, $ CurrentColor );
}
// This script has produced an image. now you need to send it to the browser. the important thing is to send the header to the browser, let it know that it is a GIF file. Otherwise, you can only see a bunch of strange garbled characters.
Header ("Content-type: image/png ");
Imagegif ($ image );
?>
...