Create a pie chart survey using PHP

Source: Internet
Author: User

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

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.