Jump to content

How to create histogram Chart from database table in PHP 4


websoftexpert

Recommended Posts

Hi I wish to create histogram Chart from database table.

 

There are 13 column in table

 

Column 1 = 8

Column 2 = 5

Column 3 = 8

Column 4 = 5

Column 5 = 8

Column 6 = 6

Column 7 = 7

Column 8 = 8

Column 9 = 9

Column 10 = 8

Column 11 = 12

Column 13 = 4

Column 13 = 2

 

I wish to create histogram Chart as shown in attached file

 

I am using PHP4, But I also work in PHP 5 also.

 

thanks in advance

 

A sinha

 

[attachment deleted by admin]

here's a very simple one (horizontal bars)

 

:: bar.php ::

<?php
// set dimensions
     $w = 202;
     $h = 20;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $red  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
     $green  = imagecolorallocate($im, 0x50, 0xB6, 0x30);  
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$bg);                      // border uses background colur also
     imagecolortransparent($im, $bg);                             // now make bg colour transparent
// get value and max value from query string
     $val = isset($_GET['val']) ? $_GET['val'] : 0;
     $max = isset($_GET['max']) ? $_GET['max'] : 100; 
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
 if ($barw)
     {
        $barcolor = $red;
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
     }
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

 

sample

<?php
$data = array (
		'Column 1' => 8,
		'Column 2' => 5,
		'Column 3' => 8,
		'Column 4' => 5,
		'Column 5' => 8,
		'Column 6' => 6,
		'Column 7' => 7,
		'Column 8' => 8,
		'Column 9' => 9,
		'Column 10' => 8,
		'Column 11' => 12,
		'Column 12' => 4,
		'Column 13' => 2
		);
$max = max($data);
echo '<table>';
foreach ($data as $k=>$v)
{
echo "<tr><td>$k</td><td><img src='bar.php?max=$max&val=$v'> $v</td></tr>";
}
echo '</table>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.