|
Bar Charts With GD
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: ♥) 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.
[ Next Page ]
| Comments: | ||
| how to install GD library in EasyPHP1-7? | bob | 11/08/05 11:06 |
| Great script, a few glitches still: ) | Startide | 09/29/05 05:43 |
| RE: how to install GD library in PHP? | ram | 06/16/05 09:10 |
| RE: Pie Charts | ricardo | 04/08/05 12:53 |
| how to install GD library in PHP? | Nafees Anjum | 02/23/05 15:48 |
| bar chart with gd | george | 11/24/04 07:28 |
| how to install GD library in PHP? | Mukul Kumar | 11/19/04 04:38 |
| RE: Pie Charts | John Slaughter Jr. | 05/04/04 14:58 |
| Pie Charts | Spartacus | 03/09/04 09:37 |
| How to install GD library | Bhaskar | 04/23/03 06:46 |
| RE: how will rotate an image that was copied | pierre | 12/25/02 11:24 |
| how will rotate an image that was copied. | fethullah uzun | 09/20/02 18:57 |
| RE: Why?!? | Digimatt | 08/21/02 12:02 |
| re-bar caps | Will Roebuck | 08/16/02 14:38 |
| RE: Why?!? | Bruno | 07/25/02 10:27 |
| this example | aginamist | 07/24/02 09:12 |
| Why?!? | Ben Smith | 07/03/02 13:06 |
| how to install GD library in PHP? | June How | 05/23/02 06:50 |
| RE: How 'bout pie charts? | Tobi Fuchs | 05/15/02 14:03 |
| Great example: how about using Flash | MKent | 05/09/02 23:17 |
| RE: To start with | zakreg | 04/18/02 03:19 |
| How 'bout pie charts? | Kevin Won | 04/13/02 02:03 |
| running in scheduled task | chris wilde | 03/22/02 22:55 |
| A faster first page | Jon Benson | 02/27/02 11:27 |
| JGraph Library | Emile Bosch | 12/28/01 07:49 |
| RE: Bar Charts With GD | Atom | 08/20/01 18:04 |
| Bar charts without GD | Matt | 08/15/01 05:28 |
| RE: Displaying Images From Php | EppO | 08/06/01 10:40 |
| problems with ImagePng() | Felix Sanchez | 08/02/01 23:34 |
| Special Caracters | Luiz Ribeiro | 07/22/01 05:05 |
| RE: bar charts | Richard | 07/09/01 01:01 |
| RE: Bar Charts With GD | jessie | 06/10/01 19:00 |
| RE: Creating Bar Charts Dynamically in ASP | Mark | 06/06/01 03:16 |
| RE: How to get GD? | Herwig Huber | 05/31/01 15:27 |
| Displaying Images From Php | Stijn | 05/27/01 19:59 |
| RE: Data from a database | Jim Wallace | 05/17/01 20:00 |
| RE: Data from a database | irina | 05/15/01 22:23 |
| smaller values | chris | 05/02/01 07:00 |
| RE: Data from a database | Luxor | 04/21/01 18:23 |
| RE: Data from a database | skyblueie | 04/20/01 05:32 |
| RE: Creating Bar Charts Dynamically in ASP | TRiBAL FiRE | 04/13/01 20:00 |
| Research | Cid | 04/10/01 20:44 |
| How to get GD? | Jesse | 03/31/01 23:57 |
| bar charts | ioan dyfrig | 03/29/01 15:01 |
| RE: Creating Bar Charts Dynamically in ASP | Viswanadha Dev | 03/16/01 08:23 |
| RE: Data from a database | GeonetiX | 01/24/01 10:46 |
| Library Problem | Animesh Das | 01/15/01 14:38 |
| RE: To start with | Mark Walker | 01/06/01 10:48 |
| RE: To start with | jeremy_brooks | 01/02/01 11:53 |
| Data from a database | John Wallard | 01/02/01 10:46 |
| To start with | Philip Degasi | 12/30/00 11:32 |
| RE: For Win32 ?? | Joerg Behrens | 12/21/00 19:11 |
| For Win32 ?? | neville | 12/20/00 00:52 |
| VHGraph | David Sachs | 12/20/00 00:01 |
| RE: Php Search Engine | Stallion | 12/19/00 16:58 |
| Php Search Engine | Vikas | 12/19/00 06:36 |
| Bar Charts With GD | Andrea Rimicci | 12/19/00 04:42 |
| Great! | niky | 12/19/00 03:18 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


