Web www.gerd-tentler.de
Version 4.10 (released Jan. 7, 2012) [Download]

Horizontal Bar Graphs

Have a look at the following examples to see how you can create your own graphs:
Example #1
The most simple graph looks like this:
graph = graphs.BarGraph('hBar')
graph.values = [380, 150, 260, 310, 430]
print graph.create()
 1 
 25% 
 2 
 10% 
 3 
 17% 
 4 
 20% 
 5 
 28% 
Example #2
Now let's add some labels and titles:
graph = graphs.BarGraph('hBar')
graph.values = [380, 150, 260, 310, 430]
graph.labels = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May']
graph.titles = ['Month', 'Sales']
print graph.create()
MonthSales
 Jan. 
 25% 
 Feb. 
 10% 
 Mar. 
 17% 
 Apr. 
 20% 
 May 
 28% 
Example #3
It's also possible to create grouped bar graphs for comparison.
Put grouped values into tuples and add a legend:
graph = graphs.BarGraph('hBar')
graph.labels = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May']
graph.titles = ['Month', 'Sales']
graph.values = [(380, 420), (150, 340), (260, 120), (310, 250), (430, 370)]
graph.legend = ['2001', '2002']
print graph.create()
MonthSales
 Jan. 
 13% 
 14% 
 Feb. 
 5% 
 11% 
 Mar. 
 9% 
 4% 
 Apr. 
 10% 
 8% 
 May 
 14% 
 12% 
 
2001
2002
Example #4
Let's change some colors and show the values:
graph = graphs.BarGraph('hBar')
graph.labels = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May']
graph.titles = ['Month', 'Sales', 'Percent']
graph.values = [(380, 420), (150, 340), (260, 120), (310, 250), (430, 370)]
graph.legend = ['2001', '2002']
graph.graphBGColor = '#B0E0B0'
graph.graphBorder = '1px solid green'
graph.graphPadding = 10
graph.titleColor = 'yellow'
graph.titleBGColor = '#60C060'
graph.barColors = ['#C0D0C0', '#80D080']
graph.barBGColor = '#D0F0D0'
graph.labelColor = 'yellow'
graph.labelBGColor = '#60C060'
graph.absValuesColor = '#008000'
graph.absValuesBGColor = '#D0F0D0'
graph.percValuesColor = '#008000'
graph.legendColor = '#008000'
graph.legendBGColor = '#D0F0D0'
graph.legendAbsValues = 1
graph.showValues = 1
print graph.create()
MonthSalesPercent
 Jan.  380 
 13% 
 420 
 14% 
 Feb.  150 
 5% 
 340 
 11% 
 Mar.  260 
 9% 
 120 
 4% 
 Apr.  310 
 10% 
 250 
 8% 
 May  430 
 14% 
 370 
 12% 
 
2001(1530)
2002(1500)
Example #5
Bar width (pixels), length (ratio) and border (CSS-spec) are also changable.
By the way, BarGraph also supports floating point numbers and negative values:
graph = graphs.BarGraph('hBar')
graph.labels = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May']
graph.barColors = 'white'
graph.showValues = 1
graph.values = [0.38, -0.15, -0.26, 0.31, 0.43]
graph.percValuesDecimals = 2
graph.barWidth = 40
graph.barLength = 1.5
graph.barBorder = '2px solid red'
print graph.create()
 Jan.  0.38 
 24.84% 
 Feb.  -0.15 
9.80% 
 Mar.  -0.26 
16.99% 
 Apr.  0.31 
 20.26% 
 May  0.43 
 28.10% 
Example #6
For large amounts of values, it may be useful to split them into several charts:
graph = graphs.BarGraph('hBar')
graph.values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
graph.labelBGColor = '#C0C0F0'
graph.barBGColor = '#E0E0F0'
graph.charts = 2
print graph.create()
 1 
 1% 
 2 
 2% 
 3 
 3% 
 4 
 4% 
 5 
 5% 
 6 
 5% 
 7 
 6% 
 8 
 7% 
 9 
 8% 
 10 
 9% 
 11 
 9% 
 12 
 8% 
 13 
 7% 
 14 
 6% 
 15 
 5% 
 16 
 5% 
 17 
 4% 
 18 
 3% 
 19 
 2% 
 20 
 1% 
