In the survey program, we need to generate various charts based on the statistical data to vividly represent the percentage of the survey. In this respect, PHP is also expected to achieve the initial implementation by loading the GD Library. A pie chart is a good way to view the percentage of a value in the total value. Now we will use PHP to implement a pie chart to show you the application of PHP in this area. Its design philosophy is: first, use imagecreate () to generate a blank image, and then use the imageare () arc function to draw an arc in the blank image, draw two lines to connect the center and the arc endpoint (the PHP image function cannot draw slices), and then fill the slices with the imagefilltoborder function. The program implementation is as follows:
<? Php
/*
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 ));
}
// Fill in the chart parameters
$ ChartDiameter = 200; // Chart diameter
$ ChartFont = 2; // Chart font
$ ChartFontHeight = imagefontheight ($ ChartFont); // The font size of the chart.
$ ChartData = array ("75", "45"); // data used to generate charts, which can be determined through the database
// $ ChartLabel = array ("yes", "no"); // name of the data
// Determine the image size
$ ChartWidth = $ ChartDiameter 20;
$ ChartHeight = $ ChartDiameter 20
($ ChartFontHeight 2) * count ($ ChartData ));
// Determine the total number of statistics
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, 0x00, 0x00, 0x00 );
$ ColorText = imagecolorallocate ($ image, 0x00, 0x00, 0x00 );
$ ColorSlice = imagecolorallocate ($ image, 0xFF, 0x00, 0x00 );
$ ColorSlice [] = imagecolorallocate ($ image, 0x00, 0xFF, 0x00 );
// Fill in the background
Imagefill ($ image, 0, 0, $ colorBody );