Version: 1
Type: Function
Category: Graphics
License: GNU General Public License
Description: Function to create a bar chart by passing an array with labels as the index and the value, and a filename for the image.
<?php function makeChart($info, $name) { // store the keys in separate array for display as label $keys = array_keys($info); // Set the maximum $max = max($info); // Determine how many bars to create $n = count($info); // set up image $height = $n*100+1; $width = 600; $barstart = 100; $barwidth = 580; $im = ImageCreate($width+1, $height+1); $white = ImageColorAllocate ($im, 255, 255, 255); $black = ImageColorAllocate ($im, 0, 0, 0); $blue = ImageColorAllocate($im, 0,0,255); // Create initial image w/borders ImageFilledRectangle($im, 0, 0, $width, $height,$white); ImageRectangle($im, 0, 0, $width,$height,$black); $horiz = 0; // for each value divide chart and draw bar for ($i = 0; $i < $n; $i++ ) { // Print division line ImageLine($im, 0, $horiz, $width-1, $horiz, $black); // get and print label $label = $keys[$i]; ImageString($im, 4, 10, $horiz+5, $label, $blue); // print value ImageString($im, 30, 10, $horiz+75, $info[$keys[$i]], $black); // draw outline of entire bar ImageRectangle($im, $barstart, $horiz+15, $barwidth,$horiz+75,$black); // Standardize the values. Max will fill all of bar $perc = $info[$keys[$i]]/$max; $filled = ($barwidth-$barstart)*$perc; // Draw filled bar ImageFilledRectangle($im, $barstart, $horiz+15, $barstart+$filled,$horiz+75,$blue); // Move down to next area $horiz += 100; } // output image ImagePng ($im,$name); // clean up ImageDestroy($im); } ?>