Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Bar graphs without GD Lib

PHP and Ghraphics

Creating Bar graphs without GD Lib

by  4x4uk  Posted    (Edited  )
If you can't or don't want to install GD Lib but still want to be able to manipulate images then this may be of interest to you. Particularly if you want to produce some simple bar graphs as I did.

First of all create a 2*10 pixel gif image (called bar.gif in this eaxmple)
then have a look at the script below and modify it to suit your own needs

<?php
// set the graphic scaling value
// use this to increase the size of the graphics
$scale="1.5";

// Use your own values here ie results from mysql query etc

$value1="10";
$value2="25";
$value3="80";
$total=($value1+$value2+$value3);

//Calculate Value as a pecentage with 1 decimal place showing

$pcv1=round($value1/$total*100,1);
$pcv2=round($value2/$total*100,1);
$pcv3=round($value3/$total*100,1);

// Multiply Percentage value by Graphic scaling value
// Not totally necessary but this gives a level of
// control over the final graph size

$bar1=$pcv1*$scale;
$bar2=$pcv2*$scale;
$bar3=$pcv3*$scale;

// Output the data

echo"<html><head></head><body>
<table border=\"0\"><tr>
<td><b>Value 1</b></td><td width=\"150px\"></td><td align=\"center\"><b>%</b></td><td><b>Count</b></td>
</tr>
<tr><td>Value 1</td>
<td>
<img src=\"bar.gif\" width=\"$bar1\" height=\"10\" hspace=\"0\" vspace=\"0\" border=\"0\" align=\"top\">
</td><td align=\"right\">$pcv1</td><td align=\"right\">$value1</td>
</tr>
<tr>
<td>Value 2</td><td>
<img src=\"bar.gif\" width=\"$bar2\" height=\"10\" hspace=\"0\" vspace=\"0\" border=\"0\" align=\"top\">
</td><td align=\"right\">$pcv2</td><td align=\"right\">$value2</td>
</tr>
<tr>
<td>Value 3</td><td>
<img src=\"bar.gif\" width=\"$bar3\" height=\"10\" hspace=\"0\" vspace=\"0\" border=\"0\" align=\"top\">
</td><td align=\"right\">$pcv3</td><td align=\"right\">$value3</td>
</tr></table></body></html>";
?>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top