drawing graphs in php

Posted by user434885 on Stack Overflow See other posts from Stack Overflow or by user434885
Published on 2011-01-12T05:44:07Z Indexed on 2011/01/12 5:53 UTC
Read the original article Hit count: 173

Filed under:
|
|

i am using php to generate graphs from arrays. I wish to create multiple graphs on the same page as i need to design a summary report from answers extracted from a database. Currently i am using this code and am only able to get one single graph. what additions to the code do i need to make to get multiple graphs?

<?php    
function draw_graph($values)    
{    
    // Get the total number of columns we are going to plot    
    $columns  = count($values);

    // Get the height and width of the final 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 ;

    // Generate the image 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);    

    // Fill in the background of the image    
    imagefilledrectangle($im,0,0,$width,$height,$white);        
    $maxv = 0;

    // Calculate the maximum value we are going to plot    
    for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv);

    // Now plot each column            
    for($i=0;$i<$columns;$i++)    
    {    
        $column_height = ($height / 100) * (( $values[$i] / $maxv) *100);    
        $x1 = $i*$column_width;    
        $y1 = $height-$column_height;    
        $x2 = (($i+1)*$column_width)-$padding;    
        $y2 = $height;    
        imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray_dark);    
    }

    header ("Content-type: image/png");    
    imagepng($im);  
    imagedestroy($im);    
}

$values = array("23","32","35","57","12");    
$values2 = array("123","232","335","157","102");
draw_graph($values2);    
draw_graph($values);//no output is coming    
draw_graph($values2);//no output is coming    
draw_graph($values);//no output is coming    
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about dynamic