PHP generates a bar chart. 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 the code as follows: 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:
The 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.
Examples in this article describes how to generate a bar chart in PHP. Share it with you for your reference. The specific implementation method is as follows: the code is as follows...