root/trunk/examples/stackedbarchart.py

Revision 169, 1.9 kB (checked in by lgs, 17 months ago)

Big refactor about how the colors scheme are created and used. See #29

Line 
1# Copyright (c) 2009 by Yaco S.L.
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.stackedbar
23
24
25def stackedBarChart(output, chartFactory):
26    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 400, 200)
27
28    dataSet = (
29        ('internal', [(0, 8), (1, 10), (2, 5), (3, 6)]),
30        ('external', [(0, 5), (1, 2), (2, 4), (3, 8)]),
31        )
32
33    options = {
34        'background': {
35            'chartColor': '#ffeeff',
36            'baseColor': '#ffffff',
37            'lineColor': '#444444',
38        },
39        'colorScheme': {
40            'name': 'gradient',
41            'args': {
42                'initialColor': 'red',
43            },
44        },
45        'legend': {
46            'hide': True,
47        },
48        'padding': {
49            'left': 75,
50            'bottom': 55,
51        },
52        'title': 'Sample Chart'
53    }
54    chart = chartFactory(surface, options)
55
56    chart.addDataset(dataSet)
57    chart.render()
58
59    surface.write_to_png(output)
60
61if __name__ == '__main__':
62    if len(sys.argv) > 1:
63        output = sys.argv[1]
64    else:
65        output = 'stackedbarchart.png'
66    stackedBarChart('v' + output, pycha.stackedbar.StackedVerticalBarChart)
67    stackedBarChart('h' + output, pycha.stackedbar.StackedHorizontalBarChart)
Note: See TracBrowser for help on using the browser.