For a pie chart, you can use img to directly control its width and height.

Source: Internet
Author: User
Call method: imagesrcchart. phpchart. php is the file name of this article? * Convert the angle to the radian * functionradians ($ degrees) {return ($ degrees * (pi () 180.0);} *** to obtain (0, 0) at the center of the curve) * functioncircle_point ($ degrees, $ diameter) {$ xcos (radia // call method:
// Chart. php is the file name of this article


/*
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.

Require ('MySQL. php3 ');
$ SQL = new mysql_class;
$ SQL-> create ('star ');
$ SQL-> query ("select * from serch where answer = 'fujitsu Changhao Jiuduan '");
$ Record1 = $ SQL-> rows;
$ SQL-> query ("select * from serch where answer = 'fujitsu Cao Yunxuan Jiuduan '");
$ Record2 = $ SQL-> rows;

$ ChartData = array ($ record1, $ record2); // data used to generate charts, which can be determined through the database
// $ ChartLabel = array ("Changhao Jiuduan", "Cao Yunxuan Jiuduan"); // 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, 0xe4, 0xe6, 0xd1 );
$ ColorBorder = imagecolorallocate ($ image, 0x00, 0x00, 0x00 );
$ ColorText = imagecolorallocate ($ image, 0x00, 0x00, 0x00 );

$ ColorSlice [] = imagecolorallocate ($ image, 0xFF, 0x00, 0x00 );
$ ColorSlice [] = imagecolorallocate ($ image, 0x00, 0x00, 0xFF );


// Fill in the background
Imagefill ($ image, 0, 0, $ colorBody );


/*
** Draw each slice
*/
$ 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 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 );
}

// 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 <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,
"$ 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 );
?>

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.