GD barchart generator
- » Overview
- » Usage & params
- » Example
- » Notes
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().
Comments (6)
Tommy, at 9/06/'11 23:23, said:
Shahnawaz, at 13/06/'11 10:07, said:
Mitya, at 9/07/'11 14:09, said:
Tommy Carstensen, at 25/07/'11 12:03, said:
Mitya, at 25/07/'11 12:49, said:
James Welch, at 1/07/'12 14:33, said: