|
Bar Charts With GD
Stefan Wiesendanger
Introduction
In this tutorial, I will give ideas on how the graphics capabilities of
PHP can be used to create simple bar charts in GIF and PNG format. I will briefly touch on
non-graphics alternatives as well. My objective is to show you some basics that will enable
you to proceed to much more complex charts. The included code will give
you a working foundation to start with. If you are looking for further inspiration after
this tutorial, you can find pointers to related code - most notably for pie charts -
in the very end.
The focus will be on drawing. We'll create a chart from an array
$data
whose keys will be taken to be the X axis labels and whose values are the numbers to be
plotted on the Y axis. I will not address the question of how to get data into the array in the
first place. If you are interested in that information, I recommend you read the column
"Graphing With PHP and GD" by Allan Kent here on PHPBuilder.
One of the neat features of PHP is that you can create graphics on the fly.
In order to accomplish this, you need to compile PHP with the GD image library or install it from packages
that support GD. Under Linux, the standard mod_php3 RPM packages from RedHat have
everything you'll need for running PHP3 as an Apache module. If you insist on the newer
PHP4, RPMs are available from Troels Arvin
at http://fsr.ku.dk/people/troels/rpms/php/.
Before long, more official versions should also show up on RPMfind and
other redhat-contrib mirrors.
To integrate a PHP generated image into your web pages, all it takes is
an HTML
IMG tag which will call a script that streams image data to the
browser, instead of the IMG tag pointing to a file on disk:<IMG SRC="chart.php" HEIGHT="height_of_chart" WIDTH="width_of_chart">
I have used the techniques described on several production sites for over a
year without any problems. Just one word of caution: if you put dynamically generated
graphics on all your pages, performance may suffer. But don't despair, I'll describe
several cures for this problem later.
In general, I like to stick to the "keep it simple" rule. That's why I am going to
cover some alternative ways for drawing charts before entering into the subject of how
to create pixel graphics. For your needs, it may not even take all the layout control and
flexibility of a bitmapped image.
[ 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. | ||


