Php generates slice proportion chart instances

Source: Internet
Author: User

On many websites, we can see some graph percentage display charts, such as the number of three regions or the score. We will introduce you to a php-generated code showing the percentage of slices, however, you must first use the phpGD library.
Copy codeThe Code is as follows:
<? Php
// Fill in the chart Parameters
$ ChartDiameter = 60; // chart diameter
$ ChartData = array (); // used to generate chart data. It can be obtained through the database or multiple can match the color array.
// 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 ));}
// Determine the image size
$ ChartWidth = $ ChartDiameter + 20;
$ ChartHeight = $ ChartDiameter + 20;
// Determine the total number of statistics
$ ChartTotal = "";
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, 0 × 00, 0 × 00, 0 × 00 );
$ ColorText = imagecolorallocate ($ image, 0 × 00, 0 × 00, 0 × 00 );
$ ColorSlice [] = imagecolorallocate ($ image, 0xFF, 0 × 00, 0 × 00); // The color corresponding to the array you wrote above
$ ColorSlice [] = imagecolorallocate ($ image, 0 × 00, 0xFF, 0 × 00 );
// 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 );
}
// 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/png ");
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.