Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/charts.py
blob: 2159fee340e6cab33021d1242f190bf579895501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# charts.py by:
#    Agustin Zubiaga <aguzubiaga97@gmail.com>
#    Gonzalo Odiard <godiard@gmail.com>
#    Manuel QuiƱones <manuq@laptop.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import pycha.bar
import pycha.line
import pycha.pie

import cairo
import gobject


class Chart(gobject.GObject):
    def __init__(self, type="vertical", width=600, height=460):
        gobject.GObject.__init__(self)

        self.dataSet = None
        self.options = None
        self.surface = None

        self.type = type
        self.width = width
        self.height = height

    def data_set(self, data):
        """Set chart data (dataSet)"""

        self.dataSet = (
            ('Puntos', [(i, l[1]) for i, l in enumerate(data)]),
            )

        self.options = {
            'legend': {'hide': True},
            'axis': {
                'x': {
                    'ticks': [dict(v=i, label=l[0]) for i,
                                                        l in enumerate(data)],
                    'label': 'X',
                },
                'y': {
                    'tickCount': 5,
                    'label': 'Y',
                }
            },
            'background': {
                'chartColor': '#FFFFFF',
                'lineColor': '#d1e5ec'
            },
            'colorScheme': {
                'name': 'gradient',
                'args': {
                    'initialColor': 'blue',
                },
            },
        }

    def set_color_scheme(self, color='blue'):
        """Set the chart color scheme"""
        self.options["colorScheme"]["args"] = {'initialColor': color}

    def set_line_color(self, color='#d1e5ec'):
        """Set the chart line color"""
        self.options["background"]["lineColor"] = color

    def set_x_label(self, text="X"):
        """Set the X Label"""
        self.options["axis"]["x"]["label"] = str(text)

    def set_y_label(self, text="Y"):
        """Set the Y Label"""
        self.options["axis"]["y"]["label"] = str(text)

    def set_type(self, type="vertical"):
        """Set chart type (vertical, horizontal, line, pie)"""
        self.type = type

    def set_title(self, title="Chart"):
        """Set the chart title"""
        self.options["title"] = title

    def render(self, sg=None):
        """Draw the chart
           Use the self.surface variable for show the chart"""
        self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                                            self.width,
                                            self.height)

        if self.type == "vbar":
            chart = pycha.bar.VerticalBarChart(self.surface, self.options)

        elif self.type == "hbar":
            chart = pycha.bar.HorizontalBarChart(self.surface, self.options)

        elif self.type == "line":
            chart = pycha.line.LineChart(self.surface, self.options)

        elif self.type == "pie":
            self.options["legend"] = {"hide": "False"}
            chart = pycha.pie.PieChart(self.surface, self.options)
            self.dataSet = [(data[0],
                            [[0, data[1]]]) for data in sg.chart_data]

        chart.addDataset(self.dataSet)
        chart.render()

    def as_png(self, file):
        """Save the chart as png image"""
        self.surface.write_to_png(file)