Home Tutorial Php Phpgd php gd bar graph

 
 

Share on Google+Share on Google+
php gd bar graph
Posted on: October 31, 2009 at 12:00 AM
Advertisement
This example shows how to create Bar Graph in php gd.

<?php

$data = array('3000','2470','225','663','6666','3456','789');

$sum = array_sum($data);

$height = 255;

$width = 320;

$im = imagecreate($width,$height);

$white = imagecolorallocate($im,255,255,255);

$black = imagecolorallocate($im,0,0,0);

$red = imagecolorallocate($im,255,0,0);

imageline($im, 10, 5, 10, 230, $black);

imageline($im, 10, 230, 300, 230, $black);

$x = 15;

$y = 230;

$x_width = 20;

$y_ht = 0;

for ($i=0;$i<7;$i++){

$y_ht = ($data[$i]/$sum)* $height;

imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);

imagestring( $im,2,$x-1,$y+10,$data[$i],$black);

$x += ($x_width+20);

}

imagejpeg($im);

header("Content-type: image/jpeg");

?>

After running the program you will get the following output

Advertisement

Related Tags for php gd bar graph:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 31, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com