On many websites, we can see some graph percentage display charts, such as the number of three regions or the score. We will introduce you to a php-generated code showing the percentage of slices, however, you must first use the phpGD library.
Copy codeThe Code is as follows:
<? Php
// 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 );
?>