In the investigative process, we need to generate a variety of charts based on statistical data to graphically represent the percentage of surveys. In PHP in this respect is also not negative expectations, it can be loaded by the GD library to achieve the beginning. A pie chart is a good way to view the percentage of a value as a percent of the total. Now we are using PHP to implement a pie chart, to tell you about the application of PHP in this regard. Its design idea is: first to use Imagecreate () to generate a blank shape, and then in a blank shape with imageare () Arc function first draw arc, and then draw two lines to connect the center of the Circle and the arc end (PHP image function can not be drawn fan), Then use the Imagefilltoborder function to fill the sector. Its program is implemented as follows: <?php/* Convert angle to radians */function radians ($degrees) {return ($degrees * (pi ()/180.0)); }/* * Gets the value of x, Y points on the Circle Circle (0,0) */function Circle_point ($degrees, $diameter) {$x = cos (radians ($degr EES)) * ($diameter/2); $y = sin (radians ($degrees)) * ($diameter/2); Return (Array ($x, $y)); }//Fill the chart with parameter $ChartDiameter = 200; Chart diameter $ChartFont = 2; The chart font $ChartFontHeight = Imagefontheight ($ChartFont);//The size of the chart font $ChartData = Array ("75", "45");//The data used to generate the chart can be obtained through the database To determine//$ChartLabel = Array ("Yes", "no"); The name of the data//determines the size of the graph $ChartWidth = $ChartDiameter + 20; $ChartHeight = $ChartDiameter + + (($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 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); $colorSlice [] = Imagecolorallocate ($image, 0x00, 0xFF, 0x00); Fill the back Border Imagefill ($image, 0, 0, $colorBody); /* * * * Draw each sector */$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 Straight list ($ArcX, $ArcY) = Circle_point ($StartDegrees, $ChartDiameter); Imageline ($image, $ChartCenterX, $ChartCenterY, floor ($ChartCenterX + $ArcX), floor ($ChartCenterY + $ArcY), $ CurrentColor); Draw Straight list ($ArcX, $ArcY) = Circle_point ($EndDegrees, $ChartDiameter); Imageline ($image, $ChartCenterX, $ChartCenterY, ceil ($ChartCenterX + $ArcX), ceil ($ChartCenterY + $ArcY), $CurrentColor ); Fill Sector $MidPoint = Round (($EndDegrees-$StartDegrees)/2) + $StartDegrees); List ($ArcX, $ArcY) = Circle_point ($MidPoint, $ChartDiameter/2); Imagefilltoborder ($image, floor ($ChartCenterX + $ArcX), floor ($ChartCenterY + $ArcY), $CurrentColor, $CurrentColor); }//Draw Border Imagearc ($image, $ChartCenterX, $ChartCenterY, $ChartDiameter, $ChartDiameter, 0, 180 , $colorBorder); Imagearc ($image, $ChartCenterX, $ChartCenterY, $ChartDiameter, $ChartDiameter, he, he, $coloRborder); Imagearc ($image, $ChartCenterX, $ChartCenterY, $ChartDiameter +7, $ChartDiameter +7, 0, $col Orborder); Imagearc ($image, $ChartCenterX, $ChartCenterY, $ChartDiameter +7, $ChartDiameter +7, the $c Olorborder); Imagefilltoborder ($image, Floor ($ChartCenterX + ($ChartDiameter/2) + 2), $ChartCenterY, $colorBorder, $col Orborder); Drawing example for ($index = 0; $index < count ($ChartData), $index + +) {$CurrentColor = $colorSlice [$index% (count ($color Slice)]; $LineY = $ChartDiameter + + ($index * ($ChartFontHeight +2)); Draw Color Box Imagerectangle ($image, ten, $LineY, + $ChartFontHeight, $LineY + $ChartFontHeight, $colorBorder); Imagefilltoborder ($image, $LineY + 2, $colorBorder, $CurrentColor); Draw Label Imagestring ($image, $ChartFont, + $ChartFontHeight, $LineY, "$ChartLabel [$index]: $ChartData [$ind Ex] ", $colorText);}//To this script has already had an image of, now need to send it to the browser, it is important to send the hair to the browser, let it know is a GIF file. Otherwise you can only see a bunch of strange garbled headers ("content-type:image/gif"); Output generated picture imagegif ($image); ?> saved as chart.php, running the program with its result 1. But this is born in a GIF image on the server, and we need to call it in the following format to apply it in the HTML file: <?php echo " Note: The operating environment is apache_1_3_ Under the 12+php-4.0rc1+win98,windows platform. In PHP, the image function is completed in the GD library, the GD library is actually a free software processing GIF format. To load the GD extension, you can use PHP4 's GD library to download to www.phpuser.com. Unzip the copy php_gd.dll file to the execution directory of PHP, then edit the php.ini configuration file, locate the configuration file, Extension=php_gd.dll "This line to remove"; ", if not found in the configuration file ' Dynamic Extensions ' After adding a line extension=php_gd.dl. Finally run the Phpinfo () function and you can see the support information. Yesky Yesky June 23, 2000
Making pie chart questionnaires with PHP