PHP generates a bar chart. php generates a bar chart.

Source: Internet
Author: User

PHP generates a bar chart. php generates a bar chart.

This example describes how to generate a bar chart in PHP. Share it with you for your reference. The specific implementation method is as follows:

Copy codeThe Code is as follows:
<? Php
// Create an array of values for the chart. These values
// Cocould come from anywhere, POST, GET, database etc.
$ Values = array (, 30 );
 
// Now we get the number of values in the array. this will
// Tell us how many columns to plot
$ Columns = count ($ values );
 
// Set the height and width of the graph image
 
$ Width = 300;
$ Height = 200;
 
// Set the amount of space between each column
$ Padding = 5;
 
// Get the width of 1 column
$ Column_width = $ width/$ columns;
 
// Set the graph color variables
$ Im = imagecreate ($ width, $ height );
$ Gray = imagecolorallocate ($ im, 0xcc, 0xcc, 0xcc );
$ Gray_lite = imagecolorallocate ($ im, 0xee, 0xee, 0xee );
$ Gray_dark = imagecolorallocate ($ im, 0x7f, 0x7f, 0x7f );
$ White = imagecolorallocate ($ im, 0xff, 0xff, 0xff );
 
// Set the background color of the graph
Imagefilledrectangle ($ im, 0, 0, $ width, $ height, $ white );
 
 
// Calculate the maximum value we are going to plot
$ Max_value = max ($ values );
 
// Loop over the array of columns
For ($ I = 0; $ I <$ columns; $ I ++)
{
// Set the column hieght for each value
$ Column_height = ($ height/100) * ($ values [$ I]/$ max_value)
 
* 100 );
// Now the coords
$ X1 = $ I * $ column_width;
$ Y1 = $ height-$ column_height;
$ X2 = ($ I + 1) * $ column_width)-$ padding;
$ Y2 = $ height;
 
// Write the columns over the background
Imagefilledrectangle ($ im, $ x1, $ y1, $ x2, $ y2, $ gray );
 
// This gives the columns a little 3d effect
Imageline ($ im, $ x1, $ y1, $ x1, $ y2, $ gray_lite );
Imageline ($ im, $ x1, $ y2, $ x2, $ y2, $ gray_lite );
Imageline ($ im, $ x2, $ y1, $ x2, $ y2, $ gray_dark );
}
 
// Set the correct png headers
Header ("Content-type: image/png ");
// Spit the image out the other end
Imagepng ($ im );
?>

Shows the running effect:

I hope this article will help you with PHP programming.

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.