Sr. Pega Architect
The Computer Merchant, Ltd
US-CA-Simi Valley

Justtechjobs.com Post A Job | Post A Resume

Bar Charts With GD
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?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.