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:
hearts (♥ – HTML:
For layout control, use a preformatted section
table. This is probably the most economic way to create charts in terms of server load
and page download times.
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:
•
),hearts (♥ – HTML:
♥
) etc.For layout control, use a preformatted section
<PRE><PRE>
or atable. 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
it will be drawn as a number
value
$maxval
andit will be drawn as a number
$maxsize
of stars. In order to plot thevalue
$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.
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
by
a
stars. The only difference this time,
of stars.
$base_of_gif
by
1
pixels and to stretch them to a certain height or width usinga
$stretching factor = (int)($val*$maxsize / $maxval)
as before when plottingstars. The only difference this time,
$maxsize
is in pixels insteadof 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.
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
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.
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.