All about PHP/MySQL

Thursday, February 5, 2009

Graphical Bar for Percentage Values

Following function can be used to display the percentage value (e.g Polling Result) in form of graphical bar.

Function:

function DrawBar($percentage) {
if ($percentage > 0)
{
$per = imagecreate($percentage * 3,20);
$background = imagecolorallocate($per, 0x00, 0x99, 0xFF);
$foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);
$border = imagecolorallocate($per, 0x99, 0x99, 0x99);

header("Content-type: image/png");
imagepng($per);
imagedestroy($per);
}else{
$per = imagecreate(2,20);
$background = imagecolorallocate($per, 0x00, 0x99, 0xFF);
$foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);
$border = imagecolorallocate($per, 0x99, 0x99, 0x99);

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

Usage:

DrawBar($_GET['value']);

$_GET['value'] is the percentage value which we want the function to display in graphical format. You can use this script in <img> tag also. Create a file named as graph.php and save the above code in it. Then in html portion you can call the script in the following way:

<img src="graph.php?value=75">

<img src="graph.php?value=65">

Easy and Useful!

Happy Coding :)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home