root/trunk/examples/barchart.py @ 169

Revision 169, 2.1 kB (checked in by lgs, 18 months ago)

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

RevLine 
[85]1# Copyright (c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
[32]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 lines import lines
25
[33]26def barChart(output, chartFactory):
[32]27    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 400, 200)
28
29    dataSet = (
30        ('lines', [(i, l[1]) for i, l in enumerate(lines)]),
31        )
32
33    options = {
34        'axis': {
35            'x': {
36                'ticks': [dict(v=i, label=l[0]) for i, l in enumerate(lines)],
[82]37                'label': 'Files',
38                'rotate': 25,
[32]39            },
40            'y': {
41                'tickCount': 4,
[82]42                'rotate': 25,
43                'label': 'Lines'
[32]44            }
45        },
46        'background': {
[81]47            'chartColor': '#ffeeff',
48            'baseColor': '#ffffff',
[32]49            'lineColor': '#444444'
50        },
[169]51        'colorScheme': {
52            'name': 'gradient',
53            'args': {
54                'initialColor': 'red',
55            },
56        },
[32]57        'legend': {
58            'hide': True,
59        },
60        'padding': {
[84]61            'left': 75,
[82]62            'bottom': 55,
[81]63        },
64        'title': 'Sample Chart'
[32]65    }
[33]66    chart = chartFactory(surface, options)
[32]67
68    chart.addDataset(dataSet)
69    chart.render()
70
71    surface.write_to_png(output)
72
73if __name__ == '__main__':
[136]74    if len(sys.argv) > 1:
75        output = sys.argv[1]
76    else:
77        output = 'barchart.png'
[33]78    barChart('v' + output, pycha.bar.VerticalBarChart)
79    barChart('h' + output, pycha.bar.HorizontalBarChart)
Note: See TracBrowser for help on using the browser.