Home & blog  /  Scripts  /  view post  /

GD barchart generator

Overview

This is a barchart generator built using PHP's GD image library (GD is normally installed on most PHP builds). It accepts a number of parameters in the 's src and you can also set more global settings in the file itself, giving you full control over what data is displayed, in what colours, sizing, fonts and more.

Usage & params

In barchart.php, update the variable $f to point to a TTF font file the script should use (one is included in the download ZIP). In this file you can also set other global settings, such as font sizes (variables $fs, $fs2 and $fs3) and colours.

Call the script in the src of an :

"../path/to/barchart.php?params" />

...where params is a set of property/value pairings from the following:

data (string, required) - a set of number/label pairings separated by a |, and with numbers separated from labels with a colon, e.g. 5:label 1|16:label 2|3:label 3

fontAngle (int, default: -45°) - the angle of the text labels underneath each block

increment (int, default: dyamic) - the script will itself attempt to work out the most suitable numerical increments on the Y axis, depending on the data you pass. If you want to force it to a particular issue, e.g. 10 or 100, pass it here.

chartHeight (int, default: 400) - the height in pixels of the image

numUnitsAboveMax (int, default: 1) - the number of increments above the highest value to display (e.g. if increment is 5 and highest value is 36, the highest increment will be 40). This allows some padding between the top of the highest block and the top of the chart.

yAxisInfo (string) - a legend to be displayed vertically alongside the Y axis, explaining its role

blockWidth (int, default: 30) - the width in pixels of each block

spaceBetweenBlocks (int, default: 0) - the margin in pixels either side of each block

randomColours (bool, default: false) - if true, block colours will be random, not those set in the PHP file

noGrid (bool, default: false) - if true, no grid will appear behind the blocks

gradient (string) - either 'dark' or 'light'. Applies a gradient to the blocks (you can tailor the sensitivity of this in the PHP itself)

Example

Europe's longest rivers

Here's the code behind behind this example, applied to the 's src attribute:

1/inc/script_demos/barchart/barchart.php?

2     data=

3         2294:Volga

4         |1780:Danube

5         |1509:Ural

6         |1420:Dnieper

7         |1210:Don

8         |1124:Pechora

9         |1102:North Dvina-Vychegda

10         |846:Dniester

11         |768:Rhine

12         |678:Elbe

13         |651:Vistula

14         |645:Tagus

15     &increment=500

16     &chartHeight=250

17     &yAxisInfo=Length in miles

18"/>

Notes

GD

This script requires GD to be installed as part of your PHP build. It normally is, but you can confirm this by running phpinfo().

post new comment

Comments (6)

Tommy, at 9/06/'11 23:23, said:
I think this guy stole your chart without referencing you:
http://www.gotphp.co.cc/generate-gd-bar-chart-in-php/
Shahnawaz, at 13/06/'11 10:07, said:
Tommy, First of all you need to check every corner. Then shout tommy again check my website, Reference link is there. 
Mitya, at 9/07/'11 14:09, said:
Incredible, lifting the entire page without attribution! Oh wait, there is a link to my site, but the text of the link doesn't say my site so no one would notice. Very lame.
Tommy Carstensen, at 25/07/'11 12:03, said:
Now I know where to look for original ideas. You should do something similar for pie charts.

P.S. Can't post a comment with cookies disabled...
Mitya, at 25/07/'11 12:49, said:
Thanks, Tommy. Actually I have a pie chart version of this but it was built for a client so can't really post it here. Perhaps I could adapt it and post it eventually.

My commenting system doesn't actually utilise cookies at all, so that's weird.
James Welch, at 1/07/'12 14:33, said:
Excellent script! I specifically needed a simple graph tool that i could create a graph with and then paste in to Google docs.

Do you realise how many graphing tools out there dont let you copy and paste (because they are not using gd) - something that I think many have overlooked. They look pretty, but you cant do much with them afterwards.

This script has REALLY helped me out, so thank you.

One thing? Could you make a horizontal version? I will pay for this, or get you a book from amazon etc.

Regards
James

post new comment