Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

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: &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.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
how to install GD library in EasyPHP1-7?bob11/08/05 11:06
Great script, a few glitches still: )Startide09/29/05 05:43
RE: how to install GD library in PHP?ram06/16/05 09:10
RE: Pie Chartsricardo04/08/05 12:53
how to install GD library in PHP?Nafees Anjum02/23/05 15:48
bar chart with gdgeorge11/24/04 07:28
how to install GD library in PHP?Mukul Kumar11/19/04 04:38
RE: Pie ChartsJohn Slaughter Jr.05/04/04 14:58
Pie ChartsSpartacus03/09/04 09:37
How to install GD libraryBhaskar04/23/03 06:46
RE: how will rotate an image that was copiedpierre12/25/02 11:24
how will rotate an image that was copied.fethullah uzun09/20/02 18:57
RE: Why?!?Digimatt08/21/02 12:02
re-bar capsWill Roebuck08/16/02 14:38
RE: Why?!?Bruno07/25/02 10:27
this exampleaginamist07/24/02 09:12
Why?!?Ben Smith07/03/02 13:06
how to install GD library in PHP?June How05/23/02 06:50
RE: How 'bout pie charts?Tobi Fuchs05/15/02 14:03
Great example: how about using FlashMKent05/09/02 23:17
RE: To start withzakreg04/18/02 03:19
How 'bout pie charts?Kevin Won04/13/02 02:03
running in scheduled taskchris wilde03/22/02 22:55
A faster first pageJon Benson02/27/02 11:27
JGraph LibraryEmile Bosch12/28/01 07:49
RE: Bar Charts With GDAtom08/20/01 18:04
Bar charts without GDMatt08/15/01 05:28
RE: Displaying Images From Php EppO08/06/01 10:40
problems with ImagePng()Felix Sanchez08/02/01 23:34
Special CaractersLuiz Ribeiro07/22/01 05:05
RE: bar chartsRichard 07/09/01 01:01
RE: Bar Charts With GDjessie06/10/01 19:00
RE: Creating Bar Charts Dynamically in ASPMark06/06/01 03:16
RE: How to get GD?Herwig Huber05/31/01 15:27
Displaying Images From Php Stijn05/27/01 19:59
RE: Data from a databaseJim Wallace05/17/01 20:00
RE: Data from a databaseirina05/15/01 22:23
smaller valueschris05/02/01 07:00
RE: Data from a databaseLuxor04/21/01 18:23
RE: Data from a databaseskyblueie04/20/01 05:32
RE: Creating Bar Charts Dynamically in ASPTRiBAL FiRE04/13/01 20:00
ResearchCid04/10/01 20:44
How to get GD?Jesse03/31/01 23:57
bar chartsioan dyfrig03/29/01 15:01
RE: Creating Bar Charts Dynamically in ASPViswanadha Dev03/16/01 08:23
RE: Data from a databaseGeonetiX01/24/01 10:46
Library ProblemAnimesh Das01/15/01 14:38
RE: To start withMark Walker01/06/01 10:48
RE: To start withjeremy_brooks01/02/01 11:53
Data from a databaseJohn Wallard01/02/01 10:46
To start withPhilip Degasi12/30/00 11:32
RE: For Win32 ??Joerg Behrens12/21/00 19:11
For Win32 ??neville12/20/00 00:52
VHGraphDavid Sachs12/20/00 00:01
RE: Php Search EngineStallion12/19/00 16:58
Php Search EngineVikas12/19/00 06:36
Bar Charts With GDAndrea Rimicci12/19/00 04:42
Great!niky12/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.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.