Example #7
It is possible to set extra colors for bars whose value exceeds certain levels:
graph = graphs.BarGraph('hBar')
graph.values = [380, 150, 260, 310, 430]
graph.showValues = 1
graph.barLevelColors = [1, 'red', 260, 'yellow', 380, 'lightgreen']
print graph.create()
 1  380 
 25% 
 2  150 
 10% 
 3  260 
 17% 
 4  310 
 20% 
 5  430 
 28% 

NOTE: All level-color-pairs should be written in ascending order, and of course this feature is not suited for grouped bars.

You can also use the keyword MAX for setting the color of the bar with the highest value:
graph = graphs.BarGraph('hBar')
graph.values = [380, 150, 260, 310, 430]
graph.showValues = 1
graph.barLevelColors = [1, '#C0C0FF', 300, '#8080FF', 'MAX', '#4040FF']
print graph.create()
 1  380 
 25% 
 2  150 
 10% 
 3  260 
 17% 
 4  310 
 20% 
 5  430 
 28% 
Example #8
Let's do some more design changes and add a prefix to the values:
graph = graphs.BarGraph('hBar')
graph.values = [380, 150, 260, 310, 430]
graph.labels = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May']
graph.showValues = 1
graph.barColors = '#E0E0E0'
graph.barBGColor = 'white'
graph.barBorder = '1px solid #808080'
graph.labelColor = '#A0A0A0'
graph.labelBGColor = ''
graph.labelBorder = '1px dashed #A0A0A0'
graph.labelFont = 'Arial Black, Arial, Helvetica'
graph.labelSize = 16
graph.absValuesColor = 'silver'
graph.absValuesBGColor = 'white'
graph.absValuesBorder = '1px solid silver'
graph.absValuesFont = 'Verdana, Arial, Helvetica'
graph.absValuesSize = 14
graph.absValuesPrefix = '€'
graph.percValuesColor = '#C0C0C0'
graph.percValuesFont = 'Comic Sans MS, Times New Roman'
graph.percValuesSize = 16
print graph.create()
 Jan.  €380 
 25% 
 Feb.  €150 
 10% 
 Mar.  €260 
 17% 
 Apr.  €310 
 20% 
 May  €430 
 28% 
Example #9
You can also use images (PNG, JPG or GIF) instead of bar colors, and add some space between labels:
graph = graphs.BarGraph('hBar')
graph.values = [(50, 30, 40), (60, 80, 50), (70, 40, 60)]
graph.labels = ['Jan.', 'Feb.', 'Mar.']
graph.legend = ['cats', 'dogs', 'birds']
graph.barColors = ['blue.gif', 'red.gif', 'green.gif']
graph.labelSpace = 10
print graph.create()
 Jan. 
 10% 
 6% 
 8% 
 Feb. 
 13% 
 17% 
 10% 
 Mar. 
 15% 
 8% 
 13% 
 
cats
dogs
birds

NOTE: Images are not included in this package.
Example #10
Sometimes you have very similar values, like for instance 1000, 1010, 1020. The difference between them is too small to view in a graph:
graph = graphs.BarGraph('hBar')
graph.values = [1010, 1020, 1030]
graph.showValues = 1
print graph.create()
 1  1010 
 33% 
 2  1020 
 33% 
 3  1030 
 34% 

In this case you can tweak the bar length by using the baseValue variable. Please note that not only the bar length, but also the percentage changes accordingly:
graph = graphs.BarGraph('hBar')
graph.values = [1010, 1020, 1030]
graph.baseValue = 1000
graph.showValues = 1
print graph.create()
 1  1010 
 17% 
 2  1020 
 33% 
 3  1030 
 50% 

Comments