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]

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.