How to generate a bar chart in PHP, generate bars in php_PHP tutorial

WBOY
Release: 2016-07-13 10:12:03
Original
878 people have browsed it

How to generate bar chart in PHP, generate bar in php

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

Copy code The code is as follows:

// create an array of values ​​for the chart. These values ​​
// could come from anywhere, POST, GET, database etc.
$values ​​= array(23,32,35,57,12,3,36,54,32,15,43,24,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);
?>

The operation effect is as shown below:

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/925129.htmlTechArticleHow to generate bar charts in PHP, generate bars in php. This article describes how to generate bar charts in PHP. Share it with everyone for your reference. The specific implementation method is as follows: Copy the code The code is as follows...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!