Ticket #25: barchart.py

File barchart.py, 2.2 kB (added by aprzywecki@…, 3 years ago)
Line 
1# Copyright (c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
2#
3# This file is part of PyCha.
4#
5# PyCha is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# PyCha is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.
17
18import sys
19
20import cairo
21
22import pycha.bar
23
24from precip import Tainan, Paris
25
26def barChart(output, chartFactory):
27    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 300)
28
29    dataSet = (
30        ('Tainan', [(i, l[1]) for i, l in enumerate(Tainan)]),
31        ('Paris', [(i, l[1]) for i, l in enumerate(Paris)]),
32        )
33
34    options = {
35        'axis': {
36            'x': {
37                'ticks': [dict(v=i, label=l[0]) for i, l in enumerate(Tainan)],
38                'label': 'Month',
39                'rotate': 25,
40            },
41            'y': {
42                'tickCount': 4,
43                'rotate': 25,
44                'label': 'Precipitation (mm)'
45            }
46        },
47        'background': {
48            'chartColor': '#d8e7ec',
49            'baseColor': '#efebe7',
50            'lineColor': '#444444'
51        },
52        'colorScheme': '#6eafc1',
53        'legend': {
54            'hide': False,
55            'position': {'top': 5, 'left': 5},
56        },
57        'padding': {
58            'left': 135,
59            'bottom': 55,
60        },
61        'stroke': {'hide': True},
62        'title': 'Monthly Precipitation'
63    }
64    chart = chartFactory(surface, options)
65
66    chart.addDataset(dataSet)
67    chart.render()
68
69    surface.write_to_png(output)
70
71if __name__ == '__main__':
72    if len(sys.argv) > 1:
73        output = sys.argv[1]
74    else:
75        output = 'barchart.png'
76    barChart('v' + output, pycha.bar.VerticalBarChart)
77    barChart('h' + output, pycha.bar.HorizontalBarChart)