Create a pie chart survey using PHP. 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 regard, PHP is also very optimistic. it can load G in the survey program. we need to generate various charts based on the statistical data to vividly express 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:
$ #@ 60 ;? 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 $ # @ 60; 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 );
/*
** Draw each slice
*/
$ Degrees = 0;
For ($ index = 0; $ index $ # @ 60; 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 );
}
There are two pages in this news. Currently, there are two pages in page 1st.
// Draw a border
Imagearc ($ image,
$ ChartCenterX,
$ ChartCenterY,
$ ChartDiameter,
$ ChartDiameter,
0,
180,
$ ColorBorder );
Imagearc ($ image,
$ ChartCenterX,
$ ChartCenterY,
$ ChartDiameter,
$ ChartDiameter,
180,
360,
$ ColorBorder );
Imagearc ($ image,
$ ChartCenterX,
$ ChartCenterY,
$ ChartDiameter + 7,
$ ChartDiameter + 7,
0,
180,
$ ColorBorder );
Imagearc ($ image,
$ ChartCenterX,
$ ChartCenterY,
$ ChartDiameter + 7,
$ ChartDiameter + 7,
180,
360,
$ ColorBorder );
Imagefilltoborder ($ image,
Floor ($ ChartCenterX + ($ ChartDiameter/2) + 2 ),
$ ChartCenterY,
$ ColorBorder,
$ ColorBorder );
// Draw a legend
For ($ index = 0; $ index $ # @ 60; count ($ ChartData); $ index ++)
{
$ CurrentColor = $ colorSlice [$ index % (count ($ colorSlice)];
$ LineY = $ ChartDiameter + 20 + ($ index * ($ ChartFontHeight + 2 ));
// Draw color box
Imagerectangle ($ image,
10,
$ LineY,
10 + $ ChartFontHeight,
$ LineY + $ ChartFontHeight,
$ ColorBorder );
Imagefilltoborder ($ image,
12,
$ LineY + 2,
$ ColorBorder,
$ CurrentColor );
// Draw tags
Imagestring ($ image,
$ ChartFont,
20 + $ ChartFontHeight,
$ LineY,
"$ ChartLabel [$ index]: $ ChartData [$ index]",
$ ColorText );
}
// 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/gif ");
// Output the generated image
Imagegif ($ image );
? $ #@ 62;
Save as chart. php and run the program. Result 1.
However, this is because a GIF image is generated on the server side. to apply it in an HTML file, you need to call it in the following format:
$ #@ 60 ;? Php
Echo "$ # @ 60; img src = chart. php $ # @ 62 ;"
? $ #@ 62;
Note: the runtime environment is apache_1_3_12 + php-4.0RC1 + win98, windows platform. in PHP, image functions are completed in the GD Library, the GD Library is actually a free software for processing GIF format. The GD library of php4 can be downloaded at www.phpuser.com to load the GD extension. Decompress the COPY php_gd.dll file to the PHP execution directory, and then edit php. ini configuration file, find the configuration file; extension = php_gd.dll "this line is removed"; "number, if not found, add a line extension = php_gd.dl after Dynamic Extensions of the configuration file. Run the phpinfo () function to view the supported information.
There are two pages in this news. Currently, there are two pages in page 2nd.
Generates various charts to vividly represent the percentage of the survey. In this respect, PHP is also highly promising. it can load G...