Bar diagram (graph) in PHP using GD
Today we'll program a function which biulds bar graph using GD library for PHP. For each bar diagram we need datas and also names for these data. So in our example our function will have array $ar ($ar['name']=data;) and $title for diagram title as input.
PHP:
-
<?
-
-
function GraphBars($ar,$title) {
-
-
//Now we need some coordinates and dimensions for our diagram.
-
$diagramWidth=500;
-
$diagramWidth2=450;
-
$diagramHeight=400;
-
$diagramHeight2=350;
-
-
// We have rectangle picture $diagramWidth x $diagramHeight and field for diagram $diagramWidth2 x $diagramHeight 2.
-
// Offset from the left side where to start diagram output.
-
$leftoffset=40;
-
-
// and Legend offset:
-
$legendOffset=50;
-
-
// Now we need to determine with of each bar. Count of bars is the count of $ar array with data. They will have equal width, so the width will be:
-
-
// Maximum value (needed for scaling)
-
-
// Now we should know how many pixels in 1 of our values.
-
$perc=$diagramHeight2/$maxval;
-
-
// GD function to create image
-
$image = imageCreate($diagramWidth, $diagramHeight+100);
-
-
// Now we need to determine some colours
-
//Background:
-
$colorBackgr = imageColorAllocate($image, 220,220,220);
-
-
//Text :
-
$colorText = imageColorAllocate($image, 0, 0, 0);
-
-
// White colour:
-
$w = imageColorAllocate($image, 255, 255, 255);
-
-
// Axes colour
-
$axiscolour = imageColorAllocate($image, 100, 100, 100);
-
-
// Bar 1 colour:
-
$colors[0] = imagecolorallocate($image, 182,110,29);
-
-
// Bar 2 colour:
-
$colors[1] = imagecolorallocate($image, 132,64,255);
-
-
//Rectangle for the diagram:
-
imageFilledRectangle($image, 0, 0, $diagramWidth - 1, $diagramHeight - 1, $colorBackgr);
-
-
// So now we can build bars
-
-
// each bar left x coordinate is $leftoffset2
-
$leftoffset2=$leftoffset;
-
// $i we need to alternate bar colours
-
$i=0;
-
foreach ($ar as $name => $val) {
-
// build bar
-
imagefilledrectangle($image,$leftoffset2+1,$diagramHeight-(25+ceil($perc*$val)),$leftoffset2+$boxWidth,$diagramHeight-25,$colors[($i%2)]);
-
// write bar title "vertically"
-
imagettftext ($image, 8, 270, $leftoffset2+($boxWidth/2)-5, $diagramHeight-20, $colorText, "arialcyr.ttf", $name);
-
$i++;
-
$leftoffset2+=$boxWidth;
-
}
-
-
// Print diagram title
-
imagettftext ($image, 14, 0, 10, 18, $colorText, "arialcyr.ttf", $title);
-
-
// Vertical axis on the left side
-
// Horizontal axes
-
// maximum value
-
ImageString ($image, 2, $leftoffset-20, $diagramHeight-(25+ceil($perc*$maxval)), $maxval, $colorText);
-
// 0
-
ImageString ($image, 2, $leftoffset-20, $diagramHeight-25, 0, $colorText);
-
// 1/4
-
// 1/2
-
// 3/4
-
imageline($image,$leftoffset-1, $diagramHeight-25,$leftoffset-1, $diagramHeight-(25+ceil($perc*$maxval)),$axiscolour);
-
-
// Just copyright text
-
imagettftext ($image, 10, 0, 10, $diagramHeight+90, $colorText, "arialcyr.ttf", "www.aurorascorpio.com");
-
-
// Output picture
-
imagepng($image);
-
// create an interlaced image for better loading in the browser
-
imageInterlace($image, 1);
-
// mark background color as being transparent
-
imageColorTransparent($image, $colorBackgr);
-
-
return;
-
}
-
?>
Don't forget to have arialcyr.ttf font in your script folder. Run it and you will see a diagram picture in your browser.

DEMO: http://www.aurorascorpio.com/test/graphs/diagrambartest.php
good script!
Comment by Segega — April 22, 2008 @ 8:23 am