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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Bar Graph generation

Status
Not open for further replies.

cyberspace

Technical User
Aug 19, 2005
968
GB
I wish to be able to generate a simple bar graph that will, for instance, show the number of entries in a database table for a particular month.

I've seen a few examples but they are rather complex to follow and i'm wondering what the absolute most basic method to do this is?

advice appreciated

PS - using purposebuilt graphics packages such as JPGraph is not going to be an option, will need to be code.

'When all else fails.......read the manual'
 
Any graphing code that produces images (either JPG, GIF or PNG) is going to use PHP's image-manipulation routines and libraries. So you may as well use JPGraph -- it's just a bunch of PHP classes anyway.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Something very simple:

Create an image that is 1px x 1px. To create a bar graph use:

while($width > 100) // 100 is the maximum width (in pixels) of the bar
{
$width = $width / 2; // Scale the bar by half
}

printf("<img src='px.gif' width='%s' height='10'>", $width);

For multiple bars use a variable called $scale_factor.


$max_bar_size=100; // The longest bar can be 100px max
$scale_factor=1;
$cur_max=0;

// Find the largest value
foreach($bars as $val)
{
if($val > $cur_max) $cur_max = $val;
}

// Now we know the maximum value work out the scale factor to fit the graph
while($cur_max * $scale_factor > $max_bar_size)
{
$scale_factor = $scale_factor/2;
}

It's in no way perfect, though is probably the best you'll do with using JPgraph or similar. I've used this in a couple of places with no problems.
 
Thanks...

Does JPGraph require installation by the admin of the PHP server? Just I have no access to the PHP installation!

'When all else fails.......read the manual'
 
nope. it's just a set of php classes. just download them and put them on your webserver. you need the GD library installed but pretty much all hosts compile that in by default.
 
JPGraph is awesome. If you need help with the code for populating the data for the graphs from SQL, let me know. There are also lots of tutorials and info about JPGraph online. Good luck.


LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
I found the following Bar Graph generator easy to use for creating simple bar graphs in a recent project:

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top