Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugarpycha/polygonal.py
blob: bb5ced64302c3c87343224e9a49544e6db552767 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Copyright(c) 2011 by Roberto Garcia Carvajal <roberpot@gmail.com>
#
# This file is part of PyCha.
#
# PyCha is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyCha 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.

import math

import cairo

from sugarpycha.chart import Chart
from sugarpycha.line import Point
from sugarpycha.color import hex2rgb
from sugarpycha.utils import safe_unicode


class PolygonalChart(Chart):

    def __init__(self, surface=None, options={}):
        super(PolygonalChart, self).__init__(surface, options)
        self.points = []

    def _updateChart(self):
        """Evaluates measures for polygonal charts"""
        self.points = []

        for i, (name, store) in enumerate(self.datasets):
            for item in store:
                xval, yval = item
                x = (xval - self.minxval) * self.xscale
                y = 1.0 - (yval - self.minyval) * self.yscale
                point = Point(x, y, xval, yval, name)

                if 0.0 <= point.x <= 1.0 and 0.0 <= point.y <= 1.0:
                    self.points.append(point)

    def _renderBackground(self, cx):
        """Renders the background area of the chart"""
        if self.options.background.hide:
            return

        cx.save()

        if self.options.background.baseColor:
            cx.set_source_rgb(*hex2rgb(self.options.background.baseColor))
            cx.paint()

        if self.options.background.chartColor:
            cx.set_source_rgb(*hex2rgb(self.options.background.chartColor))
            cx.set_line_width(10.0)
            cx.new_path()
            init = None
            count = len(self.xticks)
            for index, tick in enumerate(self.xticks):
                ang = math.pi / 2 - index * 2 * math.pi / count
                x = (self.layout.chart.x + self.layout.chart.w / 2
                     - math.cos(ang)
                     * min(self.layout.chart.w / 2, self.layout.chart.h / 2))
                y = (self.layout.chart.y + self.layout.chart.h / 2
                     - math.sin(ang)
                     * min(self.layout.chart.w / 2, self.layout.chart.h / 2))
                if init is None:
                    cx.move_to(x, y)
                    init = (x, y)
                else:
                    cx.line_to(x, y)
            cx.line_to(init[0], init[1])
            cx.close_path()
            cx.fill()

        if self.options.background.lineColor:
            cx.set_source_rgb(*hex2rgb(self.options.background.lineColor))
            cx.set_line_width(self.options.axis.lineWidth)
            self._renderLines(cx)

        cx.restore()

    def _renderLine(self, cx, tick, horiz):
        """Aux function for _renderLines"""

        rad = (self.layout.chart.h / 2) * (1 - tick[0])
        cx.new_path()
        init = None
        count = len(self.xticks)
        for index, tick in enumerate(self.xticks):
            ang = math.pi / 2 - index * 2 * math.pi / count
            x = (self.layout.chart.x + self.layout.chart.w / 2
                 - math.cos(ang) * rad)
            y = (self.layout.chart.y + self.layout.chart.h / 2
                 - math.sin(ang) * rad)
            if init is None:
                cx.move_to(x, y)
                init = (x, y)
            else:
                cx.line_to(x, y)
        cx.line_to(init[0], init[1])
        cx.close_path()
        cx.stroke()

    def _renderXAxis(self, cx):
        """Draws the horizontal line representing the X axis"""

        count = len(self.xticks)

        centerx = self.layout.chart.x + self.layout.chart.w / 2
        centery = self.layout.chart.y + self.layout.chart.h / 2

        for i in range(0, count):
            offset1 = i * 2 * math.pi / count
            offset = math.pi / 2 - offset1

            rad = self.layout.chart.h / 2
            (r1, r2) = (0, rad + 5)

            x1 = centerx - math.cos(offset) * r1
            x2 = centerx - math.cos(offset) * r2
            y1 = centery - math.sin(offset) * r1
            y2 = centery - math.sin(offset) * r2

            cx.new_path()
            cx.move_to(x1, y1)
            cx.line_to(x2, y2)
            cx.close_path()
            cx.stroke()

    def _renderYTick(self, cx, tick, center):
        """Aux method for _renderAxis"""

        i = tick
        tick = self.yticks[i]

        count = len(self.yticks)

        if callable(tick):
            return

        x = center[0]
        y = center[1] - i * (self.layout.chart.h / 2) / count

        cx.new_path()
        cx.move_to(x, y)
        cx.line_to(x - self.options.axis.tickSize, y)
        cx.close_path()
        cx.stroke()

        cx.select_font_face(self.options.axis.tickFont,
                            cairo.FONT_SLANT_NORMAL,
                            cairo.FONT_WEIGHT_NORMAL)
        cx.set_font_size(self.options.axis.tickFontSize)

        label = safe_unicode(tick[1], self.options.encoding)
        extents = cx.text_extents(label)
        labelWidth = extents[2]
        labelHeight = extents[3]

        if self.options.axis.y.rotate:
            radians = math.radians(self.options.axis.y.rotate)
            cx.move_to(x - self.options.axis.tickSize
                       - (labelWidth * math.cos(radians))
                       - 4,
                       y + (labelWidth * math.sin(radians))
                       + labelHeight / (2.0 / math.cos(radians)))
            cx.rotate(-radians)
            cx.show_text(label)
            cx.rotate(radians)  # this is probably faster than a save/restore
        else:
            cx.move_to(x - self.options.axis.tickSize - labelWidth - 4,
                       y + labelHeight / 2.0)
            cx.rel_move_to(0.0, -labelHeight / 2.0)
            cx.show_text(label)

        return label

    def _renderYAxis(self, cx):
        """Draws the vertical line for the Y axis"""

        centerx = self.layout.chart.x + self.layout.chart.w / 2
        centery = self.layout.chart.y + self.layout.chart.h / 2

        offset = math.pi / 2

        r1 = self.layout.chart.h / 2

        x1 = centerx - math.cos(offset) * r1
        y1 = centery - math.sin(offset) * r1

        cx.new_path()
        cx.move_to(centerx, centery)
        cx.line_to(x1, y1)
        cx.close_path()
        cx.stroke()

    def _renderAxis(self, cx):
        """Renders axis"""
        if self.options.axis.x.hide and self.options.axis.y.hide:
            return

        cx.save()
        cx.set_source_rgb(*hex2rgb(self.options.axis.lineColor))
        cx.set_line_width(self.options.axis.lineWidth)

        centerx = self.layout.chart.x + self.layout.chart.w / 2
        centery = self.layout.chart.y + self.layout.chart.h / 2

        if not self.options.axis.y.hide:
            if self.yticks:

                count = len(self.yticks)

                for i in range(0, count):
                    self._renderYTick(cx, i, (centerx, centery))

            if self.options.axis.y.label:
                self._renderYAxisLabel(cx, self.options.axis.y.label)

            self._renderYAxis(cx)

        if not self.options.axis.x.hide:
            fontAscent = cx.font_extents()[0]
            if self.xticks:

                count = len(self.xticks)

                for i in range(0, count):
                    self._renderXTick(cx, i, fontAscent, (centerx, centery))

            if self.options.axis.x.label:
                self._renderXAxisLabel(cx, self.options.axis.x.label)

            self._renderXAxis(cx)

        cx.restore()

    def _renderXTick(self, cx, i, fontAscent, center):
        tick = self.xticks[i]
        if callable(tick):
            return

        count = len(self.xticks)
        cx.select_font_face(self.options.axis.tickFont,
                            cairo.FONT_SLANT_NORMAL,
                            cairo.FONT_WEIGHT_NORMAL)
        cx.set_font_size(self.options.axis.tickFontSize)

        label = safe_unicode(tick[1], self.options.encoding)
        extents = cx.text_extents(label)
        labelWidth = extents[2]
        labelHeight = extents[3]

        x, y = center
        cx.move_to(x, y)

        if self.options.axis.x.rotate:
            radians = math.radians(self.options.axis.x.rotate)
            cx.move_to(x - (labelHeight * math.cos(radians)),
                       y + self.options.axis.tickSize
                       + (labelHeight * math.cos(radians))
                       + 4.0)
            cx.rotate(radians)
            cx.show_text(label)
            cx.rotate(-radians)
        else:
            offset1 = i * 2 * math.pi / count
            offset = math.pi / 2 - offset1

            rad = self.layout.chart.h / 2 + 10

            x = center[0] - math.cos(offset) * rad
            y = center[1] - math.sin(offset) * rad

            cx.move_to(x, y)
            cx.rotate(offset - math.pi / 2)

            if math.sin(offset) < 0.0:
                cx.rotate(math.pi)
                cx.rel_move_to(0.0, 5.0)

            cx.rel_move_to(-labelWidth / 2.0, 0)
            cx.show_text(label)
            if math.sin(offset) < 0.0:
                cx.rotate(-math.pi)

            cx.rotate(-(offset - math.pi / 2))
        return label

    def _renderChart(self, cx):
        """Renders a polygonal chart"""
        # draw the polygon.
        def preparePath(storeName):
            cx.new_path()
            firstPoint = True

            count = len(self.points) / len(self.datasets)
            centerx = self.layout.chart.x + self.layout.chart.w / 2
            centery = self.layout.chart.y + self.layout.chart.h / 2

            firstPointCoord = None

            for index, point in enumerate(self.points):
                if point.name == storeName:
                    offset1 = index * 2 * math.pi / count
                    offset = math.pi / 2 - offset1

                    rad = (self.layout.chart.h / 2) * (1 - point.y)

                    x = centerx - math.cos(offset) * rad
                    y = centery - math.sin(offset) * rad

                    if firstPointCoord is None:
                        firstPointCoord = (x, y)

                    if not self.options.shouldFill and firstPoint:
                        # starts the first point of the line
                        cx.move_to(x, y)
                        firstPoint = False
                        continue
                    cx.line_to(x, y)

            if not firstPointCoord is None:
                cx.line_to(firstPointCoord[0], firstPointCoord[1])

            if self.options.shouldFill:
                # Close the path to the start point
                y = ((1.0 - self.origin)
                     * self.layout.chart.h + self.layout.chart.y)
            else:
                cx.set_source_rgb(*self.colorScheme[storeName])
                cx.stroke()

        cx.save()
        cx.set_line_width(self.options.stroke.width)
        if self.options.shouldFill:

            def drawLine(storeName):
                if self.options.stroke.shadow:
                    # draw shadow
                    cx.save()
                    cx.set_source_rgba(0, 0, 0, 0.15)
                    cx.translate(2, -2)
                    preparePath(storeName)
                    cx.fill()
                    cx.restore()

                # fill the line
                cx.set_source_rgb(*self.colorScheme[storeName])
                preparePath(storeName)
                cx.fill()

                if not self.options.stroke.hide:
                    # draw stroke
                    cx.set_source_rgb(*hex2rgb(self.options.stroke.color))
                    preparePath(storeName)
                    cx.stroke()

            # draw the lines
            for key in self._getDatasetsKeys():
                drawLine(key)
        else:
            for key in self._getDatasetsKeys():
                preparePath(key)
        cx.restore()