PHPBuilder - Bar Charts With GD Page 2



RSS Twitter
Articles Php Functions

Bar Charts With GD - Page 2

by: Stefan Wiesendanger
|
December 17, 2000

Alternatives

PHP wouldn't be PHP if there were no other ways to achieve similar effects, without having to rely on graphics and the GD library. The simplest solution is to print a number of stars (*) proportional to the value you want to plot. You can of course use any other character you like: bullets ( - HTML: &bull;), hearts ( - HTML: &hearts;) etc. For layout control, use a preformatted section <PRE><PRE> or a table. This is probably the most economic way to create charts in terms of server load and page download times.
Let's assume the maximum data value you'll need to plot is $maxval and it will be drawn as a number $maxsize of stars. In order to plot the value $val, you could write:

<?php

echo "<PRE>"
for (
$i 0$i < (int)($val*$maxsize $maxval); $i++) echo "*"
echo 
"\n"
echo 
"</PRE>"

?>
For a whole series of values, this results in something like the following chart:
  535'385   *****
1'984'345   ********************
  354'899   ****
  893'423   *********
Another possibility is to employ tables or table cells that you force to a specific width and background color. This even lets you use border effects and graphical backgrounds on the columns and bars of your chart. However, it's not always easy to come up with a layout that will work as expected on all browsers.
The most elegant trick in my opinion is to use small GIFs of $base_of_gif by 1 pixels and to stretch them to a certain height or width using a $stretching factor = (int)($val*$maxsize / $maxval) as before when plotting stars. The only difference this time, $maxsize is in pixels instead of stars.
<IMG SRC="row.gif" HEIGHT="<?php echo $base_of_gif; ?>" WIDTH="<?php echo $stretching_factor; ?>"> 
The samples below were done in this way. Feel free to adapt the HTML source to your needs.
Charts can be done both vertically:
535'385 1'984'345 354'899 893'423
and horizontally:
535'385
1'984'345
354'899
893'423
Tim Perdue's PHPBuilder column "HTML Graphs" describes a library coincidentally called HTML_Graphs that unites all these ideas in a handy package.
Finally, you could dynamically generate Adobe PDF with PHP, but I don't really see why you would want to do that, unless you want to produce high quality material for printing. In most cases, the layout control offered by HTML will be sufficient and you won't annoy users who don't have Acrobat Reader.

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Stefan Wiesendanger

Comment:



Comment:

(Maximum characters: 1200). You have characters left.