Create Bar Chart Using GD library

GD library is useful for creating 2D charts.

Graph Image

Copy and paste this code in your php file
<!–/ By Raghwendra$graphValues=array(50,80,23,11,190,120,50,80,111,210,55);
$graphValues_city=array(‘mumbai’,’delhi’,’channai’,’banglore’,’chandigarh’,
‘surat’,’kanpur’,’patna’,’hyderabad’,’jammu’,’lacknow’);
header(“Content-type: image/png”);
$imgWidth=1000;
$imgHeight=280;
// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255);
$colorGrey=imagecolorallocate($image, 192, 192, 192);
$colorBlue=imagecolorallocate($image, 0, 0, 255);
// Create border around image
imageline($image, 0, 0, 0, 250, $colorGrey);
imageline($image, 0, 249, 1000, 249, $colorGrey);
for ($i=0; $i<11; $i++)
{
imagefilledrectangle($image, ($i*80)+10, (250-$graphValues[$i])+1, (($i+1)*80)-20, 248, $colorBlue);
imagefttext( $image, 10, 0, ($i*80)+strlen($graphValues_city[$i]),270, $colorGrey, ‘trebuc.ttf’, $graphValues_city[$i], array(‘lineheight’=>2.0) );
imagefttext( $image, 10, 0, ($i*80)+20,244-$graphValues[$i], $colorGrey, ‘trebuc.ttf’, $graphValues[$i], array(‘lineheight’=>2.0) );
}
imagepng($image);
imagedestroy($image);
//imagefilledrectangle($image, $i*25, (250-$graphValues[$i]), ($i+1)*25, 250, $colorBlue);
//imagefilledrectangle($image, ($i*50)-10, (250-$graphValues[$i])+1, (($i+1)*50)-5, 248, $colorBlue);
?>

rangnath

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.