Make pie chart Questionnaire with PHP

Source: Internet
Author: User
Tags array count php and
Pie chart

In the investigation process, we need to generate a variety of graphs based on statistical data to graphically indicate the percentage of the survey. PHP In this respect is also the expectations, it can be loaded by loading the GD library to achieve the beginning. Pie charts are a good way to see the percentage of a value in total. Now we're going to use PHP to implement a pie chart that tells you about PHP's application in this area. Its design idea is: first to use Imagecreate () to generate a blank shape, and then in the blank graphics with the Imageare () Arc function first to draw a circular arc, and then draw two lines to connect the center and ARC Endpoints (PHP image function can not draw fan), Then use the Imagefilltoborder function to populate the sector. The program is implemented as follows:
$#@60;? Php

/*
Convert Angle to radians
*/
function radians ($degrees)
{
Return ($degrees * (pi ()/180.0));
}
/*
* * Get the value of the X,y point on the center of the Circle (0,0)
*/
function Circle_point ($degrees, $diameter)
{
$x = cos (radians ($degrees)) * ($diameter/2);
$y = sin (radians ($degrees)) * ($diameter/2);

Return (Array ($x, $y));
}
Fill in the parameters of a chart
$ChartDiameter = 200; Chart diameter
$ChartFont = 2; Chart font
$ChartFontHeight = Imagefontheight ($ChartFont);//Chart font size
$ChartData = Array ("75", "45");//The data used to generate the chart can be obtained from the database to determine
$ChartLabel = Array ("Yes", "no"); Name of the data corresponding

Determine the size of a graphic
$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 Blank Graphics
$image = Imagecreate ($ChartWidth, $ChartHeight);

assigning colors
$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 every fan
*/
$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))];

Paint 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 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 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);

Drawing example
for ($index = 0; $index $#@60 count ($ChartData); $index + +)
{
$CurrentColor = $colorSlice [$index% (count ($colorSlice))];
$LineY = $ChartDiameter + + ($index * ($ChartFontHeight +2));

Draw Color Box
Imagerectangle ($image,
10,
$LineY,
Ten + $ChartFontHeight,
$LineY + $ChartFontHeight,
$colorBorder);

Imagefilltoborder ($image,
12,
$LineY + 2,
$colorBorder,
$CurrentColor);

Draw a label
Imagestring ($image,
$ChartFont,
+ $ChartFontHeight,
$LineY,
"$ChartLabel [$index]: $ChartData [$index]",
$colorText);
}

To this script has given birth to an image, now need to send it to the browser, it is important to the hair to the browser, let it know is a GIF file. Otherwise, you can only see a bunch of weird gibberish.

Header ("Content-type:image/gif");
Output generated pictures
Imagegif ($image);
? $#@62;
Save As chart.php and run the program as shown in Figure 1.
But this is in a server-side GIF picture, and we want to apply it in the HTML file to call it in the following format:
$#@60;? Php
echo "$#@60;img src=chart.php $#@62;"
? $#@62;

Note: The operating environment is under the Apache_1_3_12+php-4.0rc1+win98,windows platform. In PHP, the image function is completed in the GD library, the GD library is the actual processing of GIF format free software. To load the GD extension to use the PHP4 GD library can be downloaded to www.phpuser.com. Unzip the copy php_gd.dll file to the PHP execution directory, and then edit the php.ini configuration file to locate the configuration file; Extension=php_gd.dll "This line removes the"; "number if not found in the configuration file's dynamic Add a row of extension=php_gd.dl after Extensions. Finally run the Phpinfo () function and you can see the support information.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.