Merge lp:~magnun-leno/cairoplot/series into lp:cairoplot
- series
- Merge into trunk
Proposed by
Magnun Leno
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~magnun-leno/cairoplot/series |
Merge into: | lp:cairoplot |
Diff against target: | None lines |
To merge this branch: | bzr merge lp:~magnun-leno/cairoplot/series |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Rodrigo Moreira Araújo | Pending | ||
Review via email: mp+6090@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Magnun Leno (magnun-leno) wrote : | # |
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file 'trunk/Series.py' |
2 | --- trunk/Series.py 1970-01-01 00:00:00 +0000 |
3 | +++ trunk/Series.py 2009-05-01 14:45:26 +0000 |
4 | @@ -0,0 +1,1141 @@ |
5 | +#!/usr/bin/env python |
6 | +# -*- coding: utf-8 -*- |
7 | + |
8 | +# Serie.py |
9 | +# |
10 | +# Copyright (c) 2008 Magnun Leno da Silva |
11 | +# |
12 | +# Author: Magnun Leno da Silva <magnun.leno@gmail.com> |
13 | +# |
14 | +# This program is free software; you can redistribute it and/or |
15 | +# modify it under the terms of the GNU Lesser General Public License |
16 | +# as published by the Free Software Foundation; either version 2 of |
17 | +# the License, or (at your option) any later version. |
18 | +# |
19 | +# This program is distributed in the hope that it will be useful, |
20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | +# GNU General Public License for more details. |
23 | +# |
24 | +# You should have received a copy of the GNU Lesser General Public |
25 | +# License along with this program; if not, write to the Free Software |
26 | +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
27 | +# USA |
28 | + |
29 | +# Contributor: Rodrigo Moreiro Araujo <alf.rodrigo@gmail.com> |
30 | + |
31 | +#import cairoplot |
32 | +import doctest |
33 | + |
34 | +NUMTYPES = (int, float, long) |
35 | +LISTTYPES = (list, tuple) |
36 | +STRTYPES = (str, unicode) |
37 | +FILLING_TYPES = ['linear', 'solid', 'gradient'] |
38 | +DEFAULT_COLOR_FILLING = 'solid' |
39 | +#TODO: Define default color list |
40 | +DEFAULT_COLOR_LIST = None |
41 | + |
42 | +class Data(object): |
43 | + ''' |
44 | + Class that models the main data structure. |
45 | + It can hold: |
46 | + - a number type (int, float or long) |
47 | + - a tuple, witch represents a point and can have 2 or 3 items (x,y,z) |
48 | + - if passed a list it will be converted to a tuple. |
49 | + |
50 | + obs: In case is passed a tuple it will convert to tuple |
51 | + ''' |
52 | + def __init__(self, data=None, name=None, parent=None): |
53 | + ''' |
54 | + Starts main atributes from the Data class |
55 | + @name - Name for each point; |
56 | + @content - The real data, can be an int, float, long or tuple, which |
57 | + represents a point (x,y,z); |
58 | + @parent - A pointer that give the data access to it's parent. |
59 | + |
60 | + Usage: |
61 | + >>> d = Data(name='empty'); print d |
62 | + empty: () |
63 | + >>> d = Data((1,1),'point a'); print d |
64 | + point a: (1, 1) |
65 | + >>> d = Data((1,2,3),'point b'); print d |
66 | + point b: (1, 2, 3) |
67 | + >>> d = Data([2,3],'point c'); print d |
68 | + point c: (2, 3) |
69 | + >>> d = Data(12, 'simple value'); print d |
70 | + simple value: 12 |
71 | + ''' |
72 | + # Initial values |
73 | + self.__content = None |
74 | + self.__name = None |
75 | + |
76 | + # Setting passed values |
77 | + self.parent = parent |
78 | + self.name = name |
79 | + self.content = data |
80 | + |
81 | + # Name property |
82 | + @apply |
83 | + def name(): |
84 | + doc = ''' |
85 | + Name is a read/write property that controls the input of name. |
86 | + - If passed an invalid value it cleans the name with None |
87 | + |
88 | + Usage: |
89 | + >>> d = Data(13); d.name = 'name_test'; print d |
90 | + name_test: 13 |
91 | + >>> d.name = 11; print d |
92 | + 13 |
93 | + >>> d.name = 'other_name'; print d |
94 | + other_name: 13 |
95 | + >>> d.name = None; print d |
96 | + 13 |
97 | + >>> d.name = 'last_name'; print d |
98 | + last_name: 13 |
99 | + >>> d.name = ''; print d |
100 | + 13 |
101 | + ''' |
102 | + def fget(self): |
103 | + ''' |
104 | + returns the name as a string |
105 | + ''' |
106 | + return self.__name |
107 | + |
108 | + def fset(self, name): |
109 | + ''' |
110 | + Sets the name of the Data |
111 | + ''' |
112 | + if type(name) in STRTYPES and len(name) > 0: |
113 | + self.__name = name |
114 | + else: |
115 | + self.__name = None |
116 | + |
117 | + |
118 | + |
119 | + return property(**locals()) |
120 | + |
121 | + # Content property |
122 | + @apply |
123 | + def content(): |
124 | + doc = ''' |
125 | + Content is a read/write property that validate the data passed |
126 | + and return it. |
127 | + |
128 | + Usage: |
129 | + >>> d = Data(); d.content = 13; d.content |
130 | + 13 |
131 | + >>> d = Data(); d.content = (1,2); d.content |
132 | + (1, 2) |
133 | + >>> d = Data(); d.content = (1,2,3); d.content |
134 | + (1, 2, 3) |
135 | + >>> d = Data(); d.content = [1,2,3]; d.content |
136 | + (1, 2, 3) |
137 | + >>> d = Data(); d.content = [1.5,.2,3.3]; d.content |
138 | + (1.5, 0.20000000000000001, 3.2999999999999998) |
139 | + ''' |
140 | + def fget(self): |
141 | + ''' |
142 | + Return the content of Data |
143 | + ''' |
144 | + return self.__content |
145 | + |
146 | + def fset(self, data): |
147 | + ''' |
148 | + Ensures that data is a valid tuple/list or a number (int, float |
149 | + or long) |
150 | + ''' |
151 | + # Type: None |
152 | + if data is None: |
153 | + self.__content = None |
154 | + return |
155 | + |
156 | + # Type: Int or Float |
157 | + elif type(data) in NUMTYPES: |
158 | + self.__content = data |
159 | + |
160 | + # Type: List or Tuple |
161 | + elif type(data) in LISTTYPES: |
162 | + # Ensures the correct size |
163 | + if len(data) not in (2, 3): |
164 | + raise TypeError, "Data (as list/tuple) must have 2 or 3 items" |
165 | + return |
166 | + |
167 | + # Ensures that all items in list/tuple is a number |
168 | + isnum = lambda x : type(x) not in NUMTYPES |
169 | + |
170 | + if max(map(isnum, data)): |
171 | + # An item in data isn't an int or a float |
172 | + raise TypeError, "All content of data must be a number (int or float)" |
173 | + |
174 | + # Convert the tuple to list |
175 | + if type(data) is list: |
176 | + data = tuple(data) |
177 | + |
178 | + # Append a copy and sets the type |
179 | + self.__content = data[:] |
180 | + |
181 | + # Unknown type! |
182 | + else: |
183 | + self.__content = None |
184 | + raise TypeError, "Data must be an int, float or a tuple with two or three items" |
185 | + return |
186 | + |
187 | + return property(**locals()) |
188 | + |
189 | + |
190 | + def clear(self): |
191 | + ''' |
192 | + Clear the all Data (content, name and parent) |
193 | + ''' |
194 | + self.content = None |
195 | + self.name = None |
196 | + self.parent = None |
197 | + |
198 | + def copy(self): |
199 | + ''' |
200 | + Returns a copy of the Data structure |
201 | + ''' |
202 | + # The copy |
203 | + new_data = Data() |
204 | + if self.content is not None: |
205 | + # If content is a point |
206 | + if type(self.content) is tuple: |
207 | + new_data.__content = self.content[:] |
208 | + |
209 | + # If content is a number |
210 | + else: |
211 | + new_data.__content = self.content |
212 | + |
213 | + # If it has a name |
214 | + if self.name is not None: |
215 | + new_data.__name = self.name |
216 | + |
217 | + return new_data |
218 | + |
219 | + def __str__(self): |
220 | + ''' |
221 | + Return a string representation of the Data structure |
222 | + ''' |
223 | + if self.name is None: |
224 | + if self.content is None: |
225 | + return '' |
226 | + return str(self.content) |
227 | + else: |
228 | + if self.content is None: |
229 | + return self.name+": ()" |
230 | + return self.name+": "+str(self.content) |
231 | + |
232 | + def __len__(self): |
233 | + ''' |
234 | + Return the length of the Data. |
235 | + - If it's a number return 1; |
236 | + - If it's a list return it's length; |
237 | + - If its None return 0. |
238 | + ''' |
239 | + if self.content is None: |
240 | + return 0 |
241 | + elif type(self.content) in NUMTYPES: |
242 | + return 1 |
243 | + return len(self.content) |
244 | + |
245 | + |
246 | + |
247 | + |
248 | +class Group(object): |
249 | + ''' |
250 | + Class that moodels a group of data. Every value (int, float, long, tuple |
251 | + or list) passed is converted to a list of Data. |
252 | + It can receive: |
253 | + - A single number (int, float, long); |
254 | + - A list of numbers; |
255 | + - A tuple of numbers; |
256 | + - An instance of Data; |
257 | + - A list of Data; |
258 | + |
259 | + Obs: If a tuple with 2 or 3 items is passed it is converted to a point. |
260 | + If a tuple with only 1 item is passed it's converted to a number; |
261 | + If a tuple with more then 2 items is passed it's converted to a |
262 | + list of numbers |
263 | + ''' |
264 | + def __init__(self, group=None, name=None, parent=None): |
265 | + ''' |
266 | + Starts main atributes in Group instance. |
267 | + @data_list - a list of data witch compound the group; |
268 | + @range - a range that represent the x axis of possible functions; |
269 | + @name - name of the grouping of data; |
270 | + @parent - the Serie parent of this group. |
271 | + |
272 | + Usage: |
273 | + >>> g = Group(13, 'simple number'); print g |
274 | + simple number ['13'] |
275 | + >>> g = Group((1,2), 'simple point'); print g |
276 | + simple point ['(1, 2)'] |
277 | + >>> g = Group([1,2,3,4], 'list of numbers'); print g |
278 | + list of numbers ['1', '2', '3', '4'] |
279 | + >>> g = Group((1,2,3,4),'int in tuple'); print g |
280 | + int in tuple ['1', '2', '3', '4'] |
281 | + >>> g = Group([(1,2),(2,3),(3,4)], 'list of points'); print g |
282 | + list of points ['(1, 2)', '(2, 3)', '(3, 4)'] |
283 | + >>> g = Group([[1,2,3],[1,2,3]], '2D coordinated lists'); print g |
284 | + 2D coordinated lists ['(1, 1)', '(2, 2)', '(3, 3)'] |
285 | + >>> g = Group([[1,2],[1,2],[1,2]], '3D coordinated lists'); print g |
286 | + 3D coordinated lists ['(1, 1, 1)', '(2, 2, 2)'] |
287 | + ''' |
288 | + # Initial values |
289 | + self.__data_list = [] |
290 | + self.__range = [] |
291 | + self.__name = None |
292 | + |
293 | + |
294 | + self.parent = parent |
295 | + self.name = name |
296 | + self.data_list = group |
297 | + |
298 | + # Name property |
299 | + @apply |
300 | + def name(): |
301 | + doc = ''' |
302 | + Name is a read/write property that controls the input of name. |
303 | + - If passed an invalid value it cleans the name with None |
304 | + |
305 | + Usage: |
306 | + >>> g = Group(13); g.name = 'name_test'; print g |
307 | + name_test ['13'] |
308 | + >>> g.name = 11; print g |
309 | + ['13'] |
310 | + >>> g.name = 'other_name'; print g |
311 | + other_name ['13'] |
312 | + >>> g.name = None; print g |
313 | + ['13'] |
314 | + >>> g.name = 'last_name'; print g |
315 | + last_name ['13'] |
316 | + >>> g.name = ''; print g |
317 | + ['13'] |
318 | + ''' |
319 | + def fget(self): |
320 | + ''' |
321 | + Returns the name as a string |
322 | + ''' |
323 | + return self.__name |
324 | + |
325 | + def fset(self, name): |
326 | + ''' |
327 | + Sets the name of the Group |
328 | + ''' |
329 | + if type(name) in STRTYPES and len(name) > 0: |
330 | + self.__name = name |
331 | + else: |
332 | + self.__name = None |
333 | + |
334 | + return property(**locals()) |
335 | + |
336 | + # data_list property |
337 | + @apply |
338 | + def data_list(): |
339 | + doc = ''' |
340 | + The data_list is a read/write property that can be a list of |
341 | + numbers, a list of points or a list of coordinated lists. This |
342 | + property use mainly the self.add_data method. |
343 | + |
344 | + Usage: |
345 | + >>> g = Group(); g.data_list = 13; print g |
346 | + ['13'] |
347 | + >>> g.data_list = (1,2); print g |
348 | + ['(1, 2)'] |
349 | + >>> g.data_list = Data((1,2),'point a'); print g |
350 | + ['point a: (1, 2)'] |
351 | + >>> g.data_list = [1,2,3]; print g |
352 | + ['1', '2', '3'] |
353 | + >>> g.data_list = (1,2,3,4); print g |
354 | + ['1', '2', '3', '4'] |
355 | + >>> g.data_list = [(1,2),(2,3),(3,4)]; print g |
356 | + ['(1, 2)', '(2, 3)', '(3, 4)'] |
357 | + >>> g.data_list = [[1,2],[1,2]]; print g |
358 | + ['(1, 1)', '(2, 2)'] |
359 | + >>> g.data_list = [[1,2],[1,2],[1,2]]; print g |
360 | + ['(1, 1, 1)', '(2, 2, 2)'] |
361 | + >>> g.range = (10); g.data_list = lambda x:x**2; print g |
362 | + ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 4.0)', '(3.0, 9.0)', '(4.0, 16.0)', '(5.0, 25.0)', '(6.0, 36.0)', '(7.0, 49.0)', '(8.0, 64.0)', '(9.0, 81.0)'] |
363 | + ''' |
364 | + def fget(self): |
365 | + ''' |
366 | + Returns the value of data_list |
367 | + ''' |
368 | + return self.__data_list |
369 | + |
370 | + def fset(self, group): |
371 | + ''' |
372 | + Ensures that group is valid. |
373 | + ''' |
374 | + # None |
375 | + if group is None: |
376 | + self.__data_list = [] |
377 | + |
378 | + # Int/float/long or Instance of Data |
379 | + elif type(group) in NUMTYPES or isinstance(group, Data): |
380 | + # Clean data_list |
381 | + self.__data_list = [] |
382 | + self.add_data(group) |
383 | + |
384 | + # One point |
385 | + elif type(group) is tuple and len(group) in (2,3): |
386 | + self.__data_list = [] |
387 | + self.add_data(group) |
388 | + |
389 | + # list of items |
390 | + elif type(group) in LISTTYPES and type(group[0]) is not list: |
391 | + # Clean data_list |
392 | + self.__data_list = [] |
393 | + for item in group: |
394 | + # try to append and catch an exception |
395 | + self.add_data(item) |
396 | + |
397 | + # function lambda |
398 | + elif callable(group): |
399 | + # Explicit is better than implicit |
400 | + function = group |
401 | + # Have range |
402 | + if len(self.range) is not 0: |
403 | + # Clean data_list |
404 | + self.__data_list = [] |
405 | + # Generate values for the lambda function |
406 | + for x in self.range: |
407 | + #self.add_data((x,round(group(x),2))) |
408 | + self.add_data((x,function(x))) |
409 | + |
410 | + # Only have range in parent |
411 | + elif self.parent is not None and len(self.parent.range) is not 0: |
412 | + # Copy parent range |
413 | + self.__range = self.parent.range[:] |
414 | + # Clean data_list |
415 | + self.__data_list = [] |
416 | + # Generate values for the lambda function |
417 | + for x in self.range: |
418 | + #self.add_data((x,round(group(x),2))) |
419 | + self.add_data((x,function(x))) |
420 | + |
421 | + # Don't have range anywhere |
422 | + else: |
423 | + # x_data don't exist |
424 | + raise Exception, "Data argument is valid but to use function type please set x_range first" |
425 | + |
426 | + # Coordinated Lists |
427 | + elif type(group) in LISTTYPES and type(group[0]) is list: |
428 | + # Clean data_list |
429 | + self.__data_list = [] |
430 | + data = [] |
431 | + if len(group) == 3: |
432 | + data = [ (group[0][i], group[1][i], group[2][i]) for i in range(len(group[0])) ] |
433 | + elif len(group) == 2: |
434 | + data = [ (group[0][i], group[1][i]) for i in range(len(group[0])) ] |
435 | + else: |
436 | + raise TypeError, "Only one list of coordinates was received." |
437 | + |
438 | + for item in data: |
439 | + self.add_data(item) |
440 | + |
441 | + else: |
442 | + raise TypeError, "Group type not supported" |
443 | + |
444 | + return property(**locals()) |
445 | + |
446 | + @apply |
447 | + def range(): |
448 | + doc = ''' |
449 | + The range is a read/write property that generates a range of values |
450 | + for the x axis of the functions. When passed a tuple it almost works |
451 | + like the buil-in range funtion: |
452 | + - 1 item, represent the end of the range started from 0; |
453 | + - 2 items, represents the start and the end, respectively; |
454 | + - 3 items, the last one represents the step; |
455 | + |
456 | + When passed a list the range function understands as a valid range. |
457 | + |
458 | + Usage: |
459 | + >>> g = Group(); g.range = 10; print g.range |
460 | + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] |
461 | + >>> g = Group(); g.range = (5); print g.range |
462 | + [0.0, 1.0, 2.0, 3.0, 4.0] |
463 | + >>> g = Group(); g.range = (1,7); print g.range |
464 | + [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] |
465 | + >>> g = Group(); g.range = (0,10,2); print g.range |
466 | + [0.0, 2.0, 4.0, 6.0, 8.0] |
467 | + >>> |
468 | + >>> g = Group(); g.range = [0]; print g.range |
469 | + [0.0] |
470 | + >>> g = Group(); g.range = [0,10,20]; print g.range |
471 | + [0.0, 10.0, 20.0] |
472 | + ''' |
473 | + def fget(self): |
474 | + ''' |
475 | + Returns the range |
476 | + ''' |
477 | + return self.__range |
478 | + |
479 | + def fset(self, x_range): |
480 | + ''' |
481 | + Controls the input of a valid type and generate the range |
482 | + ''' |
483 | + # if passed a simple number convert to tuple |
484 | + if type(x_range) in NUMTYPES: |
485 | + x_range = (x_range,) |
486 | + |
487 | + # A list, just convert to float |
488 | + if type(x_range) is list and len(x_range) > 0: |
489 | + # Convert all to float |
490 | + x_range = map(float, x_range) |
491 | + # Prevents repeated values and convert back to list |
492 | + self.__range = list(set(x_range[:])) |
493 | + # Sort the list to ascending order |
494 | + self.__range.sort() |
495 | + |
496 | + # A tuple, must check the lengths and generate the values |
497 | + elif type(x_range) is tuple and len(x_range) in (1,2,3): |
498 | + # Convert all to float |
499 | + x_range = map(float, x_range) |
500 | + |
501 | + # Inital values |
502 | + start = 0.0 |
503 | + step = 1.0 |
504 | + end = 0.0 |
505 | + |
506 | + # Only the end and it can't be less or iqual to 0 |
507 | + if len(x_range) is 1 and x_range > 0: |
508 | + end = x_range[0] |
509 | + |
510 | + # The start and the end but the start must be lesser then the end |
511 | + elif len(x_range) is 2 and x_range[0] < x_range[1]: |
512 | + start = x_range[0] |
513 | + end = x_range[1] |
514 | + |
515 | + # All 3, but the start must be lesser then the end |
516 | + elif x_range[0] <= x_range[1]: |
517 | + start = x_range[0] |
518 | + end = x_range[1] |
519 | + step = x_range[2] |
520 | + |
521 | + # Starts the range |
522 | + self.__range = [] |
523 | + # Generate the range |
524 | + # Cnat use the range function becouse it don't suport float values |
525 | + while start < end: |
526 | + self.__range.append(start) |
527 | + start += step |
528 | + |
529 | + # Incorrect type |
530 | + else: |
531 | + raise Exception, "x_range must be a list with one or more item or a tuple with 2 or 3 items" |
532 | + |
533 | + return property(**locals()) |
534 | + |
535 | + def add_data(self, data, name=None): |
536 | + ''' |
537 | + Append a new data to the data_list. |
538 | + - If data is an instance of Data, append it |
539 | + - If it's an int, float, tuple or list create an instance of Data and append it |
540 | + |
541 | + Usage: |
542 | + >>> g = Group() |
543 | + >>> g.add_data(12); print g |
544 | + ['12'] |
545 | + >>> g.add_data(7,'other'); print g |
546 | + ['12', 'other: 7'] |
547 | + >>> |
548 | + >>> g = Group() |
549 | + >>> g.add_data((1,1),'a'); print g |
550 | + ['a: (1, 1)'] |
551 | + >>> g.add_data((2,2),'b'); print g |
552 | + ['a: (1, 1)', 'b: (2, 2)'] |
553 | + >>> |
554 | + >>> g.add_data(Data((1,2),'c')); print g |
555 | + ['a: (1, 1)', 'b: (2, 2)', 'c: (1, 2)'] |
556 | + ''' |
557 | + if not isinstance(data, Data): |
558 | + # Try to convert |
559 | + data = Data(data,name,self) |
560 | + |
561 | + if data.content is not None: |
562 | + self.__data_list.append(data.copy()) |
563 | + self.__data_list[-1].parent = self |
564 | + |
565 | + |
566 | + def to_list(self): |
567 | + ''' |
568 | + Returns the group as a list of numbers (int, float or long) or a |
569 | + list of tuples (points 2D or 3D). |
570 | + |
571 | + Usage: |
572 | + >>> g = Group([1,2,3,4],'g1'); g.to_list() |
573 | + [1, 2, 3, 4] |
574 | + >>> g = Group([(1,2),(2,3),(3,4)],'g2'); g.to_list() |
575 | + [(1, 2), (2, 3), (3, 4)] |
576 | + >>> g = Group([(1,2,3),(3,4,5)],'g2'); g.to_list() |
577 | + [(1, 2, 3), (3, 4, 5)] |
578 | + ''' |
579 | + return [data.content for data in self] |
580 | + |
581 | + def copy(self): |
582 | + ''' |
583 | + Returns a copy of this group |
584 | + ''' |
585 | + new_group = Group() |
586 | + new_group.__name = self.__name |
587 | + if self.__range is not None: |
588 | + new_group.__range = self.__range[:] |
589 | + for data in self: |
590 | + new_group.add_data(data.copy()) |
591 | + return new_group |
592 | + |
593 | + def get_names(self): |
594 | + ''' |
595 | + Return a list with the names of all data in this group |
596 | + ''' |
597 | + names = [] |
598 | + for data in self: |
599 | + if data.name is None: |
600 | + names.append('Data '+str(data.index()+1)) |
601 | + else: |
602 | + names.append(data.name) |
603 | + return names |
604 | + |
605 | + |
606 | + def __str__ (self): |
607 | + ''' |
608 | + Returns a string representing the Group |
609 | + ''' |
610 | + ret = "" |
611 | + if self.name is not None: |
612 | + ret += self.name + " " |
613 | + if len(self) > 0: |
614 | + list_str = [str(item) for item in self] |
615 | + ret += str(list_str) |
616 | + else: |
617 | + ret += "[]" |
618 | + return ret |
619 | + |
620 | + def __getitem__(self, key): |
621 | + ''' |
622 | + Makes a Group iterable, based in the data_list property |
623 | + ''' |
624 | + return self.data_list[key] |
625 | + |
626 | + def __len__(self): |
627 | + ''' |
628 | + Returns the length of the Group, based in the data_list property |
629 | + ''' |
630 | + return len(self.data_list) |
631 | + |
632 | + |
633 | +class Colors(object): |
634 | + ''' |
635 | + Class that models the colors its labels (names) and its properties, RGB |
636 | + and filling type. |
637 | + |
638 | + It can receive: |
639 | + - A list where each item is a list with 3 or 4 items. The |
640 | + first 3 item represents the colors RGB and the last argument |
641 | + defines the filling type. The list will be converted to a dict |
642 | + and each color will receve a name based in its position in the |
643 | + list. |
644 | + - A dictionary where each key will be the color name and its item |
645 | + can be a list with 3 or 4 items. The first 3 item represents |
646 | + the colors RGB and the last argument defines the filling type. |
647 | + ''' |
648 | + def __init__(self, color_list=None): |
649 | + ''' |
650 | + Start the color_list property |
651 | + @ color_list |
652 | + is the list or dict contaning the colors properties. |
653 | + ''' |
654 | + self.__color_list = None |
655 | + |
656 | + self.color_list = color_list |
657 | + |
658 | + @apply |
659 | + def color_list(): |
660 | + doc = ''' |
661 | + >>> c = Colors([[1,1,1],[2,2,2,'linear'],[3,3,3,'gradient']]) |
662 | + >>> print c.color_list |
663 | + {'Color 2': [2, 2, 2, 'linear'], 'Color 3': [3, 3, 3, 'gradient'], 'Color 1': [1, 1, 1, 'solid']} |
664 | + >>> c.color_list = [[1,1,1],(2,2,2,'solid'),(3,3,3,'linear')] |
665 | + >>> print c.color_list |
666 | + {'Color 2': [2, 2, 2, 'solid'], 'Color 3': [3, 3, 3, 'linear'], 'Color 1': [1, 1, 1, 'solid']} |
667 | + >>> c.color_list = {'a':[1,1,1],'b':(2,2,2,'solid'),'c':(3,3,3,'linear'), 'd':(4,4,4)} |
668 | + >>> print c.color_list |
669 | + {'a': [1, 1, 1, 'solid'], 'c': [3, 3, 3, 'linear'], 'b': [2, 2, 2, 'solid'], 'd': [4, 4, 4, 'solid']} |
670 | + ''' |
671 | + def fget(self): |
672 | + ''' |
673 | + Return the color list |
674 | + ''' |
675 | + return self.__color_list |
676 | + |
677 | + def fset(self, color_list): |
678 | + ''' |
679 | + Format the color list to a dictionary |
680 | + ''' |
681 | + if color_list is None: |
682 | + self.__color_list = None |
683 | + return |
684 | + |
685 | + if type(color_list) in LISTTYPES and type(color_list[0]) in LISTTYPES: |
686 | + old_color_list = color_list[:] |
687 | + color_list = {} |
688 | + for index, color in enumerate(old_color_list): |
689 | + if len(color) is 3 and max(map(type, color)) in NUMTYPES: |
690 | + color_list['Color '+str(index+1)] = list(color)+[DEFAULT_COLOR_FILLING] |
691 | + elif len(color) is 4 and max(map(type, color[:-1])) in NUMTYPES and color[-1] in FILLING_TYPES: |
692 | + color_list['Color '+str(index+1)] = list(color) |
693 | + else: |
694 | + raise TypeError, "Unsuported color format" |
695 | + elif type(color_list) is not dict: |
696 | + raise TypeError, "Unsuported color format" |
697 | + |
698 | + for name, color in color_list.items(): |
699 | + if len(color) is 3: |
700 | + if max(map(type, color)) in NUMTYPES: |
701 | + color_list[name] = list(color)+[DEFAULT_COLOR_FILLING] |
702 | + else: |
703 | + raise TypeError, "Unsuported color format" |
704 | + elif len(color) is 4: |
705 | + if max(map(type, color[:-1])) in NUMTYPES and color[-1] in FILLING_TYPES: |
706 | + color_list[name] = list(color) |
707 | + else: |
708 | + raise TypeError, "Unsuported color format" |
709 | + self.__color_list = color_list.copy() |
710 | + |
711 | + return property(**locals()) |
712 | + |
713 | + |
714 | +class Serie(object): |
715 | + ''' |
716 | + Class that models a Serie (group of groups). Every value (int, float, |
717 | + long, tuple or list) passed is converted to a list of Group or Data. |
718 | + It can receive: |
719 | + - a single number or point, will be converted to a Group of one Data; |
720 | + - a list of numbers, will be converted to a group of numbers; |
721 | + - a list of tuples, will converted to a single Group of points; |
722 | + - a list of lists of numbers, each 'sublist' will be converted to a |
723 | + group of numbers; |
724 | + - a list of lists of tuples, each 'sublist' will be converted to a |
725 | + group of points; |
726 | + - a list of lists of lists, the content of the 'sublist' will be |
727 | + processed as coordinated lists and the result will be converted to |
728 | + a group of points; |
729 | + - a Dictionary where each item can be the same of the list: number, |
730 | + point, list of numbers, list of points or list of lists (coordinated |
731 | + lists); |
732 | + - an instance of Data; |
733 | + - an instance of group. |
734 | + ''' |
735 | + def __init__(self, serie=None, name=None, property=[], colors=None): |
736 | + ''' |
737 | + Starts main atributes in Group instance. |
738 | + @serie - a list, dict of data witch compound the serie; |
739 | + @name - name of the serie; |
740 | + @property - a list/dict of properties to be used in the plots of |
741 | + this Serie |
742 | + |
743 | + Usage: |
744 | + >>> print Serie([1,2,3,4]) |
745 | + ["Group 1 ['1', '2', '3', '4']"] |
746 | + >>> print Serie([[1,2,3],[4,5,6]]) |
747 | + ["Group 1 ['1', '2', '3']", "Group 2 ['4', '5', '6']"] |
748 | + >>> print Serie((1,2)) |
749 | + ["Group 1 ['(1, 2)']"] |
750 | + >>> print Serie([(1,2),(2,3)]) |
751 | + ["Group 1 ['(1, 2)', '(2, 3)']"] |
752 | + >>> print Serie([[(1,2),(2,3)],[(4,5),(5,6)]]) |
753 | + ["Group 1 ['(1, 2)', '(2, 3)']", "Group 2 ['(4, 5)', '(5, 6)']"] |
754 | + >>> print Serie([[[1,2,3],[1,2,3],[1,2,3]]]) |
755 | + ["Group 1 ['(1, 1, 1)', '(2, 2, 2)', '(3, 3, 3)']"] |
756 | + >>> print Serie({'g1':[1,2,3], 'g2':[4,5,6]}) |
757 | + ["g1 ['1', '2', '3']", "g2 ['4', '5', '6']"] |
758 | + >>> print Serie({'g1':[(1,2),(2,3)], 'g2':[(4,5),(5,6)]}) |
759 | + ["g1 ['(1, 2)', '(2, 3)']", "g2 ['(4, 5)', '(5, 6)']"] |
760 | + >>> print Serie({'g1':[[1,2],[1,2]], 'g2':[[4,5],[4,5]]}) |
761 | + ["g1 ['(1, 1)', '(2, 2)']", "g2 ['(4, 4)', '(5, 5)']"] |
762 | + >>> print Serie(Data(1,'d1')) |
763 | + ["Group 1 ['d1: 1']"] |
764 | + >>> print Serie(Group([(1,2),(2,3)],'g1')) |
765 | + ["g1 ['(1, 2)', '(2, 3)']"] |
766 | + ''' |
767 | + # Intial values |
768 | + self.__group_list = [] |
769 | + self.__name = None |
770 | + self.__range = None |
771 | + |
772 | + # TODO: Implement colors with filling |
773 | + self.__colors = None |
774 | + |
775 | + self.name = name |
776 | + self.group_list = serie |
777 | + self.colors = colors |
778 | + |
779 | + # Name property |
780 | + @apply |
781 | + def name(): |
782 | + doc = ''' |
783 | + Name is a read/write property that controls the input of name. |
784 | + - If passed an invalid value it cleans the name with None |
785 | + |
786 | + Usage: |
787 | + >>> s = Serie(13); s.name = 'name_test'; print s |
788 | + name_test ["Group 1 ['13']"] |
789 | + >>> s.name = 11; print s |
790 | + ["Group 1 ['13']"] |
791 | + >>> s.name = 'other_name'; print s |
792 | + other_name ["Group 1 ['13']"] |
793 | + >>> s.name = None; print s |
794 | + ["Group 1 ['13']"] |
795 | + >>> s.name = 'last_name'; print s |
796 | + last_name ["Group 1 ['13']"] |
797 | + >>> s.name = ''; print s |
798 | + ["Group 1 ['13']"] |
799 | + ''' |
800 | + def fget(self): |
801 | + ''' |
802 | + Returns the name as a string |
803 | + ''' |
804 | + return self.__name |
805 | + |
806 | + def fset(self, name): |
807 | + ''' |
808 | + Sets the name of the Group |
809 | + ''' |
810 | + if type(name) in STRTYPES and len(name) > 0: |
811 | + self.__name = name |
812 | + else: |
813 | + self.__name = None |
814 | + |
815 | + return property(**locals()) |
816 | + |
817 | + |
818 | + |
819 | + # Colors property |
820 | + @apply |
821 | + def colors(): |
822 | + doc = ''' |
823 | + >>> s = Serie() |
824 | + >>> s.colors = [[1,1,1],[2,2,2,'linear'],[3,3,3,'gradient']] |
825 | + >>> print s.colors |
826 | + {'Color 2': [2, 2, 2, 'linear'], 'Color 3': [3, 3, 3, 'gradient'], 'Color 1': [1, 1, 1, 'solid']} |
827 | + >>> s.colors = [[1,1,1],(2,2,2,'solid'),(3,3,3,'linear')] |
828 | + >>> print s.colors |
829 | + {'Color 2': [2, 2, 2, 'solid'], 'Color 3': [3, 3, 3, 'linear'], 'Color 1': [1, 1, 1, 'solid']} |
830 | + >>> s.colors = {'a':[1,1,1],'b':(2,2,2,'solid'),'c':(3,3,3,'linear'), 'd':(4,4,4)} |
831 | + >>> print s.colors |
832 | + {'a': [1, 1, 1, 'solid'], 'c': [3, 3, 3, 'linear'], 'b': [2, 2, 2, 'solid'], 'd': [4, 4, 4, 'solid']} |
833 | + ''' |
834 | + def fget(self): |
835 | + ''' |
836 | + Return the color list |
837 | + ''' |
838 | + return self.__colors.color_list |
839 | + |
840 | + def fset(self, colors): |
841 | + ''' |
842 | + Format the color list to a dictionary |
843 | + ''' |
844 | + self.__colors = Colors(colors) |
845 | + |
846 | + return property(**locals()) |
847 | + |
848 | + @apply |
849 | + def range(): |
850 | + doc = ''' |
851 | + The range is a read/write property that generates a range of values |
852 | + for the x axis of the functions. When passed a tuple it almost works |
853 | + like the buil-in range funtion: |
854 | + - 1 item, represent the end of the range started from 0; |
855 | + - 2 items, represents the start and the end, respectively; |
856 | + - 3 items, the last one represents the step; |
857 | + |
858 | + When passed a list the range function understands as a valid range. |
859 | + |
860 | + Usage: |
861 | + >>> s = Serie(); s.range = 10; print s.range |
862 | + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] |
863 | + >>> s = Serie(); s.range = (5); print s.range |
864 | + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] |
865 | + >>> s = Serie(); s.range = (1,7); print s.range |
866 | + [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0] |
867 | + >>> s = Serie(); s.range = (0,10,2); print s.range |
868 | + [0.0, 2.0, 4.0, 6.0, 8.0, 10.0] |
869 | + >>> |
870 | + >>> s = Serie(); s.range = [0]; print s.range |
871 | + [0.0] |
872 | + >>> s = Serie(); s.range = [0,10,20]; print s.range |
873 | + [0.0, 10.0, 20.0] |
874 | + ''' |
875 | + def fget(self): |
876 | + ''' |
877 | + Returns the range |
878 | + ''' |
879 | + return self.__range |
880 | + |
881 | + def fset(self, x_range): |
882 | + ''' |
883 | + Controls the input of a valid type and generate the range |
884 | + ''' |
885 | + # if passed a simple number convert to tuple |
886 | + if type(x_range) in NUMTYPES: |
887 | + x_range = (x_range,) |
888 | + |
889 | + # A list, just convert to float |
890 | + if type(x_range) is list and len(x_range) > 0: |
891 | + # Convert all to float |
892 | + x_range = map(float, x_range) |
893 | + # Prevents repeated values and convert back to list |
894 | + self.__range = list(set(x_range[:])) |
895 | + # Sort the list to ascending order |
896 | + self.__range.sort() |
897 | + |
898 | + # A tuple, must check the lengths and generate the values |
899 | + elif type(x_range) is tuple and len(x_range) in (1,2,3): |
900 | + # Convert all to float |
901 | + x_range = map(float, x_range) |
902 | + |
903 | + # Inital values |
904 | + start = 0.0 |
905 | + step = 1.0 |
906 | + end = 0.0 |
907 | + |
908 | + # Only the end and it can't be less or iqual to 0 |
909 | + if len(x_range) is 1 and x_range > 0: |
910 | + end = x_range[0] |
911 | + |
912 | + # The start and the end but the start must be lesser then the end |
913 | + elif len(x_range) is 2 and x_range[0] < x_range[1]: |
914 | + start = x_range[0] |
915 | + end = x_range[1] |
916 | + |
917 | + # All 3, but the start must be lesser then the end |
918 | + elif x_range[0] < x_range[1]: |
919 | + start = x_range[0] |
920 | + end = x_range[1] |
921 | + step = x_range[2] |
922 | + |
923 | + # Starts the range |
924 | + self.__range = [] |
925 | + # Generate the range |
926 | + # Cnat use the range function becouse it don't suport float values |
927 | + while start <= end: |
928 | + self.__range.append(start) |
929 | + start += step |
930 | + |
931 | + # Incorrect type |
932 | + else: |
933 | + raise Exception, "x_range must be a list with one or more item or a tuple with 2 or 3 items" |
934 | + |
935 | + return property(**locals()) |
936 | + |
937 | + @apply |
938 | + def group_list(): |
939 | + doc = ''' |
940 | + The group_list is a read/write property used to pre-process the list |
941 | + of Groups. |
942 | + It can be: |
943 | + - a single number, point or lambda, will be converted to a single |
944 | + Group of one Data; |
945 | + - a list of numbers, will be converted to a group of numbers; |
946 | + - a list of tuples, will converted to a single Group of points; |
947 | + - a list of lists of numbers, each 'sublist' will be converted to |
948 | + a group of numbers; |
949 | + - a list of lists of tuples, each 'sublist' will be converted to a |
950 | + group of points; |
951 | + - a list of lists of lists, the content of the 'sublist' will be |
952 | + processed as coordinated lists and the result will be converted |
953 | + to a group of points; |
954 | + - a list of lambdas, each lambda represents a Group; |
955 | + - a Dictionary where each item can be the same of the list: number, |
956 | + point, list of numbers, list of points, list of lists |
957 | + (coordinated lists) or lambdas |
958 | + - an instance of Data; |
959 | + - an instance of group. |
960 | + |
961 | + Usage: |
962 | + >>> s = Serie() |
963 | + >>> s.group_list = [1,2,3,4]; print s |
964 | + ["Group 1 ['1', '2', '3', '4']"] |
965 | + >>> s.group_list = [[1,2,3],[4,5,6]]; print s |
966 | + ["Group 1 ['1', '2', '3']", "Group 2 ['4', '5', '6']"] |
967 | + >>> s.group_list = (1,2); print s |
968 | + ["Group 1 ['(1, 2)']"] |
969 | + >>> s.group_list = [(1,2),(2,3)]; print s |
970 | + ["Group 1 ['(1, 2)', '(2, 3)']"] |
971 | + >>> s.group_list = [[(1,2),(2,3)],[(4,5),(5,6)]]; print s |
972 | + ["Group 1 ['(1, 2)', '(2, 3)']", "Group 2 ['(4, 5)', '(5, 6)']"] |
973 | + >>> s.group_list = [[[1,2,3],[1,2,3],[1,2,3]]]; print s |
974 | + ["Group 1 ['(1, 1, 1)', '(2, 2, 2)', '(3, 3, 3)']"] |
975 | + >>> s.group_list = [(0.5,5.5) , [(0,4),(6,8)] , (5.5,7) , (7,9)]; print s |
976 | + ["Group 1 ['(0.5, 5.5)']", "Group 2 ['(0, 4)', '(6, 8)']", "Group 3 ['(5.5, 7)']", "Group 4 ['(7, 9)']"] |
977 | + >>> s.group_list = {'g1':[1,2,3], 'g2':[4,5,6]}; print s |
978 | + ["g1 ['1', '2', '3']", "g2 ['4', '5', '6']"] |
979 | + >>> s.group_list = {'g1':[(1,2),(2,3)], 'g2':[(4,5),(5,6)]}; print s |
980 | + ["g1 ['(1, 2)', '(2, 3)']", "g2 ['(4, 5)', '(5, 6)']"] |
981 | + >>> s.group_list = {'g1':[[1,2],[1,2]], 'g2':[[4,5],[4,5]]}; print s |
982 | + ["g1 ['(1, 1)', '(2, 2)']", "g2 ['(4, 4)', '(5, 5)']"] |
983 | + >>> s.range = 10 |
984 | + >>> s.group_list = lambda x:x*2 |
985 | + >>> s.group_list = [lambda x:x*2, lambda x:x**2, lambda x:x**3]; print s |
986 | + ["Group 1 ['(0.0, 0.0)', '(1.0, 2.0)', '(2.0, 4.0)', '(3.0, 6.0)', '(4.0, 8.0)', '(5.0, 10.0)', '(6.0, 12.0)', '(7.0, 14.0)', '(8.0, 16.0)', '(9.0, 18.0)', '(10.0, 20.0)']", "Group 2 ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 4.0)', '(3.0, 9.0)', '(4.0, 16.0)', '(5.0, 25.0)', '(6.0, 36.0)', '(7.0, 49.0)', '(8.0, 64.0)', '(9.0, 81.0)', '(10.0, 100.0)']", "Group 3 ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 8.0)', '(3.0, 27.0)', '(4.0, 64.0)', '(5.0, 125.0)', '(6.0, 216.0)', '(7.0, 343.0)', '(8.0, 512.0)', '(9.0, 729.0)', '(10.0, 1000.0)']"] |
987 | + >>> s.group_list = {'linear':lambda x:x*2, 'square':lambda x:x**2, 'cubic':lambda x:x**3}; print s |
988 | + ["cubic ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 8.0)', '(3.0, 27.0)', '(4.0, 64.0)', '(5.0, 125.0)', '(6.0, 216.0)', '(7.0, 343.0)', '(8.0, 512.0)', '(9.0, 729.0)', '(10.0, 1000.0)']", "linear ['(0.0, 0.0)', '(1.0, 2.0)', '(2.0, 4.0)', '(3.0, 6.0)', '(4.0, 8.0)', '(5.0, 10.0)', '(6.0, 12.0)', '(7.0, 14.0)', '(8.0, 16.0)', '(9.0, 18.0)', '(10.0, 20.0)']", "square ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 4.0)', '(3.0, 9.0)', '(4.0, 16.0)', '(5.0, 25.0)', '(6.0, 36.0)', '(7.0, 49.0)', '(8.0, 64.0)', '(9.0, 81.0)', '(10.0, 100.0)']"] |
989 | + >>> s.group_list = Data(1,'d1'); print s |
990 | + ["Group 1 ['d1: 1']"] |
991 | + >>> s.group_list = Group([(1,2),(2,3)],'g1'); print s |
992 | + ["g1 ['(1, 2)', '(2, 3)']"] |
993 | + ''' |
994 | + def fget(self): |
995 | + ''' |
996 | + Return the group list. |
997 | + ''' |
998 | + return self.__group_list |
999 | + |
1000 | + def fset(self, serie): |
1001 | + ''' |
1002 | + Controls the input of a valid group list. |
1003 | + ''' |
1004 | + #TODO: Add support to the following strem of data: [ (0.5,5.5) , [(0,4),(6,8)] , (5.5,7) , (7,9)] |
1005 | + |
1006 | + # Type: None |
1007 | + if serie is None: |
1008 | + self.__group_list = [] |
1009 | + |
1010 | + # List or Tuple |
1011 | + elif type(serie) in LISTTYPES: |
1012 | + self.__group_list = [] |
1013 | + |
1014 | + is_function = lambda x: callable(x) |
1015 | + # Groups |
1016 | + if list in map(type, serie) or max(map(is_function, serie)): |
1017 | + for group in serie: |
1018 | + self.add_group(group) |
1019 | + |
1020 | + # single group |
1021 | + else: |
1022 | + self.add_group(serie) |
1023 | + |
1024 | + #old code |
1025 | + ## List of numbers |
1026 | + #if type(serie[0]) in NUMTYPES or type(serie[0]) is tuple: |
1027 | + # print serie |
1028 | + # self.add_group(serie) |
1029 | + # |
1030 | + ## List of anything else |
1031 | + #else: |
1032 | + # for group in serie: |
1033 | + # self.add_group(group) |
1034 | + |
1035 | + # Dict representing serie of groups |
1036 | + elif type(serie) is dict: |
1037 | + self.__group_list = [] |
1038 | + names = serie.keys() |
1039 | + names.sort() |
1040 | + for name in names: |
1041 | + self.add_group(Group(serie[name],name,self)) |
1042 | + |
1043 | + # A single lambda |
1044 | + elif callable(serie): |
1045 | + self.__group_list = [] |
1046 | + self.add_group(serie) |
1047 | + |
1048 | + # Int/float, instance of Group or Data |
1049 | + elif type(serie) in NUMTYPES or isinstance(serie, Group) or isinstance(serie, Data): |
1050 | + self.__group_list = [] |
1051 | + self.add_group(serie) |
1052 | + |
1053 | + # Default |
1054 | + else: |
1055 | + raise TypeError, "Serie type not supported" |
1056 | + |
1057 | + return property(**locals()) |
1058 | + |
1059 | + def add_group(self, group, name=None): |
1060 | + ''' |
1061 | + Append a new group in group_list |
1062 | + ''' |
1063 | + if not isinstance(group, Group): |
1064 | + #Try to convert |
1065 | + group = Group(group, name, self) |
1066 | + |
1067 | + if len(group.data_list) is not 0: |
1068 | + # Auto naming groups |
1069 | + if group.name is None: |
1070 | + group.name = "Group "+str(len(self.__group_list)+1) |
1071 | + |
1072 | + self.__group_list.append(group) |
1073 | + self.__group_list[-1].parent = self |
1074 | + |
1075 | + def copy(self): |
1076 | + ''' |
1077 | + Returns a copy of the Serie |
1078 | + ''' |
1079 | + new_serie = Serie() |
1080 | + new_serie.__name = self.__name |
1081 | + if self.__range is not None: |
1082 | + new_serie.__range = self.__range[:] |
1083 | + #Add color property in the copy method |
1084 | + #self.__colors = None |
1085 | + |
1086 | + for group in self: |
1087 | + new_serie.add_group(group.copy()) |
1088 | + |
1089 | + return new_serie |
1090 | + |
1091 | + def get_names(self): |
1092 | + ''' |
1093 | + Returns a list of the names of all groups in the Serie |
1094 | + ''' |
1095 | + names = [] |
1096 | + for group in self: |
1097 | + if group.name is None: |
1098 | + names.append('Group '+str(group.index()+1)) |
1099 | + else: |
1100 | + names.append(group.name) |
1101 | + |
1102 | + return names |
1103 | + |
1104 | + def to_list(self): |
1105 | + ''' |
1106 | + Returns a list with the content of all groups and data |
1107 | + ''' |
1108 | + big_list = [] |
1109 | + for group in self: |
1110 | + for data in group: |
1111 | + if type(data.content) in NUMTYPES: |
1112 | + big_list.append(data.content) |
1113 | + else: |
1114 | + big_list = big_list + list(data.content) |
1115 | + return big_list |
1116 | + |
1117 | + def __getitem__(self, key): |
1118 | + ''' |
1119 | + Makes the Serie iterable, based in the group_list property |
1120 | + ''' |
1121 | + return self.__group_list[key] |
1122 | + |
1123 | + def __str__(self): |
1124 | + ''' |
1125 | + Returns a string that represents the Serie |
1126 | + ''' |
1127 | + ret = "" |
1128 | + if self.name is not None: |
1129 | + ret += self.name + " " |
1130 | + if len(self) > 0: |
1131 | + list_str = [str(item) for item in self] |
1132 | + ret += str(list_str) |
1133 | + else: |
1134 | + ret += "[]" |
1135 | + return ret |
1136 | + |
1137 | + def __len__(self): |
1138 | + ''' |
1139 | + Returns the length of the Serie, based in the group_lsit property |
1140 | + ''' |
1141 | + return len(self.group_list) |
1142 | + |
1143 | + |
1144 | +if __name__ == '__main__': |
1145 | + doctest.testmod() |
1146 | |
1147 | === added file 'trunk/cairoplot.py' |
1148 | --- trunk/cairoplot.py 1970-01-01 00:00:00 +0000 |
1149 | +++ trunk/cairoplot.py 2009-05-01 03:58:31 +0000 |
1150 | @@ -0,0 +1,2347 @@ |
1151 | +#!/usr/bin/env python |
1152 | +# -*- coding: utf-8 -*- |
1153 | + |
1154 | +# CairoPlot.py |
1155 | +# |
1156 | +# Copyright (c) 2008 Rodrigo Moreira Araújo |
1157 | +# |
1158 | +# Author: Rodrigo Moreiro Araujo <alf.rodrigo@gmail.com> |
1159 | +# |
1160 | +# This program is free software; you can redistribute it and/or |
1161 | +# modify it under the terms of the GNU Lesser General Public License |
1162 | +# as published by the Free Software Foundation; either version 2 of |
1163 | +# the License, or (at your option) any later version. |
1164 | +# |
1165 | +# This program is distributed in the hope that it will be useful, |
1166 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
1167 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1168 | +# GNU General Public License for more details. |
1169 | +# |
1170 | +# You should have received a copy of the GNU Lesser General Public |
1171 | +# License along with this program; if not, write to the Free Software |
1172 | +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
1173 | +# USA |
1174 | + |
1175 | +#Contributor: João S. O. Bueno |
1176 | + |
1177 | +#TODO: review BarPlot Code |
1178 | +#TODO: x_label colision problem on Horizontal Bar Plot |
1179 | +#TODO: y_label's eat too much space on HBP |
1180 | + |
1181 | + |
1182 | +__version__ = 1.1 |
1183 | + |
1184 | +import cairo |
1185 | +import math |
1186 | +import random |
1187 | +from Series import Serie, Group, Data |
1188 | + |
1189 | +HORZ = 0 |
1190 | +VERT = 1 |
1191 | +NORM = 2 |
1192 | + |
1193 | +COLORS = {"red" : (1.0,0.0,0.0,1.0), "lime" : (0.0,1.0,0.0,1.0), "blue" : (0.0,0.0,1.0,1.0), |
1194 | + "maroon" : (0.5,0.0,0.0,1.0), "green" : (0.0,0.5,0.0,1.0), "navy" : (0.0,0.0,0.5,1.0), |
1195 | + "yellow" : (1.0,1.0,0.0,1.0), "magenta" : (1.0,0.0,1.0,1.0), "cyan" : (0.0,1.0,1.0,1.0), |
1196 | + "orange" : (1.0,0.5,0.0,1.0), "white" : (1.0,1.0,1.0,1.0), "black" : (0.0,0.0,0.0,1.0), |
1197 | + "gray" : (0.5,0.5,0.5,1.0), "light_gray" : (0.9,0.9,0.9,1.0), |
1198 | + "transparent" : (0.0,0.0,0.0,0.0)} |
1199 | + |
1200 | +THEMES = {"black_red" : [(0.0,0.0,0.0,1.0), (1.0,0.0,0.0,1.0)], |
1201 | + "red_green_blue" : [(1.0,0.0,0.0,1.0), (0.0,1.0,0.0,1.0), (0.0,0.0,1.0,1.0)], |
1202 | + "red_orange_yellow" : [(1.0,0.2,0.0,1.0), (1.0,0.7,0.0,1.0), (1.0,1.0,0.0,1.0)], |
1203 | + "yellow_orange_red" : [(1.0,1.0,0.0,1.0), (1.0,0.7,0.0,1.0), (1.0,0.2,0.0,1.0)], |
1204 | + "rainbow" : [(1.0,0.0,0.0,1.0), (1.0,0.5,0.0,1.0), (1.0,1.0,0.0,1.0), (0.0,1.0,0.0,1.0), (0.0,0.0,1.0,1.0), (0.3, 0.0, 0.5,1.0), (0.5, 0.0, 1.0, 1.0)]} |
1205 | + |
1206 | +def colors_from_theme( theme, series_length, mode = 'solid' ): |
1207 | + colors = [] |
1208 | + if theme not in THEMES.keys() : |
1209 | + raise Exception, "Theme not defined" |
1210 | + color_steps = THEMES[theme] |
1211 | + n_colors = len(color_steps) |
1212 | + if series_length <= n_colors: |
1213 | + colors = [color + tuple([mode]) for color in color_steps[0:n_colors]] |
1214 | + else: |
1215 | + iterations = [(series_length - n_colors)/(n_colors - 1) for i in color_steps[:-1]] |
1216 | + over_iterations = (series_length - n_colors) % (n_colors - 1) |
1217 | + for i in range(n_colors - 1): |
1218 | + if over_iterations <= 0: |
1219 | + break |
1220 | + iterations[i] += 1 |
1221 | + over_iterations -= 1 |
1222 | + for index,color in enumerate(color_steps[:-1]): |
1223 | + colors.append(color + tuple([mode])) |
1224 | + if iterations[index] == 0: |
1225 | + continue |
1226 | + next_color = color_steps[index+1] |
1227 | + color_step = ((next_color[0] - color[0])/(iterations[index] + 1), |
1228 | + (next_color[1] - color[1])/(iterations[index] + 1), |
1229 | + (next_color[2] - color[2])/(iterations[index] + 1), |
1230 | + (next_color[3] - color[3])/(iterations[index] + 1)) |
1231 | + for i in range( iterations[index] ): |
1232 | + colors.append((color[0] + color_step[0]*(i+1), |
1233 | + color[1] + color_step[1]*(i+1), |
1234 | + color[2] + color_step[2]*(i+1), |
1235 | + color[3] + color_step[3]*(i+1), |
1236 | + mode)) |
1237 | + colors.append(color_steps[-1] + tuple([mode])) |
1238 | + return colors |
1239 | + |
1240 | + |
1241 | +def other_direction(direction): |
1242 | + "explicit is better than implicit" |
1243 | + if direction == HORZ: |
1244 | + return VERT |
1245 | + else: |
1246 | + return HORZ |
1247 | + |
1248 | +#Class definition |
1249 | + |
1250 | +class Plot(object): |
1251 | + def __init__(self, |
1252 | + surface=None, |
1253 | + data=None, |
1254 | + width=640, |
1255 | + height=480, |
1256 | + background=None, |
1257 | + border = 0, |
1258 | + x_labels = None, |
1259 | + y_labels = None, |
1260 | + series_colors = None): |
1261 | + random.seed(2) |
1262 | + self.create_surface(surface, width, height) |
1263 | + self.dimensions = {} |
1264 | + self.dimensions[HORZ] = width |
1265 | + self.dimensions[VERT] = height |
1266 | + self.context = cairo.Context(self.surface) |
1267 | + self.labels={} |
1268 | + self.labels[HORZ] = x_labels |
1269 | + self.labels[VERT] = y_labels |
1270 | + self.load_series(data, x_labels, y_labels, series_colors) |
1271 | + self.font_size = 10 |
1272 | + self.set_background (background) |
1273 | + self.border = border |
1274 | + self.borders = {} |
1275 | + self.line_color = (0.5, 0.5, 0.5) |
1276 | + self.line_width = 0.5 |
1277 | + self.label_color = (0.0, 0.0, 0.0) |
1278 | + self.grid_color = (0.8, 0.8, 0.8) |
1279 | + |
1280 | + def create_surface(self, surface, width=None, height=None): |
1281 | + self.filename = None |
1282 | + if isinstance(surface, cairo.Surface): |
1283 | + self.surface = surface |
1284 | + return |
1285 | + if not type(surface) in (str, unicode): |
1286 | + raise TypeError("Surface should be either a Cairo surface or a filename, not %s" % surface) |
1287 | + sufix = surface.rsplit(".")[-1].lower() |
1288 | + self.filename = surface |
1289 | + if sufix == "png": |
1290 | + self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
1291 | + elif sufix == "ps": |
1292 | + self.surface = cairo.PSSurface(surface, width, height) |
1293 | + elif sufix == "pdf": |
1294 | + self.surface = cairo.PSSurface(surface, width, height) |
1295 | + else: |
1296 | + if sufix != "svg": |
1297 | + self.filename += ".svg" |
1298 | + self.surface = cairo.SVGSurface(self.filename, width, height) |
1299 | + |
1300 | + def commit(self): |
1301 | + try: |
1302 | + self.context.show_page() |
1303 | + if self.filename and self.filename.endswith(".png"): |
1304 | + self.surface.write_to_png(self.filename) |
1305 | + else: |
1306 | + self.surface.finish() |
1307 | + except cairo.Error: |
1308 | + pass |
1309 | + |
1310 | + def load_series (self, data, x_labels=None, y_labels=None, series_colors=None): |
1311 | + #FIXME: implement Series class for holding series data, |
1312 | + # labels and presentation properties |
1313 | + |
1314 | + #data can be a list, a list of lists or a dictionary with |
1315 | + #each item as a labeled data series. |
1316 | + #we should (for the time being) create a list of lists |
1317 | + #and set labels for teh series rom teh values provided. |
1318 | + |
1319 | + self.series_labels = [] |
1320 | + self.serie = None |
1321 | + |
1322 | + # The pretty way... |
1323 | + #if not isinstance(data, Serie): |
1324 | + # # Not an instance of Series |
1325 | + # self.serie = Serie(data) |
1326 | + #else: |
1327 | + # self.serie = data |
1328 | + # |
1329 | + #self.series_labels = self.serie.get_names() |
1330 | + |
1331 | + #TODO: In the next version remove this... |
1332 | + # The ugly way, just to keep the retrocompatibility... |
1333 | + if callable(data) or type(data) is list and callable(data[0]): # Lambda or List of lambdas |
1334 | + self.serie = data |
1335 | + self.series_labels = None |
1336 | + elif isinstance(data, Serie): # Instance of Serie |
1337 | + self.serie = data |
1338 | + self.series_labels = data.get_names() |
1339 | + else: # Anything else |
1340 | + self.serie = Serie(data) |
1341 | + self.series_labels = self.serie.get_names() |
1342 | + |
1343 | + |
1344 | + #TODO: Remove old code |
1345 | + ##dictionary |
1346 | + #if hasattr(data, "keys"): |
1347 | + # self.series_labels = data.keys() |
1348 | + # for key in self.series_labels: |
1349 | + # self.data.append(data[key]) |
1350 | + ##lists of lists: |
1351 | + #elif max([hasattr(item,'__delitem__') for item in data]) : |
1352 | + # self.data = data |
1353 | + # self.series_labels = range(len(data)) |
1354 | + ##list |
1355 | + #else: |
1356 | + # self.data = [data] |
1357 | + # self.series_labels = None |
1358 | + |
1359 | + #TODO: allow user passed series_widths |
1360 | + self.series_widths = [1.0 for group in self.serie] |
1361 | + self.process_colors( series_colors ) |
1362 | + |
1363 | + def process_colors( self, series_colors, length = None, mode = 'solid' ): |
1364 | + #series_colors might be None, a theme, a string of colors names or a string of color tuples |
1365 | + if length is None : |
1366 | + length = len( self.serie.to_list() ) |
1367 | + |
1368 | + #no colors passed |
1369 | + if not series_colors: |
1370 | + #Randomize colors |
1371 | + self.series_colors = [ [random.random() for i in range(3)] + [1.0, mode] for series in range( length ) ] |
1372 | + else: |
1373 | + #Just theme pattern |
1374 | + if not hasattr( series_colors, "__iter__" ): |
1375 | + theme = series_colors |
1376 | + self.series_colors = colors_from_theme( theme.lower(), length ) |
1377 | + |
1378 | + #Theme pattern and mode |
1379 | + elif not hasattr(series_colors, '__delitem__') and not hasattr( series_colors[0], "__iter__" ): |
1380 | + theme = series_colors[0] |
1381 | + mode = series_colors[1] |
1382 | + self.series_colors = colors_from_theme( theme.lower(), length, mode ) |
1383 | + |
1384 | + #List |
1385 | + else: |
1386 | + self.series_colors = series_colors |
1387 | + for index, color in enumerate( self.series_colors ): |
1388 | + #element is a color name |
1389 | + if not hasattr(color, "__iter__"): |
1390 | + self.series_colors[index] = COLORS[color.lower()] + tuple([mode]) |
1391 | + #element is rgb tuple instead of rgba |
1392 | + elif len( color ) == 3 : |
1393 | + self.series_colors[index] += (1.0,mode) |
1394 | + #element has 4 elements, might be rgba tuple or rgb tuple with mode |
1395 | + elif len( color ) == 4 : |
1396 | + #last element is mode |
1397 | + if not hasattr(color[3], "__iter__"): |
1398 | + self.series_colors[index] += tuple([color[3]]) |
1399 | + self.series_colors[index][3] = 1.0 |
1400 | + #last element is alpha |
1401 | + else: |
1402 | + self.series_colors[index] += tuple([mode]) |
1403 | + |
1404 | + def get_width(self): |
1405 | + return self.surface.get_width() |
1406 | + |
1407 | + def get_height(self): |
1408 | + return self.surface.get_height() |
1409 | + |
1410 | + def set_background(self, background): |
1411 | + if background is None: |
1412 | + self.background = (0.0,0.0,0.0,0.0) |
1413 | + elif type(background) in (cairo.LinearGradient, tuple): |
1414 | + self.background = background |
1415 | + elif not hasattr(background,"__iter__"): |
1416 | + colors = background.split(" ") |
1417 | + if len(colors) == 1 and colors[0] in COLORS: |
1418 | + self.background = COLORS[background] |
1419 | + elif len(colors) > 1: |
1420 | + self.background = cairo.LinearGradient(self.dimensions[HORZ] / 2, 0, self.dimensions[HORZ] / 2, self.dimensions[VERT]) |
1421 | + for index,color in enumerate(colors): |
1422 | + self.background.add_color_stop_rgba(float(index)/(len(colors)-1),*COLORS[color]) |
1423 | + else: |
1424 | + raise TypeError ("Background should be either cairo.LinearGradient or a 3-tuple, not %s" % type(background)) |
1425 | + |
1426 | + def render_background(self): |
1427 | + if isinstance(self.background, cairo.LinearGradient): |
1428 | + self.context.set_source(self.background) |
1429 | + else: |
1430 | + self.context.set_source_rgba(*self.background) |
1431 | + self.context.rectangle(0,0, self.dimensions[HORZ], self.dimensions[VERT]) |
1432 | + self.context.fill() |
1433 | + |
1434 | + def render_bounding_box(self): |
1435 | + self.context.set_source_rgba(*self.line_color) |
1436 | + self.context.set_line_width(self.line_width) |
1437 | + self.context.rectangle(self.border, self.border, |
1438 | + self.dimensions[HORZ] - 2 * self.border, |
1439 | + self.dimensions[VERT] - 2 * self.border) |
1440 | + self.context.stroke() |
1441 | + |
1442 | + def render(self): |
1443 | + pass |
1444 | + |
1445 | +class ScatterPlot( Plot ): |
1446 | + def __init__(self, |
1447 | + surface=None, |
1448 | + data=None, |
1449 | + errorx=None, |
1450 | + errory=None, |
1451 | + width=640, |
1452 | + height=480, |
1453 | + background=None, |
1454 | + border=0, |
1455 | + axis = False, |
1456 | + dash = False, |
1457 | + discrete = False, |
1458 | + dots = 0, |
1459 | + grid = False, |
1460 | + series_legend = False, |
1461 | + x_labels = None, |
1462 | + y_labels = None, |
1463 | + x_bounds = None, |
1464 | + y_bounds = None, |
1465 | + z_bounds = None, |
1466 | + x_title = None, |
1467 | + y_title = None, |
1468 | + series_colors = None, |
1469 | + circle_colors = None ): |
1470 | + |
1471 | + self.bounds = {} |
1472 | + self.bounds[HORZ] = x_bounds |
1473 | + self.bounds[VERT] = y_bounds |
1474 | + self.bounds[NORM] = z_bounds |
1475 | + self.titles = {} |
1476 | + self.titles[HORZ] = x_title |
1477 | + self.titles[VERT] = y_title |
1478 | + self.max_value = {} |
1479 | + self.axis = axis |
1480 | + self.discrete = discrete |
1481 | + self.dots = dots |
1482 | + self.grid = grid |
1483 | + self.series_legend = series_legend |
1484 | + self.variable_radius = False |
1485 | + self.x_label_angle = math.pi / 2.5 |
1486 | + self.circle_colors = circle_colors |
1487 | + |
1488 | + Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors) |
1489 | + |
1490 | + self.dash = None |
1491 | + if dash: |
1492 | + if hasattr(dash, "keys"): |
1493 | + self.dash = [dash[key] for key in self.series_labels] |
1494 | + elif max([hasattr(item,'__delitem__') for item in data]) : |
1495 | + self.dash = dash |
1496 | + else: |
1497 | + self.dash = [dash] |
1498 | + |
1499 | + self.load_errors(errorx, errory) |
1500 | + |
1501 | + def convert_list_to_tuple(self, data): |
1502 | + #Data must be converted from lists of coordinates to a single |
1503 | + # list of tuples |
1504 | + out_data = zip(*data) |
1505 | + if len(data) == 3: |
1506 | + self.variable_radius = True |
1507 | + return out_data |
1508 | + |
1509 | + def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): |
1510 | + #TODO: In cairoplot2.0 keep only the Series instances |
1511 | + # Convert Data and Group to Serie |
1512 | + if isinstance(data, Data) or isinstance(data, Group): |
1513 | + data = Serie(data) |
1514 | + |
1515 | + # Serie |
1516 | + if isinstance(data, Serie): |
1517 | + for group in data: |
1518 | + for item in group: |
1519 | + if len(item) is 3: |
1520 | + self.variable_radius = True |
1521 | + |
1522 | + #Dictionary with lists |
1523 | + if hasattr(data, "keys") : |
1524 | + if hasattr( data.values()[0][0], "__delitem__" ) : |
1525 | + for key in data.keys() : |
1526 | + data[key] = self.convert_list_to_tuple(data[key]) |
1527 | + elif len(data.values()[0][0]) == 3: |
1528 | + self.variable_radius = True |
1529 | + #List |
1530 | + elif hasattr(data[0], "__delitem__") : |
1531 | + #List of lists |
1532 | + if hasattr(data[0][0], "__delitem__") : |
1533 | + for index,value in enumerate(data) : |
1534 | + data[index] = self.convert_list_to_tuple(value) |
1535 | + #List |
1536 | + elif type(data[0][0]) != type((0,0)): |
1537 | + data = self.convert_list_to_tuple(data) |
1538 | + #Three dimensional data |
1539 | + elif len(data[0][0]) == 3: |
1540 | + self.variable_radius = True |
1541 | + #List with three dimensional tuples |
1542 | + elif len(data[0]) == 3: |
1543 | + self.variable_radius = True |
1544 | + Plot.load_series(self, data, x_labels, y_labels, series_colors) |
1545 | + self.calc_boundaries() |
1546 | + self.calc_labels() |
1547 | + |
1548 | + def load_errors(self, errorx, errory): |
1549 | + self.errors = None |
1550 | + if errorx == None and errory == None: |
1551 | + return |
1552 | + self.errors = {} |
1553 | + self.errors[HORZ] = None |
1554 | + self.errors[VERT] = None |
1555 | + #asimetric errors |
1556 | + if errorx and hasattr(errorx[0], "__delitem__"): |
1557 | + self.errors[HORZ] = errorx |
1558 | + #simetric errors |
1559 | + elif errorx: |
1560 | + self.errors[HORZ] = [errorx] |
1561 | + #asimetric errors |
1562 | + if errory and hasattr(errory[0], "__delitem__"): |
1563 | + self.errors[VERT] = errory |
1564 | + #simetric errors |
1565 | + elif errory: |
1566 | + self.errors[VERT] = [errory] |
1567 | + |
1568 | + def calc_labels(self): |
1569 | + if not self.labels[HORZ]: |
1570 | + amplitude = self.bounds[HORZ][1] - self.bounds[HORZ][0] |
1571 | + if amplitude % 10: #if horizontal labels need floating points |
1572 | + self.labels[HORZ] = ["%.2lf" % (float(self.bounds[HORZ][0] + (amplitude * i / 10.0))) for i in range(11) ] |
1573 | + else: |
1574 | + self.labels[HORZ] = ["%d" % (int(self.bounds[HORZ][0] + (amplitude * i / 10.0))) for i in range(11) ] |
1575 | + if not self.labels[VERT]: |
1576 | + amplitude = self.bounds[VERT][1] - self.bounds[VERT][0] |
1577 | + if amplitude % 10: #if vertical labels need floating points |
1578 | + self.labels[VERT] = ["%.2lf" % (float(self.bounds[VERT][0] + (amplitude * i / 10.0))) for i in range(11) ] |
1579 | + else: |
1580 | + self.labels[VERT] = ["%d" % (int(self.bounds[VERT][0] + (amplitude * i / 10.0))) for i in range(11) ] |
1581 | + |
1582 | + def calc_extents(self, direction): |
1583 | + self.context.set_font_size(self.font_size * 0.8) |
1584 | + self.max_value[direction] = max(self.context.text_extents(item)[2] for item in self.labels[direction]) |
1585 | + self.borders[other_direction(direction)] = self.max_value[direction] + self.border + 20 |
1586 | + |
1587 | + def calc_boundaries(self): |
1588 | + #HORZ = 0, VERT = 1, NORM = 2 |
1589 | + min_data_value = [0,0,0] |
1590 | + max_data_value = [0,0,0] |
1591 | + |
1592 | + for group in self.serie: |
1593 | + if type(group[0].content) in (int, float, long): |
1594 | + group = [Data((index, item.content)) for index,item in enumerate(group)] |
1595 | + |
1596 | + for point in group: |
1597 | + for index, item in enumerate(point.content): |
1598 | + if item > max_data_value[index]: |
1599 | + max_data_value[index] = item |
1600 | + elif item < min_data_value[index]: |
1601 | + min_data_value[index] = item |
1602 | + |
1603 | + if not self.bounds[HORZ]: |
1604 | + self.bounds[HORZ] = (min_data_value[HORZ], max_data_value[HORZ]) |
1605 | + if not self.bounds[VERT]: |
1606 | + self.bounds[VERT] = (min_data_value[VERT], max_data_value[VERT]) |
1607 | + if not self.bounds[NORM]: |
1608 | + self.bounds[NORM] = (min_data_value[NORM], max_data_value[NORM]) |
1609 | + |
1610 | + def calc_all_extents(self): |
1611 | + self.calc_extents(HORZ) |
1612 | + self.calc_extents(VERT) |
1613 | + |
1614 | + self.plot_height = self.dimensions[VERT] - 2 * self.borders[VERT] |
1615 | + self.plot_width = self.dimensions[HORZ] - 2* self.borders[HORZ] |
1616 | + |
1617 | + self.plot_top = self.dimensions[VERT] - self.borders[VERT] |
1618 | + |
1619 | + def calc_steps(self): |
1620 | + #Calculates all the x, y, z and color steps |
1621 | + series_amplitude = [self.bounds[index][1] - self.bounds[index][0] for index in range(3)] |
1622 | + |
1623 | + if series_amplitude[HORZ]: |
1624 | + self.horizontal_step = float (self.plot_width) / series_amplitude[HORZ] |
1625 | + else: |
1626 | + self.horizontal_step = 0.00 |
1627 | + |
1628 | + if series_amplitude[VERT]: |
1629 | + self.vertical_step = float (self.plot_height) / series_amplitude[VERT] |
1630 | + else: |
1631 | + self.vertical_step = 0.00 |
1632 | + |
1633 | + if series_amplitude[NORM]: |
1634 | + if self.variable_radius: |
1635 | + self.z_step = float (self.bounds[NORM][1]) / series_amplitude[NORM] |
1636 | + if self.circle_colors: |
1637 | + self.circle_color_step = tuple([float(self.circle_colors[1][i]-self.circle_colors[0][i])/series_amplitude[NORM] for i in range(4)]) |
1638 | + else: |
1639 | + self.z_step = 0.00 |
1640 | + self.circle_color_step = ( 0.0, 0.0, 0.0, 0.0 ) |
1641 | + |
1642 | + def get_circle_color(self, value): |
1643 | + return tuple( [self.circle_colors[0][i] + value*self.circle_color_step[i] for i in range(4)] ) |
1644 | + |
1645 | + def render(self): |
1646 | + self.calc_all_extents() |
1647 | + self.calc_steps() |
1648 | + self.render_background() |
1649 | + self.render_bounding_box() |
1650 | + if self.axis: |
1651 | + self.render_axis() |
1652 | + if self.grid: |
1653 | + self.render_grid() |
1654 | + self.render_labels() |
1655 | + self.render_plot() |
1656 | + if self.errors: |
1657 | + self.render_errors() |
1658 | + if self.series_legend and self.series_labels: |
1659 | + self.render_legend() |
1660 | + |
1661 | + def render_axis(self): |
1662 | + #Draws both the axis lines and their titles |
1663 | + cr = self.context |
1664 | + cr.set_source_rgba(*self.line_color) |
1665 | + cr.move_to(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) |
1666 | + cr.line_to(self.borders[HORZ], self.borders[VERT]) |
1667 | + cr.stroke() |
1668 | + |
1669 | + cr.move_to(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) |
1670 | + cr.line_to(self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) |
1671 | + cr.stroke() |
1672 | + |
1673 | + cr.set_source_rgba(*self.label_color) |
1674 | + self.context.set_font_size( 1.2 * self.font_size ) |
1675 | + if self.titles[HORZ]: |
1676 | + title_width,title_height = cr.text_extents(self.titles[HORZ])[2:4] |
1677 | + cr.move_to( self.dimensions[HORZ]/2 - title_width/2, self.borders[VERT] - title_height/2 ) |
1678 | + cr.show_text( self.titles[HORZ] ) |
1679 | + |
1680 | + if self.titles[VERT]: |
1681 | + title_width,title_height = cr.text_extents(self.titles[VERT])[2:4] |
1682 | + cr.move_to( self.dimensions[HORZ] - self.borders[HORZ] + title_height/2, self.dimensions[VERT]/2 - title_width/2) |
1683 | + cr.rotate( math.pi/2 ) |
1684 | + cr.show_text( self.titles[VERT] ) |
1685 | + cr.rotate( -math.pi/2 ) |
1686 | + |
1687 | + def render_grid(self): |
1688 | + cr = self.context |
1689 | + horizontal_step = float( self.plot_height ) / ( len( self.labels[VERT] ) - 1 ) |
1690 | + vertical_step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 ) |
1691 | + |
1692 | + x = self.borders[HORZ] + vertical_step |
1693 | + y = self.plot_top - horizontal_step |
1694 | + |
1695 | + for label in self.labels[HORZ][:-1]: |
1696 | + cr.set_source_rgba(*self.grid_color) |
1697 | + cr.move_to(x, self.dimensions[VERT] - self.borders[VERT]) |
1698 | + cr.line_to(x, self.borders[VERT]) |
1699 | + cr.stroke() |
1700 | + x += vertical_step |
1701 | + for label in self.labels[VERT][:-1]: |
1702 | + cr.set_source_rgba(*self.grid_color) |
1703 | + cr.move_to(self.borders[HORZ], y) |
1704 | + cr.line_to(self.dimensions[HORZ] - self.borders[HORZ], y) |
1705 | + cr.stroke() |
1706 | + y -= horizontal_step |
1707 | + |
1708 | + def render_labels(self): |
1709 | + self.context.set_font_size(self.font_size * 0.8) |
1710 | + self.render_horz_labels() |
1711 | + self.render_vert_labels() |
1712 | + |
1713 | + def render_horz_labels(self): |
1714 | + cr = self.context |
1715 | + step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 ) |
1716 | + x = self.borders[HORZ] |
1717 | + for item in self.labels[HORZ]: |
1718 | + cr.set_source_rgba(*self.label_color) |
1719 | + width = cr.text_extents(item)[2] |
1720 | + cr.move_to(x, self.dimensions[VERT] - self.borders[VERT] + 5) |
1721 | + cr.rotate(self.x_label_angle) |
1722 | + cr.show_text(item) |
1723 | + cr.rotate(-self.x_label_angle) |
1724 | + x += step |
1725 | + |
1726 | + def render_vert_labels(self): |
1727 | + cr = self.context |
1728 | + step = ( self.plot_height ) / ( len( self.labels[VERT] ) - 1 ) |
1729 | + y = self.plot_top |
1730 | + for item in self.labels[VERT]: |
1731 | + cr.set_source_rgba(*self.label_color) |
1732 | + width = cr.text_extents(item)[2] |
1733 | + cr.move_to(self.borders[HORZ] - width - 5,y) |
1734 | + cr.show_text(item) |
1735 | + y -= step |
1736 | + |
1737 | + def render_legend(self): |
1738 | + cr = self.context |
1739 | + cr.set_font_size(self.font_size) |
1740 | + cr.set_line_width(self.line_width) |
1741 | + |
1742 | + widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2]) |
1743 | + tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3]) |
1744 | + max_width = self.context.text_extents(widest_word)[2] |
1745 | + max_height = self.context.text_extents(tallest_word)[3] * 1.1 |
1746 | + |
1747 | + color_box_height = max_height / 2 |
1748 | + color_box_width = color_box_height * 2 |
1749 | + |
1750 | + #Draw a bounding box |
1751 | + bounding_box_width = max_width + color_box_width + 15 |
1752 | + bounding_box_height = (len(self.series_labels)+0.5) * max_height |
1753 | + cr.set_source_rgba(1,1,1) |
1754 | + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - bounding_box_width, self.borders[VERT], |
1755 | + bounding_box_width, bounding_box_height) |
1756 | + cr.fill() |
1757 | + |
1758 | + cr.set_source_rgba(*self.line_color) |
1759 | + cr.set_line_width(self.line_width) |
1760 | + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - bounding_box_width, self.borders[VERT], |
1761 | + bounding_box_width, bounding_box_height) |
1762 | + cr.stroke() |
1763 | + |
1764 | + for idx,key in enumerate(self.series_labels): |
1765 | + #Draw color box |
1766 | + cr.set_source_rgba(*self.series_colors[idx][:4]) |
1767 | + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - max_width - color_box_width - 10, |
1768 | + self.borders[VERT] + color_box_height + (idx*max_height) , |
1769 | + color_box_width, color_box_height) |
1770 | + cr.fill() |
1771 | + |
1772 | + cr.set_source_rgba(0, 0, 0) |
1773 | + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - max_width - color_box_width - 10, |
1774 | + self.borders[VERT] + color_box_height + (idx*max_height), |
1775 | + color_box_width, color_box_height) |
1776 | + cr.stroke() |
1777 | + |
1778 | + #Draw series labels |
1779 | + cr.set_source_rgba(0, 0, 0) |
1780 | + cr.move_to(self.dimensions[HORZ] - self.borders[HORZ] - max_width - 5, self.borders[VERT] + ((idx+1)*max_height)) |
1781 | + cr.show_text(key) |
1782 | + |
1783 | + def render_errors(self): |
1784 | + cr = self.context |
1785 | + cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) |
1786 | + cr.clip() |
1787 | + radius = self.dots |
1788 | + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
1789 | + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
1790 | + for index, group in enumerate(self.serie): |
1791 | + cr.set_source_rgba(*self.series_colors[index][:4]) |
1792 | + for number, data in enumerate(group): |
1793 | + x = x0 + self.horizontal_step * data.content[0] |
1794 | + y = self.dimensions[VERT] - y0 - self.vertical_step * data.content[1] |
1795 | + if self.errors[HORZ]: |
1796 | + cr.move_to(x, y) |
1797 | + x1 = x - self.horizontal_step * self.errors[HORZ][0][number] |
1798 | + cr.line_to(x1, y) |
1799 | + cr.line_to(x1, y - radius) |
1800 | + cr.line_to(x1, y + radius) |
1801 | + cr.stroke() |
1802 | + if self.errors[HORZ] and len(self.errors[HORZ]) == 2: |
1803 | + cr.move_to(x, y) |
1804 | + x1 = x + self.horizontal_step * self.errors[HORZ][1][number] |
1805 | + cr.line_to(x1, y) |
1806 | + cr.line_to(x1, y - radius) |
1807 | + cr.line_to(x1, y + radius) |
1808 | + cr.stroke() |
1809 | + if self.errors[VERT]: |
1810 | + cr.move_to(x, y) |
1811 | + y1 = y + self.vertical_step * self.errors[VERT][0][number] |
1812 | + cr.line_to(x, y1) |
1813 | + cr.line_to(x - radius, y1) |
1814 | + cr.line_to(x + radius, y1) |
1815 | + cr.stroke() |
1816 | + if self.errors[VERT] and len(self.errors[VERT]) == 2: |
1817 | + cr.move_to(x, y) |
1818 | + y1 = y - self.vertical_step * self.errors[VERT][1][number] |
1819 | + cr.line_to(x, y1) |
1820 | + cr.line_to(x - radius, y1) |
1821 | + cr.line_to(x + radius, y1) |
1822 | + cr.stroke() |
1823 | + |
1824 | + |
1825 | + def render_plot(self): |
1826 | + cr = self.context |
1827 | + if self.discrete: |
1828 | + cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) |
1829 | + cr.clip() |
1830 | + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
1831 | + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
1832 | + radius = self.dots |
1833 | + for number, group in enumerate (self.serie): |
1834 | + cr.set_source_rgba(*self.series_colors[number][:4]) |
1835 | + for data in group : |
1836 | + if self.variable_radius: |
1837 | + radius = data.content[2]*self.z_step |
1838 | + if self.circle_colors: |
1839 | + cr.set_source_rgba( *self.get_circle_color( data.content[2]) ) |
1840 | + x = x0 + self.horizontal_step*data.content[0] |
1841 | + y = y0 + self.vertical_step*data.content[1] |
1842 | + cr.arc(x, self.dimensions[VERT] - y, radius, 0, 2*math.pi) |
1843 | + cr.fill() |
1844 | + else: |
1845 | + cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) |
1846 | + cr.clip() |
1847 | + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
1848 | + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
1849 | + radius = self.dots |
1850 | + for number, group in enumerate (self.serie): |
1851 | + last_data = None |
1852 | + cr.set_source_rgba(*self.series_colors[number][:4]) |
1853 | + for data in group : |
1854 | + x = x0 + self.horizontal_step*data.content[0] |
1855 | + y = y0 + self.vertical_step*data.content[1] |
1856 | + if self.dots: |
1857 | + if self.variable_radius: |
1858 | + radius = data.content[2]*self.z_step |
1859 | + cr.arc(x, self.dimensions[VERT] - y, radius, 0, 2*math.pi) |
1860 | + cr.fill() |
1861 | + if last_data : |
1862 | + old_x = x0 + self.horizontal_step*last_data.content[0] |
1863 | + old_y = y0 + self.vertical_step*last_data.content[1] |
1864 | + cr.move_to( old_x, self.dimensions[VERT] - old_y ) |
1865 | + cr.line_to( x, self.dimensions[VERT] - y) |
1866 | + cr.set_line_width(self.series_widths[number]) |
1867 | + |
1868 | + # Display line as dash line |
1869 | + if self.dash and self.dash[number]: |
1870 | + s = self.series_widths[number] |
1871 | + cr.set_dash([s*3, s*3], 0) |
1872 | + |
1873 | + cr.stroke() |
1874 | + cr.set_dash([]) |
1875 | + last_data = data |
1876 | + |
1877 | +class DotLinePlot(ScatterPlot): |
1878 | + def __init__(self, |
1879 | + surface=None, |
1880 | + data=None, |
1881 | + width=640, |
1882 | + height=480, |
1883 | + background=None, |
1884 | + border=0, |
1885 | + axis = False, |
1886 | + dash = False, |
1887 | + dots = 0, |
1888 | + grid = False, |
1889 | + series_legend = False, |
1890 | + x_labels = None, |
1891 | + y_labels = None, |
1892 | + x_bounds = None, |
1893 | + y_bounds = None, |
1894 | + x_title = None, |
1895 | + y_title = None, |
1896 | + series_colors = None): |
1897 | + |
1898 | + ScatterPlot.__init__(self, surface, data, None, None, width, height, background, border, |
1899 | + axis, dash, False, dots, grid, series_legend, x_labels, y_labels, |
1900 | + x_bounds, y_bounds, None, x_title, y_title, series_colors, None ) |
1901 | + |
1902 | + |
1903 | + def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): |
1904 | + Plot.load_series(self, data, x_labels, y_labels, series_colors) |
1905 | + for group in self.serie : |
1906 | + for index,data in enumerate(group): |
1907 | + group[index].content = (index, data.content) |
1908 | + |
1909 | + self.calc_boundaries() |
1910 | + self.calc_labels() |
1911 | + |
1912 | +class FunctionPlot(ScatterPlot): |
1913 | + def __init__(self, |
1914 | + surface=None, |
1915 | + data=None, |
1916 | + width=640, |
1917 | + height=480, |
1918 | + background=None, |
1919 | + border=0, |
1920 | + axis = False, |
1921 | + discrete = False, |
1922 | + dots = 0, |
1923 | + grid = False, |
1924 | + series_legend = False, |
1925 | + x_labels = None, |
1926 | + y_labels = None, |
1927 | + x_bounds = None, |
1928 | + y_bounds = None, |
1929 | + x_title = None, |
1930 | + y_title = None, |
1931 | + series_colors = None, |
1932 | + step = 1): |
1933 | + |
1934 | + self.function = data |
1935 | + self.step = step |
1936 | + self.discrete = discrete |
1937 | + |
1938 | + data, x_bounds = self.load_series_from_function( self.function, x_bounds ) |
1939 | + |
1940 | + ScatterPlot.__init__(self, surface, data, None, None, width, height, background, border, |
1941 | + axis, False, discrete, dots, grid, series_legend, x_labels, y_labels, |
1942 | + x_bounds, y_bounds, None, x_title, y_title, series_colors, None ) |
1943 | + |
1944 | + def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): |
1945 | + Plot.load_series(self, data, x_labels, y_labels, series_colors) |
1946 | + |
1947 | + if len(self.serie[0][0]) is 1: |
1948 | + for group_id, group in enumerate(self.serie) : |
1949 | + for index,data in enumerate(group): |
1950 | + group[index].content = (self.bounds[HORZ][0] + self.step*index, data.content) |
1951 | + |
1952 | + self.calc_boundaries() |
1953 | + self.calc_labels() |
1954 | + |
1955 | + def load_series_from_function( self, function, x_bounds ): |
1956 | + #TODO: Add the possibility for the user to define multiple functions with different discretization parameters |
1957 | + |
1958 | + #This function converts a function, a list of functions or a dictionary |
1959 | + #of functions into its corresponding array of data |
1960 | + serie = Serie() |
1961 | + |
1962 | + if isinstance(function, Group) or isinstance(function, Data): |
1963 | + function = Serie(function) |
1964 | + |
1965 | + # If is instance of Serie |
1966 | + if isinstance(function, Serie): |
1967 | + # Overwrite any bounds passed by the function |
1968 | + x_bounds = (function.range[0],function.range[-1]) |
1969 | + |
1970 | + #if no bounds are provided |
1971 | + if x_bounds == None: |
1972 | + x_bounds = (0,10) |
1973 | + |
1974 | + |
1975 | + #TODO: Finish the dict translation |
1976 | + if hasattr(function, "keys"): #dictionary: |
1977 | + for key in function.keys(): |
1978 | + group = Group(name=key) |
1979 | + #data[ key ] = [] |
1980 | + i = x_bounds[0] |
1981 | + while i <= x_bounds[1] : |
1982 | + group.add_data(function[ key ](i)) |
1983 | + #data[ key ].append( function[ key ](i) ) |
1984 | + i += self.step |
1985 | + serie.add_group(group) |
1986 | + |
1987 | + elif hasattr(function, "__delitem__"): #list of functions |
1988 | + for index,f in enumerate( function ) : |
1989 | + group = Group() |
1990 | + #data.append( [] ) |
1991 | + i = x_bounds[0] |
1992 | + while i <= x_bounds[1] : |
1993 | + group.add_data(f(i)) |
1994 | + #data[ index ].append( f(i) ) |
1995 | + i += self.step |
1996 | + serie.add_group(group) |
1997 | + |
1998 | + elif isinstance(function, Serie): # instance of Serie |
1999 | + serie = function |
2000 | + |
2001 | + else: #function |
2002 | + group = Group() |
2003 | + i = x_bounds[0] |
2004 | + while i <= x_bounds[1] : |
2005 | + group.add_data(function(i)) |
2006 | + i += self.step |
2007 | + serie.add_group(group) |
2008 | + |
2009 | + |
2010 | + return serie, x_bounds |
2011 | + |
2012 | + def calc_labels(self): |
2013 | + if not self.labels[HORZ]: |
2014 | + self.labels[HORZ] = [] |
2015 | + i = self.bounds[HORZ][0] |
2016 | + while i<=self.bounds[HORZ][1]: |
2017 | + self.labels[HORZ].append(str(i)) |
2018 | + i += float(self.bounds[HORZ][1] - self.bounds[HORZ][0])/10 |
2019 | + ScatterPlot.calc_labels(self) |
2020 | + |
2021 | + def render_plot(self): |
2022 | + if not self.discrete: |
2023 | + ScatterPlot.render_plot(self) |
2024 | + else: |
2025 | + last = None |
2026 | + cr = self.context |
2027 | + for number, group in enumerate (self.serie): |
2028 | + cr.set_source_rgba(*self.series_colors[number][:4]) |
2029 | + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
2030 | + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
2031 | + for data in group: |
2032 | + x = x0 + self.horizontal_step * data.content[0] |
2033 | + y = y0 + self.vertical_step * data.content[1] |
2034 | + cr.move_to(x, self.dimensions[VERT] - y) |
2035 | + cr.line_to(x, self.plot_top) |
2036 | + cr.set_line_width(self.series_widths[number]) |
2037 | + cr.stroke() |
2038 | + if self.dots: |
2039 | + cr.new_path() |
2040 | + cr.arc(x, self.dimensions[VERT] - y, 3, 0, 2.1 * math.pi) |
2041 | + cr.close_path() |
2042 | + cr.fill() |
2043 | + |
2044 | +class BarPlot(Plot): |
2045 | + def __init__(self, |
2046 | + surface = None, |
2047 | + data = None, |
2048 | + width = 640, |
2049 | + height = 480, |
2050 | + background = "white light_gray", |
2051 | + border = 0, |
2052 | + display_values = False, |
2053 | + grid = False, |
2054 | + rounded_corners = False, |
2055 | + stack = False, |
2056 | + three_dimension = False, |
2057 | + x_labels = None, |
2058 | + y_labels = None, |
2059 | + x_bounds = None, |
2060 | + y_bounds = None, |
2061 | + series_colors = None, |
2062 | + main_dir = None): |
2063 | + |
2064 | + self.bounds = {} |
2065 | + self.bounds[HORZ] = x_bounds |
2066 | + self.bounds[VERT] = y_bounds |
2067 | + self.display_values = display_values |
2068 | + self.grid = grid |
2069 | + self.rounded_corners = rounded_corners |
2070 | + self.stack = stack |
2071 | + self.three_dimension = three_dimension |
2072 | + self.x_label_angle = math.pi / 2.5 |
2073 | + self.main_dir = main_dir |
2074 | + self.max_value = {} |
2075 | + self.plot_dimensions = {} |
2076 | + self.steps = {} |
2077 | + self.value_label_color = (0.5,0.5,0.5,1.0) |
2078 | + |
2079 | + Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors) |
2080 | + |
2081 | + def load_series(self, data, x_labels = None, y_labels = None, series_colors = None): |
2082 | + Plot.load_series(self, data, x_labels, y_labels, series_colors) |
2083 | + self.calc_boundaries() |
2084 | + |
2085 | + def process_colors(self, series_colors): |
2086 | + #Data for a BarPlot might be a List or a List of Lists. |
2087 | + #On the first case, colors must be generated for all bars, |
2088 | + #On the second, colors must be generated for each of the inner lists. |
2089 | + |
2090 | + #TODO: Didn't get it... |
2091 | + #if hasattr(self.data[0], '__getitem__'): |
2092 | + # length = max(len(series) for series in self.data) |
2093 | + #else: |
2094 | + # length = len( self.data ) |
2095 | + |
2096 | + length = max(len(group) for group in self.serie) |
2097 | + |
2098 | + Plot.process_colors( self, series_colors, length, 'linear') |
2099 | + |
2100 | + def calc_boundaries(self): |
2101 | + if not self.bounds[self.main_dir]: |
2102 | + if self.stack: |
2103 | + max_data_value = max(sum(group.to_list()) for group in self.serie) |
2104 | + else: |
2105 | + max_data_value = max(max(group.to_list()) for group in self.serie) |
2106 | + self.bounds[self.main_dir] = (0, max_data_value) |
2107 | + if not self.bounds[other_direction(self.main_dir)]: |
2108 | + self.bounds[other_direction(self.main_dir)] = (0, len(self.serie)) |
2109 | + |
2110 | + def calc_extents(self, direction): |
2111 | + self.max_value[direction] = 0 |
2112 | + if self.labels[direction]: |
2113 | + widest_word = max(self.labels[direction], key = lambda item: self.context.text_extents(item)[2]) |
2114 | + self.max_value[direction] = self.context.text_extents(widest_word)[3 - direction] |
2115 | + self.borders[other_direction(direction)] = (2-direction)*self.max_value[direction] + self.border + direction*(5) |
2116 | + else: |
2117 | + self.borders[other_direction(direction)] = self.border |
2118 | + |
2119 | + def calc_horz_extents(self): |
2120 | + self.calc_extents(HORZ) |
2121 | + |
2122 | + def calc_vert_extents(self): |
2123 | + self.calc_extents(VERT) |
2124 | + |
2125 | + def calc_all_extents(self): |
2126 | + self.calc_horz_extents() |
2127 | + self.calc_vert_extents() |
2128 | + other_dir = other_direction(self.main_dir) |
2129 | + self.value_label = 0 |
2130 | + if self.display_values: |
2131 | + if self.stack: |
2132 | + self.value_label = self.context.text_extents(str(max(sum(group.to_list()) for group in self.serie)))[2 + self.main_dir] |
2133 | + else: |
2134 | + self.value_label = self.context.text_extents(str(max(max(group.to_list()) for group in self.serie)))[2 + self.main_dir] |
2135 | + if self.labels[self.main_dir]: |
2136 | + self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - 2*self.borders[self.main_dir] - self.value_label |
2137 | + else: |
2138 | + self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - self.borders[self.main_dir] - 1.2*self.border - self.value_label |
2139 | + self.plot_dimensions[other_dir] = self.dimensions[other_dir] - self.borders[other_dir] - self.border |
2140 | + self.plot_top = self.dimensions[VERT] - self.borders[VERT] |
2141 | + |
2142 | + def calc_steps(self): |
2143 | + other_dir = other_direction(self.main_dir) |
2144 | + self.series_amplitude = self.bounds[self.main_dir][1] - self.bounds[self.main_dir][0] |
2145 | + if self.series_amplitude: |
2146 | + self.steps[self.main_dir] = float(self.plot_dimensions[self.main_dir])/self.series_amplitude |
2147 | + else: |
2148 | + self.steps[self.main_dir] = 0.00 |
2149 | + series_length = len(self.serie) |
2150 | + self.steps[other_dir] = float(self.plot_dimensions[other_dir])/(series_length + 0.1*(series_length + 1)) |
2151 | + self.space = 0.1*self.steps[other_dir] |
2152 | + |
2153 | + def render(self): |
2154 | + self.calc_all_extents() |
2155 | + self.calc_steps() |
2156 | + self.render_background() |
2157 | + self.render_bounding_box() |
2158 | + if self.grid: |
2159 | + self.render_grid() |
2160 | + if self.three_dimension: |
2161 | + self.render_ground() |
2162 | + if self.display_values: |
2163 | + self.render_values() |
2164 | + self.render_labels() |
2165 | + self.render_plot() |
2166 | + if self.series_labels: |
2167 | + self.render_legend() |
2168 | + |
2169 | + def draw_3d_rectangle_front(self, x0, y0, x1, y1, shift): |
2170 | + self.context.rectangle(x0-shift, y0+shift, x1-x0, y1-y0) |
2171 | + |
2172 | + def draw_3d_rectangle_side(self, x0, y0, x1, y1, shift): |
2173 | + self.context.move_to(x1-shift,y0+shift) |
2174 | + self.context.line_to(x1, y0) |
2175 | + self.context.line_to(x1, y1) |
2176 | + self.context.line_to(x1-shift, y1+shift) |
2177 | + self.context.line_to(x1-shift, y0+shift) |
2178 | + self.context.close_path() |
2179 | + |
2180 | + def draw_3d_rectangle_top(self, x0, y0, x1, y1, shift): |
2181 | + self.context.move_to(x0-shift,y0+shift) |
2182 | + self.context.line_to(x0, y0) |
2183 | + self.context.line_to(x1, y0) |
2184 | + self.context.line_to(x1-shift, y0+shift) |
2185 | + self.context.line_to(x0-shift, y0+shift) |
2186 | + self.context.close_path() |
2187 | + |
2188 | + def draw_round_rectangle(self, x0, y0, x1, y1): |
2189 | + self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) |
2190 | + self.context.line_to(x1-5, y0) |
2191 | + self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) |
2192 | + self.context.line_to(x1, y1-5) |
2193 | + self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) |
2194 | + self.context.line_to(x0+5, y1) |
2195 | + self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) |
2196 | + self.context.line_to(x0, y0+5) |
2197 | + self.context.close_path() |
2198 | + |
2199 | + def render_ground(self): |
2200 | + self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
2201 | + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
2202 | + self.context.fill() |
2203 | + |
2204 | + self.draw_3d_rectangle_side (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
2205 | + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
2206 | + self.context.fill() |
2207 | + |
2208 | + self.draw_3d_rectangle_top (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
2209 | + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
2210 | + self.context.fill() |
2211 | + |
2212 | + def render_labels(self): |
2213 | + self.context.set_font_size(self.font_size * 0.8) |
2214 | + if self.labels[HORZ]: |
2215 | + self.render_horz_labels() |
2216 | + if self.labels[VERT]: |
2217 | + self.render_vert_labels() |
2218 | + |
2219 | + def render_legend(self): |
2220 | + cr = self.context |
2221 | + cr.set_font_size(self.font_size) |
2222 | + cr.set_line_width(self.line_width) |
2223 | + |
2224 | + widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2]) |
2225 | + tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3]) |
2226 | + max_width = self.context.text_extents(widest_word)[2] |
2227 | + max_height = self.context.text_extents(tallest_word)[3] * 1.1 + 5 |
2228 | + |
2229 | + color_box_height = max_height / 2 |
2230 | + color_box_width = color_box_height * 2 |
2231 | + |
2232 | + #Draw a bounding box |
2233 | + bounding_box_width = max_width + color_box_width + 15 |
2234 | + bounding_box_height = (len(self.series_labels)+0.5) * max_height |
2235 | + cr.set_source_rgba(1,1,1) |
2236 | + cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border, |
2237 | + bounding_box_width, bounding_box_height) |
2238 | + cr.fill() |
2239 | + |
2240 | + cr.set_source_rgba(*self.line_color) |
2241 | + cr.set_line_width(self.line_width) |
2242 | + cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border, |
2243 | + bounding_box_width, bounding_box_height) |
2244 | + cr.stroke() |
2245 | + |
2246 | + for idx,key in enumerate(self.series_labels): |
2247 | + #Draw color box |
2248 | + cr.set_source_rgba(*self.series_colors[idx][:4]) |
2249 | + cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, |
2250 | + self.border + color_box_height + (idx*max_height) , |
2251 | + color_box_width, color_box_height) |
2252 | + cr.fill() |
2253 | + |
2254 | + cr.set_source_rgba(0, 0, 0) |
2255 | + cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, |
2256 | + self.border + color_box_height + (idx*max_height), |
2257 | + color_box_width, color_box_height) |
2258 | + cr.stroke() |
2259 | + |
2260 | + #Draw series labels |
2261 | + cr.set_source_rgba(0, 0, 0) |
2262 | + cr.move_to(self.dimensions[HORZ] - self.border - max_width - 5, self.border + ((idx+1)*max_height)) |
2263 | + cr.show_text(key) |
2264 | + |
2265 | + |
2266 | +class HorizontalBarPlot(BarPlot): |
2267 | + def __init__(self, |
2268 | + surface = None, |
2269 | + data = None, |
2270 | + width = 640, |
2271 | + height = 480, |
2272 | + background = "white light_gray", |
2273 | + border = 0, |
2274 | + display_values = False, |
2275 | + grid = False, |
2276 | + rounded_corners = False, |
2277 | + stack = False, |
2278 | + three_dimension = False, |
2279 | + series_labels = None, |
2280 | + x_labels = None, |
2281 | + y_labels = None, |
2282 | + x_bounds = None, |
2283 | + y_bounds = None, |
2284 | + series_colors = None): |
2285 | + |
2286 | + BarPlot.__init__(self, surface, data, width, height, background, border, |
2287 | + display_values, grid, rounded_corners, stack, three_dimension, |
2288 | + x_labels, y_labels, x_bounds, y_bounds, series_colors, HORZ) |
2289 | + self.series_labels = series_labels |
2290 | + |
2291 | + def calc_vert_extents(self): |
2292 | + self.calc_extents(VERT) |
2293 | + if self.labels[HORZ] and not self.labels[VERT]: |
2294 | + self.borders[HORZ] += 10 |
2295 | + |
2296 | + def draw_rectangle_bottom(self, x0, y0, x1, y1): |
2297 | + self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) |
2298 | + self.context.line_to(x0, y0+5) |
2299 | + self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) |
2300 | + self.context.line_to(x1, y0) |
2301 | + self.context.line_to(x1, y1) |
2302 | + self.context.line_to(x0+5, y1) |
2303 | + self.context.close_path() |
2304 | + |
2305 | + def draw_rectangle_top(self, x0, y0, x1, y1): |
2306 | + self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) |
2307 | + self.context.line_to(x1, y1-5) |
2308 | + self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) |
2309 | + self.context.line_to(x0, y1) |
2310 | + self.context.line_to(x0, y0) |
2311 | + self.context.line_to(x1, y0) |
2312 | + self.context.close_path() |
2313 | + |
2314 | + def draw_rectangle(self, index, length, x0, y0, x1, y1): |
2315 | + if length == 1: |
2316 | + BarPlot.draw_rectangle(self, x0, y0, x1, y1) |
2317 | + elif index == 0: |
2318 | + self.draw_rectangle_bottom(x0, y0, x1, y1) |
2319 | + elif index == length-1: |
2320 | + self.draw_rectangle_top(x0, y0, x1, y1) |
2321 | + else: |
2322 | + self.context.rectangle(x0, y0, x1-x0, y1-y0) |
2323 | + |
2324 | + #TODO: Review BarPlot.render_grid code |
2325 | + def render_grid(self): |
2326 | + self.context.set_source_rgba(0.8, 0.8, 0.8) |
2327 | + if self.labels[HORZ]: |
2328 | + self.context.set_font_size(self.font_size * 0.8) |
2329 | + step = (self.dimensions[HORZ] - 2*self.borders[HORZ] - self.value_label)/(len(self.labels[HORZ])-1) |
2330 | + x = self.borders[HORZ] |
2331 | + next_x = 0 |
2332 | + for item in self.labels[HORZ]: |
2333 | + width = self.context.text_extents(item)[2] |
2334 | + if x - width/2 > next_x and x - width/2 > self.border: |
2335 | + self.context.move_to(x, self.border) |
2336 | + self.context.line_to(x, self.dimensions[VERT] - self.borders[VERT]) |
2337 | + self.context.stroke() |
2338 | + next_x = x + width/2 |
2339 | + x += step |
2340 | + else: |
2341 | + lines = 11 |
2342 | + horizontal_step = float(self.plot_dimensions[HORZ])/(lines-1) |
2343 | + x = self.borders[HORZ] |
2344 | + for y in xrange(0, lines): |
2345 | + self.context.move_to(x, self.border) |
2346 | + self.context.line_to(x, self.dimensions[VERT] - self.borders[VERT]) |
2347 | + self.context.stroke() |
2348 | + x += horizontal_step |
2349 | + |
2350 | + def render_horz_labels(self): |
2351 | + step = (self.dimensions[HORZ] - 2*self.borders[HORZ])/(len(self.labels[HORZ])-1) |
2352 | + x = self.borders[HORZ] |
2353 | + next_x = 0 |
2354 | + |
2355 | + for item in self.labels[HORZ]: |
2356 | + self.context.set_source_rgba(*self.label_color) |
2357 | + width = self.context.text_extents(item)[2] |
2358 | + if x - width/2 > next_x and x - width/2 > self.border: |
2359 | + self.context.move_to(x - width/2, self.dimensions[VERT] - self.borders[VERT] + self.max_value[HORZ] + 3) |
2360 | + self.context.show_text(item) |
2361 | + next_x = x + width/2 |
2362 | + x += step |
2363 | + |
2364 | + def render_vert_labels(self): |
2365 | + series_length = len(self.labels[VERT]) |
2366 | + step = (self.plot_dimensions[VERT] - (series_length + 1)*self.space)/(len(self.labels[VERT])) |
2367 | + y = self.border + step/2 + self.space |
2368 | + |
2369 | + for item in self.labels[VERT]: |
2370 | + self.context.set_source_rgba(*self.label_color) |
2371 | + width, height = self.context.text_extents(item)[2:4] |
2372 | + self.context.move_to(self.borders[HORZ] - width - 5, y + height/2) |
2373 | + self.context.show_text(item) |
2374 | + y += step + self.space |
2375 | + self.labels[VERT].reverse() |
2376 | + |
2377 | + def render_values(self): |
2378 | + self.context.set_source_rgba(*self.value_label_color) |
2379 | + self.context.set_font_size(self.font_size * 0.8) |
2380 | + if self.stack: |
2381 | + for i,group in enumerate(self.serie): |
2382 | + value = sum(group.to_list()) |
2383 | + height = self.context.text_extents(str(value))[3] |
2384 | + x = self.borders[HORZ] + value*self.steps[HORZ] + 2 |
2385 | + y = self.borders[VERT] + (i+0.5)*self.steps[VERT] + (i+1)*self.space + height/2 |
2386 | + self.context.move_to(x, y) |
2387 | + self.context.show_text(str(value)) |
2388 | + else: |
2389 | + for i,group in enumerate(self.serie): |
2390 | + inner_step = self.steps[VERT]/len(group) |
2391 | + y0 = self.border + i*self.steps[VERT] + (i+1)*self.space |
2392 | + for number,data in enumerate(group): |
2393 | + height = self.context.text_extents(str(data.content))[3] |
2394 | + self.context.move_to(self.borders[HORZ] + data.content*self.steps[HORZ] + 2, y0 + 0.5*inner_step + height/2, ) |
2395 | + self.context.show_text(str(data.content)) |
2396 | + y0 += inner_step |
2397 | + |
2398 | + def render_plot(self): |
2399 | + if self.stack: |
2400 | + for i,group in enumerate(self.serie): |
2401 | + x0 = self.borders[HORZ] |
2402 | + y0 = self.borders[VERT] + i*self.steps[VERT] + (i+1)*self.space |
2403 | + for number,data in enumerate(group): |
2404 | + if self.series_colors[number][4] in ('radial','linear') : |
2405 | + linear = cairo.LinearGradient( data.content*self.steps[HORZ]/2, y0, data.content*self.steps[HORZ]/2, y0 + self.steps[VERT] ) |
2406 | + color = self.series_colors[number] |
2407 | + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
2408 | + linear.add_color_stop_rgba(1.0, *color[:4]) |
2409 | + self.context.set_source(linear) |
2410 | + elif self.series_colors[number][4] == 'solid': |
2411 | + self.context.set_source_rgba(*self.series_colors[number][:4]) |
2412 | + if self.rounded_corners: |
2413 | + self.draw_rectangle(number, len(group), x0, y0, x0+data.content*self.steps[HORZ], y0+self.steps[VERT]) |
2414 | + self.context.fill() |
2415 | + else: |
2416 | + self.context.rectangle(x0, y0, data.content*self.steps[HORZ], self.steps[VERT]) |
2417 | + self.context.fill() |
2418 | + x0 += data.content*self.steps[HORZ] |
2419 | + else: |
2420 | + for i,group in enumerate(self.serie): |
2421 | + inner_step = self.steps[VERT]/len(group) |
2422 | + x0 = self.borders[HORZ] |
2423 | + y0 = self.border + i*self.steps[VERT] + (i+1)*self.space |
2424 | + for number,data in enumerate(group): |
2425 | + linear = cairo.LinearGradient(data.content*self.steps[HORZ]/2, y0, data.content*self.steps[HORZ]/2, y0 + inner_step) |
2426 | + color = self.series_colors[number] |
2427 | + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
2428 | + linear.add_color_stop_rgba(1.0, *color[:4]) |
2429 | + self.context.set_source(linear) |
2430 | + if self.rounded_corners and data.content != 0: |
2431 | + BarPlot.draw_round_rectangle(self,x0, y0, x0 + data.content*self.steps[HORZ], y0 + inner_step) |
2432 | + self.context.fill() |
2433 | + else: |
2434 | + self.context.rectangle(x0, y0, data.content*self.steps[HORZ], inner_step) |
2435 | + self.context.fill() |
2436 | + y0 += inner_step |
2437 | + |
2438 | +class VerticalBarPlot(BarPlot): |
2439 | + def __init__(self, |
2440 | + surface = None, |
2441 | + data = None, |
2442 | + width = 640, |
2443 | + height = 480, |
2444 | + background = "white light_gray", |
2445 | + border = 0, |
2446 | + display_values = False, |
2447 | + grid = False, |
2448 | + rounded_corners = False, |
2449 | + stack = False, |
2450 | + three_dimension = False, |
2451 | + series_labels = None, |
2452 | + x_labels = None, |
2453 | + y_labels = None, |
2454 | + x_bounds = None, |
2455 | + y_bounds = None, |
2456 | + series_colors = None): |
2457 | + |
2458 | + BarPlot.__init__(self, surface, data, width, height, background, border, |
2459 | + display_values, grid, rounded_corners, stack, three_dimension, |
2460 | + x_labels, y_labels, x_bounds, y_bounds, series_colors, VERT) |
2461 | + self.series_labels = series_labels |
2462 | + |
2463 | + def calc_vert_extents(self): |
2464 | + self.calc_extents(VERT) |
2465 | + if self.labels[VERT] and not self.labels[HORZ]: |
2466 | + self.borders[VERT] += 10 |
2467 | + |
2468 | + def draw_rectangle_bottom(self, x0, y0, x1, y1): |
2469 | + self.context.move_to(x1,y1) |
2470 | + self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) |
2471 | + self.context.line_to(x0+5, y1) |
2472 | + self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) |
2473 | + self.context.line_to(x0, y0) |
2474 | + self.context.line_to(x1, y0) |
2475 | + self.context.line_to(x1, y1) |
2476 | + self.context.close_path() |
2477 | + |
2478 | + def draw_rectangle_top(self, x0, y0, x1, y1): |
2479 | + self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) |
2480 | + self.context.line_to(x1-5, y0) |
2481 | + self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) |
2482 | + self.context.line_to(x1, y1) |
2483 | + self.context.line_to(x0, y1) |
2484 | + self.context.line_to(x0, y0) |
2485 | + self.context.close_path() |
2486 | + |
2487 | + def draw_rectangle(self, index, length, x0, y0, x1, y1): |
2488 | + if length == 1: |
2489 | + BarPlot.draw_rectangle(self, x0, y0, x1, y1) |
2490 | + elif index == 0: |
2491 | + self.draw_rectangle_bottom(x0, y0, x1, y1) |
2492 | + elif index == length-1: |
2493 | + self.draw_rectangle_top(x0, y0, x1, y1) |
2494 | + else: |
2495 | + self.context.rectangle(x0, y0, x1-x0, y1-y0) |
2496 | + |
2497 | + def render_grid(self): |
2498 | + self.context.set_source_rgba(0.8, 0.8, 0.8) |
2499 | + if self.labels[VERT]: |
2500 | + lines = len(self.labels[VERT]) |
2501 | + vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1) |
2502 | + y = self.borders[VERT] + self.value_label |
2503 | + else: |
2504 | + lines = 11 |
2505 | + vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1) |
2506 | + y = 1.2*self.border + self.value_label |
2507 | + for x in xrange(0, lines): |
2508 | + self.context.move_to(self.borders[HORZ], y) |
2509 | + self.context.line_to(self.dimensions[HORZ] - self.border, y) |
2510 | + self.context.stroke() |
2511 | + y += vertical_step |
2512 | + |
2513 | + def render_ground(self): |
2514 | + self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
2515 | + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
2516 | + self.context.fill() |
2517 | + |
2518 | + self.draw_3d_rectangle_side (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
2519 | + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
2520 | + self.context.fill() |
2521 | + |
2522 | + self.draw_3d_rectangle_top (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
2523 | + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
2524 | + self.context.fill() |
2525 | + |
2526 | + def render_horz_labels(self): |
2527 | + series_length = len(self.labels[HORZ]) |
2528 | + step = float (self.plot_dimensions[HORZ] - (series_length + 1)*self.space)/len(self.labels[HORZ]) |
2529 | + x = self.borders[HORZ] + step/2 + self.space |
2530 | + next_x = 0 |
2531 | + |
2532 | + for item in self.labels[HORZ]: |
2533 | + self.context.set_source_rgba(*self.label_color) |
2534 | + width = self.context.text_extents(item)[2] |
2535 | + if x - width/2 > next_x and x - width/2 > self.borders[HORZ]: |
2536 | + self.context.move_to(x - width/2, self.dimensions[VERT] - self.borders[VERT] + self.max_value[HORZ] + 3) |
2537 | + self.context.show_text(item) |
2538 | + next_x = x + width/2 |
2539 | + x += step + self.space |
2540 | + |
2541 | + def render_vert_labels(self): |
2542 | + self.context.set_source_rgba(*self.label_color) |
2543 | + y = self.borders[VERT] + self.value_label |
2544 | + step = (self.dimensions[VERT] - 2*self.borders[VERT] - self.value_label)/(len(self.labels[VERT]) - 1) |
2545 | + self.labels[VERT].reverse() |
2546 | + for item in self.labels[VERT]: |
2547 | + width, height = self.context.text_extents(item)[2:4] |
2548 | + self.context.move_to(self.borders[HORZ] - width - 5, y + height/2) |
2549 | + self.context.show_text(item) |
2550 | + y += step |
2551 | + self.labels[VERT].reverse() |
2552 | + |
2553 | + def render_values(self): |
2554 | + self.context.set_source_rgba(*self.value_label_color) |
2555 | + self.context.set_font_size(self.font_size * 0.8) |
2556 | + if self.stack: |
2557 | + for i,group in enumerate(self.serie): |
2558 | + value = sum(group.to_list()) |
2559 | + width = self.context.text_extents(str(value))[2] |
2560 | + x = self.borders[HORZ] + (i+0.5)*self.steps[HORZ] + (i+1)*self.space - width/2 |
2561 | + y = value*self.steps[VERT] + 2 |
2562 | + self.context.move_to(x, self.plot_top-y) |
2563 | + self.context.show_text(str(value)) |
2564 | + else: |
2565 | + for i,group in enumerate(self.serie): |
2566 | + inner_step = self.steps[HORZ]/len(group) |
2567 | + x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space |
2568 | + for number,data in enumerate(group): |
2569 | + width = self.context.text_extents(str(data.content))[2] |
2570 | + self.context.move_to(x0 + 0.5*inner_step - width/2, self.plot_top - data.content*self.steps[VERT] - 2) |
2571 | + self.context.show_text(str(data.content)) |
2572 | + x0 += inner_step |
2573 | + |
2574 | + def render_plot(self): |
2575 | + if self.stack: |
2576 | + for i,group in enumerate(self.serie): |
2577 | + x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space |
2578 | + y0 = 0 |
2579 | + for number,data in enumerate(group): |
2580 | + if self.series_colors[number][4] in ('linear','radial'): |
2581 | + linear = cairo.LinearGradient( x0, data.content*self.steps[VERT]/2, x0 + self.steps[HORZ], data.content*self.steps[VERT]/2 ) |
2582 | + color = self.series_colors[number] |
2583 | + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
2584 | + linear.add_color_stop_rgba(1.0, *color[:4]) |
2585 | + self.context.set_source(linear) |
2586 | + elif self.series_colors[number][4] == 'solid': |
2587 | + self.context.set_source_rgba(*self.series_colors[number][:4]) |
2588 | + if self.rounded_corners: |
2589 | + self.draw_rectangle(number, len(group), x0, self.plot_top - y0 - data.content*self.steps[VERT], x0 + self.steps[HORZ], self.plot_top - y0) |
2590 | + self.context.fill() |
2591 | + else: |
2592 | + self.context.rectangle(x0, self.plot_top - y0 - data.content*self.steps[VERT], self.steps[HORZ], data.content*self.steps[VERT]) |
2593 | + self.context.fill() |
2594 | + y0 += data.content*self.steps[VERT] |
2595 | + else: |
2596 | + for i,group in enumerate(self.serie): |
2597 | + inner_step = self.steps[HORZ]/len(group) |
2598 | + y0 = self.borders[VERT] |
2599 | + x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space |
2600 | + for number,data in enumerate(group): |
2601 | + if self.series_colors[number][4] == 'linear': |
2602 | + linear = cairo.LinearGradient( x0, data.content*self.steps[VERT]/2, x0 + inner_step, data.content*self.steps[VERT]/2 ) |
2603 | + color = self.series_colors[number] |
2604 | + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
2605 | + linear.add_color_stop_rgba(1.0, *color[:4]) |
2606 | + self.context.set_source(linear) |
2607 | + elif self.series_colors[number][4] == 'solid': |
2608 | + self.context.set_source_rgba(*self.series_colors[number][:4]) |
2609 | + if self.rounded_corners and data.content != 0: |
2610 | + BarPlot.draw_round_rectangle(self, x0, self.plot_top - data.content*self.steps[VERT], x0+inner_step, self.plot_top) |
2611 | + self.context.fill() |
2612 | + elif self.three_dimension: |
2613 | + self.draw_3d_rectangle_front(x0, self.plot_top - data.content*self.steps[VERT], x0+inner_step, self.plot_top, 5) |
2614 | + self.context.fill() |
2615 | + self.draw_3d_rectangle_side(x0, self.plot_top - data.content*self.steps[VERT], x0+inner_step, self.plot_top, 5) |
2616 | + self.context.fill() |
2617 | + self.draw_3d_rectangle_top(x0, self.plot_top - data.content*self.steps[VERT], x0+inner_step, self.plot_top, 5) |
2618 | + self.context.fill() |
2619 | + else: |
2620 | + self.context.rectangle(x0, self.plot_top - data.content*self.steps[VERT], inner_step, data.content*self.steps[VERT]) |
2621 | + self.context.fill() |
2622 | + |
2623 | + x0 += inner_step |
2624 | + |
2625 | +class StreamChart(VerticalBarPlot): |
2626 | + def __init__(self, |
2627 | + surface = None, |
2628 | + data = None, |
2629 | + width = 640, |
2630 | + height = 480, |
2631 | + background = "white light_gray", |
2632 | + border = 0, |
2633 | + grid = False, |
2634 | + series_legend = None, |
2635 | + x_labels = None, |
2636 | + x_bounds = None, |
2637 | + y_bounds = None, |
2638 | + series_colors = None): |
2639 | + |
2640 | + VerticalBarPlot.__init__(self, surface, data, width, height, background, border, |
2641 | + False, grid, False, True, False, |
2642 | + None, x_labels, None, x_bounds, y_bounds, series_colors) |
2643 | + |
2644 | + def calc_steps(self): |
2645 | + other_dir = other_direction(self.main_dir) |
2646 | + self.series_amplitude = self.bounds[self.main_dir][1] - self.bounds[self.main_dir][0] |
2647 | + if self.series_amplitude: |
2648 | + self.steps[self.main_dir] = float(self.plot_dimensions[self.main_dir])/self.series_amplitude |
2649 | + else: |
2650 | + self.steps[self.main_dir] = 0.00 |
2651 | + series_length = len(self.data) |
2652 | + self.steps[other_dir] = float(self.plot_dimensions[other_dir])/series_length |
2653 | + |
2654 | + def render_legend(self): |
2655 | + pass |
2656 | + |
2657 | + def ground(self, index): |
2658 | + sum_values = sum(self.data[index]) |
2659 | + return -0.5*sum_values |
2660 | + |
2661 | + def calc_angles(self): |
2662 | + middle = self.plot_top - self.plot_dimensions[VERT]/2.0 |
2663 | + self.angles = [tuple([0.0 for x in range(len(self.data)+1)])] |
2664 | + for x_index in range(1, len(self.data)-1): |
2665 | + t = [] |
2666 | + x0 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] |
2667 | + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] |
2668 | + y0 = middle - self.ground(x_index-1)*self.steps[VERT] |
2669 | + y2 = middle - self.ground(x_index+1)*self.steps[VERT] |
2670 | + t.append(math.atan(float(y0-y2)/(x0-x2))) |
2671 | + for data_index in range(len(self.data[x_index])): |
2672 | + x0 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] |
2673 | + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] |
2674 | + y0 = middle - self.ground(x_index-1)*self.steps[VERT] - self.data[x_index-1][data_index]*self.steps[VERT] |
2675 | + y2 = middle - self.ground(x_index+1)*self.steps[VERT] - self.data[x_index+1][data_index]*self.steps[VERT] |
2676 | + |
2677 | + for i in range(0,data_index): |
2678 | + y0 -= self.data[x_index-1][i]*self.steps[VERT] |
2679 | + y2 -= self.data[x_index+1][i]*self.steps[VERT] |
2680 | + |
2681 | + if data_index == len(self.data[0])-1 and False: |
2682 | + self.context.set_source_rgba(0.0,0.0,0.0,0.3) |
2683 | + self.context.move_to(x0,y0) |
2684 | + self.context.line_to(x2,y2) |
2685 | + self.context.stroke() |
2686 | + self.context.arc(x0,y0,2,0,2*math.pi) |
2687 | + self.context.fill() |
2688 | + t.append(math.atan(float(y0-y2)/(x0-x2))) |
2689 | + self.angles.append(tuple(t)) |
2690 | + self.angles.append(tuple([0.0 for x in range(len(self.data)+1)])) |
2691 | + |
2692 | + def render_plot(self): |
2693 | + self.calc_angles() |
2694 | + middle = self.plot_top - self.plot_dimensions[VERT]/2.0 |
2695 | + p = 0.4*self.steps[HORZ] |
2696 | + for data_index in range(len(self.data[0])-1,-1,-1): |
2697 | + self.context.set_source_rgba(*self.series_colors[data_index][:4]) |
2698 | + |
2699 | + #draw the upper line |
2700 | + for x_index in range(len(self.data)-1) : |
2701 | + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] |
2702 | + y1 = middle - self.ground(x_index)*self.steps[VERT] - self.data[x_index][data_index]*self.steps[VERT] |
2703 | + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] |
2704 | + y2 = middle - self.ground(x_index + 1)*self.steps[VERT] - self.data[x_index + 1][data_index]*self.steps[VERT] |
2705 | + |
2706 | + for i in range(0,data_index): |
2707 | + y1 -= self.data[x_index][i]*self.steps[VERT] |
2708 | + y2 -= self.data[x_index+1][i]*self.steps[VERT] |
2709 | + |
2710 | + if x_index == 0: |
2711 | + self.context.move_to(x1,y1) |
2712 | + |
2713 | + ang1 = self.angles[x_index][data_index+1] |
2714 | + ang2 = self.angles[x_index+1][data_index+1] + math.pi |
2715 | + self.context.curve_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1), |
2716 | + x2+p*math.cos(ang2),y2+p*math.sin(ang2), |
2717 | + x2,y2) |
2718 | + |
2719 | + for x_index in range(len(self.data)-1,0,-1) : |
2720 | + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] |
2721 | + y1 = middle - self.ground(x_index)*self.steps[VERT] |
2722 | + x2 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] |
2723 | + y2 = middle - self.ground(x_index - 1)*self.steps[VERT] |
2724 | + |
2725 | + for i in range(0,data_index): |
2726 | + y1 -= self.data[x_index][i]*self.steps[VERT] |
2727 | + y2 -= self.data[x_index-1][i]*self.steps[VERT] |
2728 | + |
2729 | + if x_index == len(self.data)-1: |
2730 | + self.context.line_to(x1,y1+2) |
2731 | + |
2732 | + #revert angles by pi degrees to take the turn back |
2733 | + ang1 = self.angles[x_index][data_index] + math.pi |
2734 | + ang2 = self.angles[x_index-1][data_index] |
2735 | + self.context.curve_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1), |
2736 | + x2+p*math.cos(ang2),y2+p*math.sin(ang2), |
2737 | + x2,y2+2) |
2738 | + |
2739 | + self.context.close_path() |
2740 | + self.context.fill() |
2741 | + |
2742 | + if False: |
2743 | + self.context.move_to(self.borders[HORZ] + 0.5*self.steps[HORZ], middle) |
2744 | + for x_index in range(len(self.data)-1) : |
2745 | + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] |
2746 | + y1 = middle - self.ground(x_index)*self.steps[VERT] - self.data[x_index][data_index]*self.steps[VERT] |
2747 | + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] |
2748 | + y2 = middle - self.ground(x_index + 1)*self.steps[VERT] - self.data[x_index + 1][data_index]*self.steps[VERT] |
2749 | + |
2750 | + for i in range(0,data_index): |
2751 | + y1 -= self.data[x_index][i]*self.steps[VERT] |
2752 | + y2 -= self.data[x_index+1][i]*self.steps[VERT] |
2753 | + |
2754 | + ang1 = self.angles[x_index][data_index+1] |
2755 | + ang2 = self.angles[x_index+1][data_index+1] + math.pi |
2756 | + self.context.set_source_rgba(1.0,0.0,0.0) |
2757 | + self.context.arc(x1+p*math.cos(ang1),y1+p*math.sin(ang1),2,0,2*math.pi) |
2758 | + self.context.fill() |
2759 | + self.context.set_source_rgba(0.0,0.0,0.0) |
2760 | + self.context.arc(x2+p*math.cos(ang2),y2+p*math.sin(ang2),2,0,2*math.pi) |
2761 | + self.context.fill() |
2762 | + '''self.context.set_source_rgba(0.0,0.0,0.0,0.3) |
2763 | + self.context.arc(x2,y2,2,0,2*math.pi) |
2764 | + self.context.fill()''' |
2765 | + self.context.move_to(x1,y1) |
2766 | + self.context.line_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1)) |
2767 | + self.context.stroke() |
2768 | + self.context.move_to(x2,y2) |
2769 | + self.context.line_to(x2+p*math.cos(ang2),y2+p*math.sin(ang2)) |
2770 | + self.context.stroke() |
2771 | + if False: |
2772 | + for x_index in range(len(self.data)-1,0,-1) : |
2773 | + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] |
2774 | + y1 = middle - self.ground(x_index)*self.steps[VERT] |
2775 | + x2 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] |
2776 | + y2 = middle - self.ground(x_index - 1)*self.steps[VERT] |
2777 | + |
2778 | + for i in range(0,data_index): |
2779 | + y1 -= self.data[x_index][i]*self.steps[VERT] |
2780 | + y2 -= self.data[x_index-1][i]*self.steps[VERT] |
2781 | + |
2782 | + #revert angles by pi degrees to take the turn back |
2783 | + ang1 = self.angles[x_index][data_index] + math.pi |
2784 | + ang2 = self.angles[x_index-1][data_index] |
2785 | + self.context.set_source_rgba(0.0,1.0,0.0) |
2786 | + self.context.arc(x1+p*math.cos(ang1),y1+p*math.sin(ang1),2,0,2*math.pi) |
2787 | + self.context.fill() |
2788 | + self.context.set_source_rgba(0.0,0.0,1.0) |
2789 | + self.context.arc(x2+p*math.cos(ang2),y2+p*math.sin(ang2),2,0,2*math.pi) |
2790 | + self.context.fill() |
2791 | + '''self.context.set_source_rgba(0.0,0.0,0.0,0.3) |
2792 | + self.context.arc(x2,y2,2,0,2*math.pi) |
2793 | + self.context.fill()''' |
2794 | + self.context.move_to(x1,y1) |
2795 | + self.context.line_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1)) |
2796 | + self.context.stroke() |
2797 | + self.context.move_to(x2,y2) |
2798 | + self.context.line_to(x2+p*math.cos(ang2),y2+p*math.sin(ang2)) |
2799 | + self.context.stroke() |
2800 | + #break |
2801 | + |
2802 | + #self.context.arc(self.dimensions[HORZ]/2, self.dimensions[VERT]/2,50,0,3*math.pi/2) |
2803 | + #self.context.fill() |
2804 | + |
2805 | + |
2806 | +class PiePlot(Plot): |
2807 | + #TODO: Check the old cairoplot, graphs aren't matching |
2808 | + def __init__ (self, |
2809 | + surface = None, |
2810 | + data = None, |
2811 | + width = 640, |
2812 | + height = 480, |
2813 | + background = "white light_gray", |
2814 | + gradient = False, |
2815 | + shadow = False, |
2816 | + colors = None): |
2817 | + |
2818 | + Plot.__init__( self, surface, data, width, height, background, series_colors = colors ) |
2819 | + self.center = (self.dimensions[HORZ]/2, self.dimensions[VERT]/2) |
2820 | + self.total = sum( self.serie.to_list() ) |
2821 | + self.radius = min(self.dimensions[HORZ]/3,self.dimensions[VERT]/3) |
2822 | + self.gradient = gradient |
2823 | + self.shadow = shadow |
2824 | + |
2825 | + def sort_function(x,y): |
2826 | + return x.content - y.content |
2827 | + |
2828 | + def load_series(self, data, x_labels=None, y_labels=None, series_colors=None): |
2829 | + Plot.load_series(self, data, x_labels, y_labels, series_colors) |
2830 | + # Already done inside series |
2831 | + #self.data = sorted(self.data) |
2832 | + |
2833 | + def draw_piece(self, angle, next_angle): |
2834 | + self.context.move_to(self.center[0],self.center[1]) |
2835 | + self.context.line_to(self.center[0] + self.radius*math.cos(angle), self.center[1] + self.radius*math.sin(angle)) |
2836 | + self.context.arc(self.center[0], self.center[1], self.radius, angle, next_angle) |
2837 | + self.context.line_to(self.center[0], self.center[1]) |
2838 | + self.context.close_path() |
2839 | + |
2840 | + def render(self): |
2841 | + self.render_background() |
2842 | + self.render_bounding_box() |
2843 | + if self.shadow: |
2844 | + self.render_shadow() |
2845 | + self.render_plot() |
2846 | + self.render_series_labels() |
2847 | + |
2848 | + def render_shadow(self): |
2849 | + horizontal_shift = 3 |
2850 | + vertical_shift = 3 |
2851 | + self.context.set_source_rgba(0, 0, 0, 0.5) |
2852 | + self.context.arc(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.radius, 0, 2*math.pi) |
2853 | + self.context.fill() |
2854 | + |
2855 | + def render_series_labels(self): |
2856 | + angle = 0 |
2857 | + next_angle = 0 |
2858 | + x0,y0 = self.center |
2859 | + cr = self.context |
2860 | + for number,key in enumerate(self.series_labels): |
2861 | + # self.data[number] should be just a number |
2862 | + data = sum(self.serie[number].to_list()) |
2863 | + |
2864 | + next_angle = angle + 2.0*math.pi*data/self.total |
2865 | + cr.set_source_rgba(*self.series_colors[number][:4]) |
2866 | + w = cr.text_extents(key)[2] |
2867 | + if (angle + next_angle)/2 < math.pi/2 or (angle + next_angle)/2 > 3*math.pi/2: |
2868 | + cr.move_to(x0 + (self.radius+10)*math.cos((angle+next_angle)/2), y0 + (self.radius+10)*math.sin((angle+next_angle)/2) ) |
2869 | + else: |
2870 | + cr.move_to(x0 + (self.radius+10)*math.cos((angle+next_angle)/2) - w, y0 + (self.radius+10)*math.sin((angle+next_angle)/2) ) |
2871 | + cr.show_text(key) |
2872 | + angle = next_angle |
2873 | + |
2874 | + def render_plot(self): |
2875 | + angle = 0 |
2876 | + next_angle = 0 |
2877 | + x0,y0 = self.center |
2878 | + cr = self.context |
2879 | + for number,group in enumerate(self.serie): |
2880 | + # Group should be just a number |
2881 | + data = sum(group.to_list()) |
2882 | + next_angle = angle + 2.0*math.pi*data/self.total |
2883 | + if self.gradient or self.series_colors[number][4] in ('linear','radial'): |
2884 | + gradient_color = cairo.RadialGradient(self.center[0], self.center[1], 0, self.center[0], self.center[1], self.radius) |
2885 | + gradient_color.add_color_stop_rgba(0.3, *self.series_colors[number][:4]) |
2886 | + gradient_color.add_color_stop_rgba(1, self.series_colors[number][0]*0.7, |
2887 | + self.series_colors[number][1]*0.7, |
2888 | + self.series_colors[number][2]*0.7, |
2889 | + self.series_colors[number][3]) |
2890 | + cr.set_source(gradient_color) |
2891 | + else: |
2892 | + cr.set_source_rgba(*self.series_colors[number][:4]) |
2893 | + |
2894 | + self.draw_piece(angle, next_angle) |
2895 | + cr.fill() |
2896 | + |
2897 | + cr.set_source_rgba(1.0, 1.0, 1.0) |
2898 | + self.draw_piece(angle, next_angle) |
2899 | + cr.stroke() |
2900 | + |
2901 | + angle = next_angle |
2902 | + |
2903 | +class DonutPlot(PiePlot): |
2904 | + def __init__ (self, |
2905 | + surface = None, |
2906 | + data = None, |
2907 | + width = 640, |
2908 | + height = 480, |
2909 | + background = "white light_gray", |
2910 | + gradient = False, |
2911 | + shadow = False, |
2912 | + colors = None, |
2913 | + inner_radius=-1): |
2914 | + |
2915 | + Plot.__init__( self, surface, data, width, height, background, series_colors = colors ) |
2916 | + |
2917 | + self.center = ( self.dimensions[HORZ]/2, self.dimensions[VERT]/2 ) |
2918 | + self.total = sum( self.serie.to_list() ) |
2919 | + self.radius = min( self.dimensions[HORZ]/3,self.dimensions[VERT]/3 ) |
2920 | + self.inner_radius = inner_radius*self.radius |
2921 | + |
2922 | + if inner_radius == -1: |
2923 | + self.inner_radius = self.radius/3 |
2924 | + |
2925 | + self.gradient = gradient |
2926 | + self.shadow = shadow |
2927 | + |
2928 | + def draw_piece(self, angle, next_angle): |
2929 | + self.context.move_to(self.center[0] + (self.inner_radius)*math.cos(angle), self.center[1] + (self.inner_radius)*math.sin(angle)) |
2930 | + self.context.line_to(self.center[0] + self.radius*math.cos(angle), self.center[1] + self.radius*math.sin(angle)) |
2931 | + self.context.arc(self.center[0], self.center[1], self.radius, angle, next_angle) |
2932 | + self.context.line_to(self.center[0] + (self.inner_radius)*math.cos(next_angle), self.center[1] + (self.inner_radius)*math.sin(next_angle)) |
2933 | + self.context.arc_negative(self.center[0], self.center[1], self.inner_radius, next_angle, angle) |
2934 | + self.context.close_path() |
2935 | + |
2936 | + def render_shadow(self): |
2937 | + horizontal_shift = 3 |
2938 | + vertical_shift = 3 |
2939 | + self.context.set_source_rgba(0, 0, 0, 0.5) |
2940 | + self.context.arc(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.inner_radius, 0, 2*math.pi) |
2941 | + self.context.arc_negative(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.radius, 0, -2*math.pi) |
2942 | + self.context.fill() |
2943 | + |
2944 | +class GanttChart (Plot) : |
2945 | + def __init__(self, |
2946 | + surface = None, |
2947 | + data = None, |
2948 | + width = 640, |
2949 | + height = 480, |
2950 | + x_labels = None, |
2951 | + y_labels = None, |
2952 | + colors = None): |
2953 | + self.bounds = {} |
2954 | + self.max_value = {} |
2955 | + Plot.__init__(self, surface, data, width, height, x_labels = x_labels, y_labels = y_labels, series_colors = colors) |
2956 | + |
2957 | + def load_series(self, data, x_labels=None, y_labels=None, series_colors=None): |
2958 | + Plot.load_series(self, data, x_labels, y_labels, series_colors) |
2959 | + self.calc_boundaries() |
2960 | + |
2961 | + def calc_boundaries(self): |
2962 | + self.bounds[HORZ] = (0,len(self.serie)) |
2963 | + end_pos = max(self.serie.to_list()) |
2964 | + |
2965 | + #for group in self.serie: |
2966 | + # if hasattr(item, "__delitem__"): |
2967 | + # for sub_item in item: |
2968 | + # end_pos = max(sub_item) |
2969 | + # else: |
2970 | + # end_pos = max(item) |
2971 | + self.bounds[VERT] = (0,end_pos) |
2972 | + |
2973 | + def calc_extents(self, direction): |
2974 | + self.max_value[direction] = 0 |
2975 | + if self.labels[direction]: |
2976 | + self.max_value[direction] = max(self.context.text_extents(item)[2] for item in self.labels[direction]) |
2977 | + else: |
2978 | + self.max_value[direction] = self.context.text_extents( str(self.bounds[direction][1] + 1) )[2] |
2979 | + |
2980 | + def calc_horz_extents(self): |
2981 | + self.calc_extents(HORZ) |
2982 | + self.borders[HORZ] = 100 + self.max_value[HORZ] |
2983 | + |
2984 | + def calc_vert_extents(self): |
2985 | + self.calc_extents(VERT) |
2986 | + self.borders[VERT] = self.dimensions[VERT]/(self.bounds[HORZ][1] + 1) |
2987 | + |
2988 | + def calc_steps(self): |
2989 | + self.horizontal_step = (self.dimensions[HORZ] - self.borders[HORZ])/(len(self.labels[VERT])) |
2990 | + self.vertical_step = self.borders[VERT] |
2991 | + |
2992 | + def render(self): |
2993 | + self.calc_horz_extents() |
2994 | + self.calc_vert_extents() |
2995 | + self.calc_steps() |
2996 | + self.render_background() |
2997 | + |
2998 | + self.render_labels() |
2999 | + self.render_grid() |
3000 | + self.render_plot() |
3001 | + |
3002 | + def render_background(self): |
3003 | + cr = self.context |
3004 | + cr.set_source_rgba(255,255,255) |
3005 | + cr.rectangle(0,0,self.dimensions[HORZ], self.dimensions[VERT]) |
3006 | + cr.fill() |
3007 | + for number,group in enumerate(self.serie): |
3008 | + linear = cairo.LinearGradient(self.dimensions[HORZ]/2, self.borders[VERT] + number*self.vertical_step, |
3009 | + self.dimensions[HORZ]/2, self.borders[VERT] + (number+1)*self.vertical_step) |
3010 | + linear.add_color_stop_rgba(0,1.0,1.0,1.0,1.0) |
3011 | + linear.add_color_stop_rgba(1.0,0.9,0.9,0.9,1.0) |
3012 | + cr.set_source(linear) |
3013 | + cr.rectangle(0,self.borders[VERT] + number*self.vertical_step,self.dimensions[HORZ],self.vertical_step) |
3014 | + cr.fill() |
3015 | + |
3016 | + def render_grid(self): |
3017 | + cr = self.context |
3018 | + cr.set_source_rgba(0.7, 0.7, 0.7) |
3019 | + cr.set_dash((1,0,0,0,0,0,1)) |
3020 | + cr.set_line_width(0.5) |
3021 | + for number,label in enumerate(self.labels[VERT]): |
3022 | + h = cr.text_extents(label)[3] |
3023 | + cr.move_to(self.borders[HORZ] + number*self.horizontal_step, self.vertical_step/2 + h) |
3024 | + cr.line_to(self.borders[HORZ] + number*self.horizontal_step, self.dimensions[VERT]) |
3025 | + cr.stroke() |
3026 | + |
3027 | + def render_labels(self): |
3028 | + self.context.set_font_size(0.02 * self.dimensions[HORZ]) |
3029 | + |
3030 | + self.render_horz_labels() |
3031 | + self.render_vert_labels() |
3032 | + |
3033 | + def render_horz_labels(self): |
3034 | + cr = self.context |
3035 | + labels = self.labels[HORZ] |
3036 | + if not labels: |
3037 | + labels = [str(i) for i in range(1, self.bounds[HORZ][1] + 1) ] |
3038 | + for number,label in enumerate(labels): |
3039 | + if label != None: |
3040 | + cr.set_source_rgba(0.5, 0.5, 0.5) |
3041 | + w,h = cr.text_extents(label)[2], cr.text_extents(label)[3] |
3042 | + cr.move_to(40,self.borders[VERT] + number*self.vertical_step + self.vertical_step/2 + h/2) |
3043 | + cr.show_text(label) |
3044 | + |
3045 | + def render_vert_labels(self): |
3046 | + cr = self.context |
3047 | + labels = self.labels[VERT] |
3048 | + if not labels: |
3049 | + labels = [str(i) for i in range(1, self.bounds[VERT][1] + 1) ] |
3050 | + for number,label in enumerate(labels): |
3051 | + w,h = cr.text_extents(label)[2], cr.text_extents(label)[3] |
3052 | + cr.move_to(self.borders[HORZ] + number*self.horizontal_step - w/2, self.vertical_step/2) |
3053 | + cr.show_text(label) |
3054 | + |
3055 | + def render_rectangle(self, x0, y0, x1, y1, color): |
3056 | + self.draw_shadow(x0, y0, x1, y1) |
3057 | + self.draw_rectangle(x0, y0, x1, y1, color) |
3058 | + |
3059 | + def draw_rectangular_shadow(self, gradient, x0, y0, w, h): |
3060 | + self.context.set_source(gradient) |
3061 | + self.context.rectangle(x0,y0,w,h) |
3062 | + self.context.fill() |
3063 | + |
3064 | + def draw_circular_shadow(self, x, y, radius, ang_start, ang_end, mult, shadow): |
3065 | + gradient = cairo.RadialGradient(x, y, 0, x, y, 2*radius) |
3066 | + gradient.add_color_stop_rgba(0, 0, 0, 0, shadow) |
3067 | + gradient.add_color_stop_rgba(1, 0, 0, 0, 0) |
3068 | + self.context.set_source(gradient) |
3069 | + self.context.move_to(x,y) |
3070 | + self.context.line_to(x + mult[0]*radius,y + mult[1]*radius) |
3071 | + self.context.arc(x, y, 8, ang_start, ang_end) |
3072 | + self.context.line_to(x,y) |
3073 | + self.context.close_path() |
3074 | + self.context.fill() |
3075 | + |
3076 | + def draw_rectangle(self, x0, y0, x1, y1, color): |
3077 | + cr = self.context |
3078 | + middle = (x0+x1)/2 |
3079 | + linear = cairo.LinearGradient(middle,y0,middle,y1) |
3080 | + linear.add_color_stop_rgba(0,3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
3081 | + linear.add_color_stop_rgba(1,*color[:4]) |
3082 | + cr.set_source(linear) |
3083 | + |
3084 | + cr.arc(x0+5, y0+5, 5, 0, 2*math.pi) |
3085 | + cr.arc(x1-5, y0+5, 5, 0, 2*math.pi) |
3086 | + cr.arc(x0+5, y1-5, 5, 0, 2*math.pi) |
3087 | + cr.arc(x1-5, y1-5, 5, 0, 2*math.pi) |
3088 | + cr.rectangle(x0+5,y0,x1-x0-10,y1-y0) |
3089 | + cr.rectangle(x0,y0+5,x1-x0,y1-y0-10) |
3090 | + cr.fill() |
3091 | + |
3092 | + def draw_shadow(self, x0, y0, x1, y1): |
3093 | + shadow = 0.4 |
3094 | + h_mid = (x0+x1)/2 |
3095 | + v_mid = (y0+y1)/2 |
3096 | + h_linear_1 = cairo.LinearGradient(h_mid,y0-4,h_mid,y0+4) |
3097 | + h_linear_2 = cairo.LinearGradient(h_mid,y1-4,h_mid,y1+4) |
3098 | + v_linear_1 = cairo.LinearGradient(x0-4,v_mid,x0+4,v_mid) |
3099 | + v_linear_2 = cairo.LinearGradient(x1-4,v_mid,x1+4,v_mid) |
3100 | + |
3101 | + h_linear_1.add_color_stop_rgba( 0, 0, 0, 0, 0) |
3102 | + h_linear_1.add_color_stop_rgba( 1, 0, 0, 0, shadow) |
3103 | + h_linear_2.add_color_stop_rgba( 0, 0, 0, 0, shadow) |
3104 | + h_linear_2.add_color_stop_rgba( 1, 0, 0, 0, 0) |
3105 | + v_linear_1.add_color_stop_rgba( 0, 0, 0, 0, 0) |
3106 | + v_linear_1.add_color_stop_rgba( 1, 0, 0, 0, shadow) |
3107 | + v_linear_2.add_color_stop_rgba( 0, 0, 0, 0, shadow) |
3108 | + v_linear_2.add_color_stop_rgba( 1, 0, 0, 0, 0) |
3109 | + |
3110 | + self.draw_rectangular_shadow(h_linear_1,x0+4,y0-4,x1-x0-8,8) |
3111 | + self.draw_rectangular_shadow(h_linear_2,x0+4,y1-4,x1-x0-8,8) |
3112 | + self.draw_rectangular_shadow(v_linear_1,x0-4,y0+4,8,y1-y0-8) |
3113 | + self.draw_rectangular_shadow(v_linear_2,x1-4,y0+4,8,y1-y0-8) |
3114 | + |
3115 | + self.draw_circular_shadow(x0+4, y0+4, 4, math.pi, 3*math.pi/2, (-1,0), shadow) |
3116 | + self.draw_circular_shadow(x1-4, y0+4, 4, 3*math.pi/2, 2*math.pi, (0,-1), shadow) |
3117 | + self.draw_circular_shadow(x0+4, y1-4, 4, math.pi/2, math.pi, (0,1), shadow) |
3118 | + self.draw_circular_shadow(x1-4, y1-4, 4, 0, math.pi/2, (1,0), shadow) |
3119 | + |
3120 | + def render_plot(self): |
3121 | + for index,group in enumerate(self.serie): |
3122 | + for data in group: |
3123 | + self.render_rectangle(self.borders[HORZ] + data.content[0]*self.horizontal_step, |
3124 | + self.borders[VERT] + index*self.vertical_step + self.vertical_step/4.0, |
3125 | + self.borders[HORZ] + data.content[1]*self.horizontal_step, |
3126 | + self.borders[VERT] + index*self.vertical_step + 3.0*self.vertical_step/4.0, |
3127 | + self.series_colors[index]) |
3128 | + |
3129 | +# Function definition |
3130 | + |
3131 | +def scatter_plot(name, |
3132 | + data = None, |
3133 | + errorx = None, |
3134 | + errory = None, |
3135 | + width = 640, |
3136 | + height = 480, |
3137 | + background = "white light_gray", |
3138 | + border = 0, |
3139 | + axis = False, |
3140 | + dash = False, |
3141 | + discrete = False, |
3142 | + dots = False, |
3143 | + grid = False, |
3144 | + series_legend = False, |
3145 | + x_labels = None, |
3146 | + y_labels = None, |
3147 | + x_bounds = None, |
3148 | + y_bounds = None, |
3149 | + z_bounds = None, |
3150 | + x_title = None, |
3151 | + y_title = None, |
3152 | + series_colors = None, |
3153 | + circle_colors = None): |
3154 | + |
3155 | + ''' |
3156 | + - Function to plot scatter data. |
3157 | + |
3158 | + - Parameters |
3159 | + |
3160 | + data - The values to be ploted might be passed in a two basic: |
3161 | + list of points: [(0,0), (0,1), (0,2)] or [(0,0,1), (0,1,4), (0,2,1)] |
3162 | + lists of coordinates: [ [0,0,0] , [0,1,2] ] or [ [0,0,0] , [0,1,2] , [1,4,1] ] |
3163 | + Notice that these kinds of that can be grouped in order to form more complex data |
3164 | + using lists of lists or dictionaries; |
3165 | + series_colors - Define color values for each of the series |
3166 | + circle_colors - Define a lower and an upper bound for the circle colors for variable radius |
3167 | + (3 dimensions) series |
3168 | + ''' |
3169 | + |
3170 | + plot = ScatterPlot( name, data, errorx, errory, width, height, background, border, |
3171 | + axis, dash, discrete, dots, grid, series_legend, x_labels, y_labels, |
3172 | + x_bounds, y_bounds, z_bounds, x_title, y_title, series_colors, circle_colors ) |
3173 | + plot.render() |
3174 | + plot.commit() |
3175 | + |
3176 | +def dot_line_plot(name, |
3177 | + data, |
3178 | + width, |
3179 | + height, |
3180 | + background = "white light_gray", |
3181 | + border = 0, |
3182 | + axis = False, |
3183 | + dash = False, |
3184 | + dots = False, |
3185 | + grid = False, |
3186 | + series_legend = False, |
3187 | + x_labels = None, |
3188 | + y_labels = None, |
3189 | + x_bounds = None, |
3190 | + y_bounds = None, |
3191 | + x_title = None, |
3192 | + y_title = None, |
3193 | + series_colors = None): |
3194 | + ''' |
3195 | + - Function to plot graphics using dots and lines. |
3196 | + |
3197 | + dot_line_plot (name, data, width, height, background = "white light_gray", border = 0, axis = False, grid = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None) |
3198 | + |
3199 | + - Parameters |
3200 | + |
3201 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; |
3202 | + data - The list, list of lists or dictionary holding the data to be plotted; |
3203 | + width, height - Dimensions of the output image; |
3204 | + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. |
3205 | + If left None, a gray to white gradient will be generated; |
3206 | + border - Distance in pixels of a square border into which the graphics will be drawn; |
3207 | + axis - Whether or not the axis are to be drawn; |
3208 | + dash - Boolean or a list or a dictionary of booleans indicating whether or not the associated series should be drawn in dashed mode; |
3209 | + dots - Whether or not dots should be drawn on each point; |
3210 | + grid - Whether or not the gris is to be drawn; |
3211 | + series_legend - Whether or not the legend is to be drawn; |
3212 | + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; |
3213 | + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; |
3214 | + x_title - Whether or not to plot a title over the x axis. |
3215 | + y_title - Whether or not to plot a title over the y axis. |
3216 | + |
3217 | + - Examples of use |
3218 | + |
3219 | + data = [0, 1, 3, 8, 9, 0, 10, 10, 2, 1] |
3220 | + CairoPlot.dot_line_plot('teste', data, 400, 300) |
3221 | + |
3222 | + data = { "john" : [10, 10, 10, 10, 30], "mary" : [0, 0, 3, 5, 15], "philip" : [13, 32, 11, 25, 2] } |
3223 | + x_labels = ["jan/2008", "feb/2008", "mar/2008", "apr/2008", "may/2008" ] |
3224 | + CairoPlot.dot_line_plot( 'test', data, 400, 300, axis = True, grid = True, |
3225 | + series_legend = True, x_labels = x_labels ) |
3226 | + ''' |
3227 | + plot = DotLinePlot( name, data, width, height, background, border, |
3228 | + axis, dash, dots, grid, series_legend, x_labels, y_labels, |
3229 | + x_bounds, y_bounds, x_title, y_title, series_colors ) |
3230 | + plot.render() |
3231 | + plot.commit() |
3232 | + |
3233 | +def function_plot(name, |
3234 | + data, |
3235 | + width, |
3236 | + height, |
3237 | + background = "white light_gray", |
3238 | + border = 0, |
3239 | + axis = True, |
3240 | + dots = False, |
3241 | + discrete = False, |
3242 | + grid = False, |
3243 | + series_legend = False, |
3244 | + x_labels = None, |
3245 | + y_labels = None, |
3246 | + x_bounds = None, |
3247 | + y_bounds = None, |
3248 | + x_title = None, |
3249 | + y_title = None, |
3250 | + series_colors = None, |
3251 | + step = 1): |
3252 | + |
3253 | + ''' |
3254 | + - Function to plot functions. |
3255 | + |
3256 | + function_plot(name, data, width, height, background = "white light_gray", border = 0, axis = True, grid = False, dots = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None, step = 1, discrete = False) |
3257 | + |
3258 | + - Parameters |
3259 | + |
3260 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; |
3261 | + data - The list, list of lists or dictionary holding the data to be plotted; |
3262 | + width, height - Dimensions of the output image; |
3263 | + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. |
3264 | + If left None, a gray to white gradient will be generated; |
3265 | + border - Distance in pixels of a square border into which the graphics will be drawn; |
3266 | + axis - Whether or not the axis are to be drawn; |
3267 | + grid - Whether or not the gris is to be drawn; |
3268 | + dots - Whether or not dots should be shown at each point; |
3269 | + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; |
3270 | + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; |
3271 | + step - the horizontal distance from one point to the other. The smaller, the smoother the curve will be; |
3272 | + discrete - whether or not the function should be plotted in discrete format. |
3273 | + |
3274 | + - Example of use |
3275 | + |
3276 | + data = lambda x : x**2 |
3277 | + CairoPlot.function_plot('function4', data, 400, 300, grid = True, x_bounds=(-10,10), step = 0.1) |
3278 | + ''' |
3279 | + |
3280 | + plot = FunctionPlot( name, data, width, height, background, border, |
3281 | + axis, discrete, dots, grid, series_legend, x_labels, y_labels, |
3282 | + x_bounds, y_bounds, x_title, y_title, series_colors, step ) |
3283 | + plot.render() |
3284 | + plot.commit() |
3285 | + |
3286 | +def pie_plot( name, data, width, height, background = "white light_gray", gradient = False, shadow = False, colors = None ): |
3287 | + |
3288 | + ''' |
3289 | + - Function to plot pie graphics. |
3290 | + |
3291 | + pie_plot(name, data, width, height, background = "white light_gray", gradient = False, colors = None) |
3292 | + |
3293 | + - Parameters |
3294 | + |
3295 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; |
3296 | + data - The list, list of lists or dictionary holding the data to be plotted; |
3297 | + width, height - Dimensions of the output image; |
3298 | + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. |
3299 | + If left None, a gray to white gradient will be generated; |
3300 | + gradient - Whether or not the pie color will be painted with a gradient; |
3301 | + shadow - Whether or not there will be a shadow behind the pie; |
3302 | + colors - List of slices colors. |
3303 | + |
3304 | + - Example of use |
3305 | + |
3306 | + teste_data = {"john" : 123, "mary" : 489, "philip" : 890 , "suzy" : 235} |
3307 | + CairoPlot.pie_plot("pie_teste", teste_data, 500, 500) |
3308 | + ''' |
3309 | + |
3310 | + plot = PiePlot( name, data, width, height, background, gradient, shadow, colors ) |
3311 | + plot.render() |
3312 | + plot.commit() |
3313 | + |
3314 | +def donut_plot(name, data, width, height, background = "white light_gray", gradient = False, shadow = False, colors = None, inner_radius = -1): |
3315 | + |
3316 | + ''' |
3317 | + - Function to plot donut graphics. |
3318 | + |
3319 | + donut_plot(name, data, width, height, background = "white light_gray", gradient = False, inner_radius = -1) |
3320 | + |
3321 | + - Parameters |
3322 | + |
3323 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; |
3324 | + data - The list, list of lists or dictionary holding the data to be plotted; |
3325 | + width, height - Dimensions of the output image; |
3326 | + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. |
3327 | + If left None, a gray to white gradient will be generated; |
3328 | + shadow - Whether or not there will be a shadow behind the donut; |
3329 | + gradient - Whether or not the donut color will be painted with a gradient; |
3330 | + colors - List of slices colors; |
3331 | + inner_radius - The radius of the donut's inner circle. |
3332 | + |
3333 | + - Example of use |
3334 | + |
3335 | + teste_data = {"john" : 123, "mary" : 489, "philip" : 890 , "suzy" : 235} |
3336 | + CairoPlot.donut_plot("donut_teste", teste_data, 500, 500) |
3337 | + ''' |
3338 | + |
3339 | + plot = DonutPlot(name, data, width, height, background, gradient, shadow, colors, inner_radius) |
3340 | + plot.render() |
3341 | + plot.commit() |
3342 | + |
3343 | +def gantt_chart(name, pieces, width, height, x_labels, y_labels, colors): |
3344 | + |
3345 | + ''' |
3346 | + - Function to generate Gantt Charts. |
3347 | + |
3348 | + gantt_chart(name, pieces, width, height, x_labels, y_labels, colors): |
3349 | + |
3350 | + - Parameters |
3351 | + |
3352 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; |
3353 | + pieces - A list defining the spaces to be drawn. The user must pass, for each line, the index of its start and the index of its end. If a line must have two or more spaces, they must be passed inside a list; |
3354 | + width, height - Dimensions of the output image; |
3355 | + x_labels - A list of names for each of the vertical lines; |
3356 | + y_labels - A list of names for each of the horizontal spaces; |
3357 | + colors - List containing the colors expected for each of the horizontal spaces |
3358 | + |
3359 | + - Example of use |
3360 | + |
3361 | + pieces = [ (0.5,5.5) , [(0,4),(6,8)] , (5.5,7) , (7,8)] |
3362 | + x_labels = [ 'teste01', 'teste02', 'teste03', 'teste04'] |
3363 | + y_labels = [ '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009', '0010' ] |
3364 | + colors = [ (1.0, 0.0, 0.0), (1.0, 0.7, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0) ] |
3365 | + CairoPlot.gantt_chart('gantt_teste', pieces, 600, 300, x_labels, y_labels, colors) |
3366 | + ''' |
3367 | + |
3368 | + plot = GanttChart(name, pieces, width, height, x_labels, y_labels, colors) |
3369 | + plot.render() |
3370 | + plot.commit() |
3371 | + |
3372 | +def vertical_bar_plot(name, |
3373 | + data, |
3374 | + width, |
3375 | + height, |
3376 | + background = "white light_gray", |
3377 | + border = 0, |
3378 | + display_values = False, |
3379 | + grid = False, |
3380 | + rounded_corners = False, |
3381 | + stack = False, |
3382 | + three_dimension = False, |
3383 | + series_labels = None, |
3384 | + x_labels = None, |
3385 | + y_labels = None, |
3386 | + x_bounds = None, |
3387 | + y_bounds = None, |
3388 | + colors = None): |
3389 | + #TODO: Fix docstring for vertical_bar_plot |
3390 | + ''' |
3391 | + - Function to generate vertical Bar Plot Charts. |
3392 | + |
3393 | + bar_plot(name, data, width, height, background, border, grid, rounded_corners, three_dimension, |
3394 | + x_labels, y_labels, x_bounds, y_bounds, colors): |
3395 | + |
3396 | + - Parameters |
3397 | + |
3398 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtime; |
3399 | + data - The list, list of lists or dictionary holding the data to be plotted; |
3400 | + width, height - Dimensions of the output image; |
3401 | + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. |
3402 | + If left None, a gray to white gradient will be generated; |
3403 | + border - Distance in pixels of a square border into which the graphics will be drawn; |
3404 | + grid - Whether or not the gris is to be drawn; |
3405 | + rounded_corners - Whether or not the bars should have rounded corners; |
3406 | + three_dimension - Whether or not the bars should be drawn in pseudo 3D; |
3407 | + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; |
3408 | + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; |
3409 | + colors - List containing the colors expected for each of the bars. |
3410 | + |
3411 | + - Example of use |
3412 | + |
3413 | + data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
3414 | + CairoPlot.vertical_bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False) |
3415 | + ''' |
3416 | + |
3417 | + plot = VerticalBarPlot(name, data, width, height, background, border, |
3418 | + display_values, grid, rounded_corners, stack, three_dimension, |
3419 | + series_labels, x_labels, y_labels, x_bounds, y_bounds, colors) |
3420 | + plot.render() |
3421 | + plot.commit() |
3422 | + |
3423 | +def horizontal_bar_plot(name, |
3424 | + data, |
3425 | + width, |
3426 | + height, |
3427 | + background = "white light_gray", |
3428 | + border = 0, |
3429 | + display_values = False, |
3430 | + grid = False, |
3431 | + rounded_corners = False, |
3432 | + stack = False, |
3433 | + three_dimension = False, |
3434 | + series_labels = None, |
3435 | + x_labels = None, |
3436 | + y_labels = None, |
3437 | + x_bounds = None, |
3438 | + y_bounds = None, |
3439 | + colors = None): |
3440 | + |
3441 | + #TODO: Fix docstring for horizontal_bar_plot |
3442 | + ''' |
3443 | + - Function to generate Horizontal Bar Plot Charts. |
3444 | + |
3445 | + bar_plot(name, data, width, height, background, border, grid, rounded_corners, three_dimension, |
3446 | + x_labels, y_labels, x_bounds, y_bounds, colors): |
3447 | + |
3448 | + - Parameters |
3449 | + |
3450 | + name - Name of the desired output file, no need to input the .svg as it will be added at runtime; |
3451 | + data - The list, list of lists or dictionary holding the data to be plotted; |
3452 | + width, height - Dimensions of the output image; |
3453 | + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. |
3454 | + If left None, a gray to white gradient will be generated; |
3455 | + border - Distance in pixels of a square border into which the graphics will be drawn; |
3456 | + grid - Whether or not the gris is to be drawn; |
3457 | + rounded_corners - Whether or not the bars should have rounded corners; |
3458 | + three_dimension - Whether or not the bars should be drawn in pseudo 3D; |
3459 | + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; |
3460 | + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; |
3461 | + colors - List containing the colors expected for each of the bars. |
3462 | + |
3463 | + - Example of use |
3464 | + |
3465 | + data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
3466 | + CairoPlot.bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False) |
3467 | + ''' |
3468 | + |
3469 | + plot = HorizontalBarPlot(name, data, width, height, background, border, |
3470 | + display_values, grid, rounded_corners, stack, three_dimension, |
3471 | + series_labels, x_labels, y_labels, x_bounds, y_bounds, colors) |
3472 | + plot.render() |
3473 | + plot.commit() |
3474 | + |
3475 | +def stream_chart(name, |
3476 | + data, |
3477 | + width, |
3478 | + height, |
3479 | + background = "white light_gray", |
3480 | + border = 0, |
3481 | + grid = False, |
3482 | + series_legend = None, |
3483 | + x_labels = None, |
3484 | + x_bounds = None, |
3485 | + y_bounds = None, |
3486 | + colors = None): |
3487 | + |
3488 | + #TODO: Fix docstring for horizontal_bar_plot |
3489 | + plot = StreamChart(name, data, width, height, background, border, |
3490 | + grid, series_legend, x_labels, x_bounds, y_bounds, colors) |
3491 | + plot.render() |
3492 | + plot.commit() |
3493 | + |
3494 | + |
3495 | +if __name__ == "__main__": |
3496 | + import tests |
3497 | + import seriestests |
3498 | |
3499 | === removed file 'trunk/cairoplot.py' |
3500 | --- trunk/cairoplot.py 2009-03-11 01:04:08 +0000 |
3501 | +++ trunk/cairoplot.py 1970-01-01 00:00:00 +0000 |
3502 | @@ -1,2042 +0,0 @@ |
3503 | -#!/usr/bin/env python |
3504 | -# -*- coding: utf-8 -*- |
3505 | - |
3506 | -# CairoPlot.py |
3507 | -# |
3508 | -# Copyright (c) 2008 Rodrigo Moreira Araújo |
3509 | -# |
3510 | -# Author: Rodrigo Moreiro Araujo <alf.rodrigo@gmail.com> |
3511 | -# |
3512 | -# This program is free software; you can redistribute it and/or |
3513 | -# modify it under the terms of the GNU Lesser General Public License |
3514 | -# as published by the Free Software Foundation; either version 2 of |
3515 | -# the License, or (at your option) any later version. |
3516 | -# |
3517 | -# This program is distributed in the hope that it will be useful, |
3518 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
3519 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3520 | -# GNU General Public License for more details. |
3521 | -# |
3522 | -# You should have received a copy of the GNU Lesser General Public |
3523 | -# License along with this program; if not, write to the Free Software |
3524 | -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
3525 | -# USA |
3526 | - |
3527 | -#Contributor: João S. O. Bueno |
3528 | - |
3529 | -#TODO: review BarPlot Code |
3530 | -#TODO: x_label colision problem on Horizontal Bar Plot |
3531 | -#TODO: y_label's eat too much space on HBP |
3532 | - |
3533 | - |
3534 | -__version__ = 1.1 |
3535 | - |
3536 | -import cairo |
3537 | -import math |
3538 | -import random |
3539 | - |
3540 | -HORZ = 0 |
3541 | -VERT = 1 |
3542 | -NORM = 2 |
3543 | - |
3544 | -COLORS = {"red" : (1.0,0.0,0.0,1.0), "lime" : (0.0,1.0,0.0,1.0), "blue" : (0.0,0.0,1.0,1.0), |
3545 | - "maroon" : (0.5,0.0,0.0,1.0), "green" : (0.0,0.5,0.0,1.0), "navy" : (0.0,0.0,0.5,1.0), |
3546 | - "yellow" : (1.0,1.0,0.0,1.0), "magenta" : (1.0,0.0,1.0,1.0), "cyan" : (0.0,1.0,1.0,1.0), |
3547 | - "orange" : (1.0,0.5,0.0,1.0), "white" : (1.0,1.0,1.0,1.0), "black" : (0.0,0.0,0.0,1.0), |
3548 | - "gray" : (0.5,0.5,0.5,1.0), "light_gray" : (0.9,0.9,0.9,1.0), |
3549 | - "transparent" : (0.0,0.0,0.0,0.0)} |
3550 | - |
3551 | -THEMES = {"black_red" : [(0.0,0.0,0.0,1.0), (1.0,0.0,0.0,1.0)], |
3552 | - "red_green_blue" : [(1.0,0.0,0.0,1.0), (0.0,1.0,0.0,1.0), (0.0,0.0,1.0,1.0)], |
3553 | - "red_orange_yellow" : [(1.0,0.2,0.0,1.0), (1.0,0.7,0.0,1.0), (1.0,1.0,0.0,1.0)], |
3554 | - "yellow_orange_red" : [(1.0,1.0,0.0,1.0), (1.0,0.7,0.0,1.0), (1.0,0.2,0.0,1.0)], |
3555 | - "rainbow" : [(1.0,0.0,0.0,1.0), (1.0,0.5,0.0,1.0), (1.0,1.0,0.0,1.0), (0.0,1.0,0.0,1.0), (0.0,0.0,1.0,1.0), (0.3, 0.0, 0.5,1.0), (0.5, 0.0, 1.0, 1.0)]} |
3556 | - |
3557 | -def colors_from_theme( theme, series_length ): |
3558 | - colors = [] |
3559 | - if theme not in THEMES.keys() : |
3560 | - raise Exception, "Theme not defined" |
3561 | - color_steps = THEMES[theme] |
3562 | - n_colors = len(color_steps) |
3563 | - if series_length <= n_colors: |
3564 | - colors = [color for color in color_steps[0:n_colors]] |
3565 | - else: |
3566 | - iterations = [(series_length - n_colors)/(n_colors - 1) for i in color_steps[:-1]] |
3567 | - over_iterations = (series_length - n_colors) % (n_colors - 1) |
3568 | - for i in range(n_colors - 1): |
3569 | - if over_iterations <= 0: |
3570 | - break |
3571 | - iterations[i] += 1 |
3572 | - over_iterations -= 1 |
3573 | - for index,color in enumerate(color_steps[:-1]): |
3574 | - colors.append(color) |
3575 | - if iterations[index] == 0: |
3576 | - continue |
3577 | - next_color = color_steps[index+1] |
3578 | - color_step = ((next_color[0] - color[0])/(iterations[index] + 1), |
3579 | - (next_color[1] - color[1])/(iterations[index] + 1), |
3580 | - (next_color[2] - color[2])/(iterations[index] + 1), |
3581 | - (next_color[3] - color[3])/(iterations[index] + 1)) |
3582 | - for i in range( iterations[index] ): |
3583 | - colors.append((color[0] + color_step[0]*(i+1), |
3584 | - color[1] + color_step[1]*(i+1), |
3585 | - color[2] + color_step[2]*(i+1), |
3586 | - color[3] + color_step[3]*(i+1))) |
3587 | - colors.append(color_steps[-1]) |
3588 | - return colors |
3589 | - |
3590 | - |
3591 | -def other_direction(direction): |
3592 | - "explicit is better than implicit" |
3593 | - if direction == HORZ: |
3594 | - return VERT |
3595 | - else: |
3596 | - return HORZ |
3597 | - |
3598 | -#Class definition |
3599 | - |
3600 | -class Plot(object): |
3601 | - def __init__(self, |
3602 | - surface=None, |
3603 | - data=None, |
3604 | - width=640, |
3605 | - height=480, |
3606 | - background=None, |
3607 | - border = 0, |
3608 | - x_labels = None, |
3609 | - y_labels = None, |
3610 | - series_colors = None): |
3611 | - random.seed(2) |
3612 | - self.create_surface(surface, width, height) |
3613 | - self.dimensions = {} |
3614 | - self.dimensions[HORZ] = width |
3615 | - self.dimensions[VERT] = height |
3616 | - self.context = cairo.Context(self.surface) |
3617 | - self.labels={} |
3618 | - self.labels[HORZ] = x_labels |
3619 | - self.labels[VERT] = y_labels |
3620 | - self.load_series(data, x_labels, y_labels, series_colors) |
3621 | - self.font_size = 10 |
3622 | - self.set_background (background) |
3623 | - self.border = border |
3624 | - self.borders = {} |
3625 | - self.line_color = (0.5, 0.5, 0.5) |
3626 | - self.line_width = 0.5 |
3627 | - self.label_color = (0.0, 0.0, 0.0) |
3628 | - self.grid_color = (0.8, 0.8, 0.8) |
3629 | - |
3630 | - def create_surface(self, surface, width=None, height=None): |
3631 | - self.filename = None |
3632 | - if isinstance(surface, cairo.Surface): |
3633 | - self.surface = surface |
3634 | - return |
3635 | - if not type(surface) in (str, unicode): |
3636 | - raise TypeError("Surface should be either a Cairo surface or a filename, not %s" % surface) |
3637 | - sufix = surface.rsplit(".")[-1].lower() |
3638 | - self.filename = surface |
3639 | - if sufix == "png": |
3640 | - self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
3641 | - elif sufix == "ps": |
3642 | - self.surface = cairo.PSSurface(surface, width, height) |
3643 | - elif sufix == "pdf": |
3644 | - self.surface = cairo.PSSurface(surface, width, height) |
3645 | - else: |
3646 | - if sufix != "svg": |
3647 | - self.filename += ".svg" |
3648 | - self.surface = cairo.SVGSurface(self.filename, width, height) |
3649 | - |
3650 | - def commit(self): |
3651 | - try: |
3652 | - self.context.show_page() |
3653 | - if self.filename and self.filename.endswith(".png"): |
3654 | - self.surface.write_to_png(self.filename) |
3655 | - else: |
3656 | - self.surface.finish() |
3657 | - except cairo.Error: |
3658 | - pass |
3659 | - |
3660 | - def load_series (self, data, x_labels=None, y_labels=None, series_colors=None): |
3661 | - #FIXME: implement Series class for holding series data, |
3662 | - # labels and presentation properties |
3663 | - |
3664 | - #data can be a list, a list of lists or a dictionary with |
3665 | - #each item as a labeled data series. |
3666 | - #we should (for the time being) create a list of lists |
3667 | - #and set labels for teh series rom teh values provided. |
3668 | - |
3669 | - self.series_labels = [] |
3670 | - self.data = [] |
3671 | - #dictionary |
3672 | - if hasattr(data, "keys"): |
3673 | - self.series_labels = data.keys() |
3674 | - for key in self.series_labels: |
3675 | - self.data.append(data[key]) |
3676 | - #lists of lists: |
3677 | - elif max([hasattr(item,'__delitem__') for item in data]) : |
3678 | - self.data = data |
3679 | - self.series_labels = range(len(data)) |
3680 | - #list |
3681 | - else: |
3682 | - self.data = [data] |
3683 | - self.series_labels = None |
3684 | - #TODO: allow user passed series_widths |
3685 | - self.series_widths = [1.0 for series in self.data] |
3686 | - self.process_colors( series_colors ) |
3687 | - |
3688 | - def process_colors( self, series_colors, length = None ): |
3689 | - #series_colors might be None, a theme, a string of colors names or a string of color tuples |
3690 | - if length is None : |
3691 | - length = len( self.data ) |
3692 | - #no colors passed |
3693 | - if not series_colors: |
3694 | - #Randomize colors |
3695 | - self.series_colors = [ [random.random() for i in range(3)] + [1.0] for series in range( length ) ] |
3696 | - else: |
3697 | - #Theme pattern |
3698 | - if not hasattr( series_colors, "__iter__" ): |
3699 | - theme = series_colors |
3700 | - self.series_colors = colors_from_theme( theme.lower(), length ) |
3701 | - #List |
3702 | - else: |
3703 | - self.series_colors = series_colors |
3704 | - for index, color in enumerate( self.series_colors ): |
3705 | - #list of color names |
3706 | - if not hasattr(color, "__iter__"): |
3707 | - self.series_colors[index] = COLORS[color.lower()] |
3708 | - #list of rgb colors instead of rgba |
3709 | - elif len( color ) == 3 : |
3710 | - self.series_colors[index] += tuple( [1.0] ) |
3711 | - |
3712 | - def get_width(self): |
3713 | - return self.surface.get_width() |
3714 | - |
3715 | - def get_height(self): |
3716 | - return self.surface.get_height() |
3717 | - |
3718 | - def set_background(self, background): |
3719 | - if background is None: |
3720 | - self.background = (0.0,0.0,0.0,0.0) |
3721 | - elif type(background) in (cairo.LinearGradient, tuple): |
3722 | - self.background = background |
3723 | - elif not hasattr(background,"__iter__"): |
3724 | - colors = background.split(" ") |
3725 | - if len(colors) == 1 and colors[0] in COLORS: |
3726 | - self.background = COLORS[background] |
3727 | - elif len(colors) > 1: |
3728 | - self.background = cairo.LinearGradient(self.dimensions[HORZ] / 2, 0, self.dimensions[HORZ] / 2, self.dimensions[VERT]) |
3729 | - for index,color in enumerate(colors): |
3730 | - self.background.add_color_stop_rgba(float(index)/(len(colors)-1),*COLORS[color]) |
3731 | - else: |
3732 | - raise TypeError ("Background should be either cairo.LinearGradient or a 3-tuple, not %s" % type(background)) |
3733 | - |
3734 | - def render_background(self): |
3735 | - if isinstance(self.background, cairo.LinearGradient): |
3736 | - self.context.set_source(self.background) |
3737 | - else: |
3738 | - self.context.set_source_rgba(*self.background) |
3739 | - self.context.rectangle(0,0, self.dimensions[HORZ], self.dimensions[VERT]) |
3740 | - self.context.fill() |
3741 | - |
3742 | - def render_bounding_box(self): |
3743 | - self.context.set_source_rgba(*self.line_color) |
3744 | - self.context.set_line_width(self.line_width) |
3745 | - self.context.rectangle(self.border, self.border, |
3746 | - self.dimensions[HORZ] - 2 * self.border, |
3747 | - self.dimensions[VERT] - 2 * self.border) |
3748 | - self.context.stroke() |
3749 | - |
3750 | - def render(self): |
3751 | - pass |
3752 | - |
3753 | -class ScatterPlot( Plot ): |
3754 | - def __init__(self, |
3755 | - surface=None, |
3756 | - data=None, |
3757 | - errorx=None, |
3758 | - errory=None, |
3759 | - width=640, |
3760 | - height=480, |
3761 | - background=None, |
3762 | - border=0, |
3763 | - axis = False, |
3764 | - dash = False, |
3765 | - discrete = False, |
3766 | - dots = 0, |
3767 | - grid = False, |
3768 | - series_legend = False, |
3769 | - x_labels = None, |
3770 | - y_labels = None, |
3771 | - x_bounds = None, |
3772 | - y_bounds = None, |
3773 | - z_bounds = None, |
3774 | - x_title = None, |
3775 | - y_title = None, |
3776 | - series_colors = None, |
3777 | - circle_colors = None ): |
3778 | - |
3779 | - self.bounds = {} |
3780 | - self.bounds[HORZ] = x_bounds |
3781 | - self.bounds[VERT] = y_bounds |
3782 | - self.bounds[NORM] = z_bounds |
3783 | - self.titles = {} |
3784 | - self.titles[HORZ] = x_title |
3785 | - self.titles[VERT] = y_title |
3786 | - self.max_value = {} |
3787 | - self.axis = axis |
3788 | - self.discrete = discrete |
3789 | - self.dots = dots |
3790 | - self.grid = grid |
3791 | - self.series_legend = series_legend |
3792 | - self.variable_radius = False |
3793 | - self.x_label_angle = math.pi / 2.5 |
3794 | - self.circle_colors = circle_colors |
3795 | - |
3796 | - Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors) |
3797 | - |
3798 | - self.dash = None |
3799 | - if dash: |
3800 | - if hasattr(dash, "keys"): |
3801 | - self.dash = [dash[key] for key in self.series_labels] |
3802 | - elif max([hasattr(item,'__delitem__') for item in data]) : |
3803 | - self.dash = dash |
3804 | - else: |
3805 | - self.dash = [dash] |
3806 | - |
3807 | - self.load_errors(errorx, errory) |
3808 | - |
3809 | - def convert_list_to_tuple(self, data): |
3810 | - #Data must be converted from lists of coordinates to a single |
3811 | - # list of tuples |
3812 | - out_data = zip(*data) |
3813 | - if len(data) == 3: |
3814 | - self.variable_radius = True |
3815 | - return out_data |
3816 | - |
3817 | - def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): |
3818 | - #Dictionary with lists |
3819 | - if hasattr(data, "keys") : |
3820 | - if hasattr( data.values()[0][0], "__delitem__" ) : |
3821 | - for key in data.keys() : |
3822 | - data[key] = self.convert_list_to_tuple(data[key]) |
3823 | - elif len(data.values()[0][0]) == 3: |
3824 | - self.variable_radius = True |
3825 | - #List |
3826 | - elif hasattr(data[0], "__delitem__") : |
3827 | - #List of lists |
3828 | - if hasattr(data[0][0], "__delitem__") : |
3829 | - for index,value in enumerate(data) : |
3830 | - data[index] = self.convert_list_to_tuple(value) |
3831 | - #List |
3832 | - elif type(data[0][0]) != type((0,0)): |
3833 | - data = self.convert_list_to_tuple(data) |
3834 | - #Three dimensional data |
3835 | - elif len(data[0][0]) == 3: |
3836 | - self.variable_radius = True |
3837 | - #List with three dimensional tuples |
3838 | - elif len(data[0]) == 3: |
3839 | - self.variable_radius = True |
3840 | - Plot.load_series(self, data, x_labels, y_labels, series_colors) |
3841 | - self.calc_boundaries() |
3842 | - self.calc_labels() |
3843 | - |
3844 | - def load_errors(self, errorx, errory): |
3845 | - self.errors = None |
3846 | - if errorx == None and errory == None: |
3847 | - return |
3848 | - self.errors = {} |
3849 | - self.errors[HORZ] = None |
3850 | - self.errors[VERT] = None |
3851 | - #asimetric errors |
3852 | - if errorx and hasattr(errorx[0], "__delitem__"): |
3853 | - self.errors[HORZ] = errorx |
3854 | - #simetric errors |
3855 | - elif errorx: |
3856 | - self.errors[HORZ] = [errorx] |
3857 | - #asimetric errors |
3858 | - if errory and hasattr(errory[0], "__delitem__"): |
3859 | - self.errors[VERT] = errory |
3860 | - #simetric errors |
3861 | - elif errory: |
3862 | - self.errors[VERT] = [errory] |
3863 | - |
3864 | - def calc_labels(self): |
3865 | - if not self.labels[HORZ]: |
3866 | - amplitude = self.bounds[HORZ][1] - self.bounds[HORZ][0] |
3867 | - if amplitude % 10: #if horizontal labels need floating points |
3868 | - self.labels[HORZ] = ["%.2lf" % (float(self.bounds[HORZ][0] + (amplitude * i / 10.0))) for i in range(11) ] |
3869 | - else: |
3870 | - self.labels[HORZ] = ["%d" % (int(self.bounds[HORZ][0] + (amplitude * i / 10.0))) for i in range(11) ] |
3871 | - if not self.labels[VERT]: |
3872 | - amplitude = self.bounds[VERT][1] - self.bounds[VERT][0] |
3873 | - if amplitude % 10: #if vertical labels need floating points |
3874 | - self.labels[VERT] = ["%.2lf" % (float(self.bounds[VERT][0] + (amplitude * i / 10.0))) for i in range(11) ] |
3875 | - else: |
3876 | - self.labels[VERT] = ["%d" % (int(self.bounds[VERT][0] + (amplitude * i / 10.0))) for i in range(11) ] |
3877 | - |
3878 | - def calc_extents(self, direction): |
3879 | - self.context.set_font_size(self.font_size * 0.8) |
3880 | - self.max_value[direction] = max(self.context.text_extents(item)[2] for item in self.labels[direction]) |
3881 | - self.borders[other_direction(direction)] = self.max_value[direction] + self.border + 20 |
3882 | - |
3883 | - def calc_boundaries(self): |
3884 | - #HORZ = 0, VERT = 1, NORM = 2 |
3885 | - min_data_value = [0,0,0] |
3886 | - max_data_value = [0,0,0] |
3887 | - for serie in self.data : |
3888 | - for tuple in serie : |
3889 | - for index, item in enumerate(tuple) : |
3890 | - if item > max_data_value[index]: |
3891 | - max_data_value[index] = item |
3892 | - elif item < min_data_value[index]: |
3893 | - min_data_value[index] = item |
3894 | - |
3895 | - if not self.bounds[HORZ]: |
3896 | - self.bounds[HORZ] = (min_data_value[HORZ], max_data_value[HORZ]) |
3897 | - if not self.bounds[VERT]: |
3898 | - self.bounds[VERT] = (min_data_value[VERT], max_data_value[VERT]) |
3899 | - if not self.bounds[NORM]: |
3900 | - self.bounds[NORM] = (min_data_value[NORM], max_data_value[NORM]) |
3901 | - |
3902 | - def calc_all_extents(self): |
3903 | - self.calc_extents(HORZ) |
3904 | - self.calc_extents(VERT) |
3905 | - |
3906 | - self.plot_height = self.dimensions[VERT] - 2 * self.borders[VERT] |
3907 | - self.plot_width = self.dimensions[HORZ] - 2* self.borders[HORZ] |
3908 | - |
3909 | - self.plot_top = self.dimensions[VERT] - self.borders[VERT] |
3910 | - |
3911 | - def calc_steps(self): |
3912 | - #Calculates all the x, y, z and color steps |
3913 | - series_amplitude = [self.bounds[index][1] - self.bounds[index][0] for index in range(3)] |
3914 | - |
3915 | - if series_amplitude[HORZ]: |
3916 | - self.horizontal_step = float (self.plot_width) / series_amplitude[HORZ] |
3917 | - else: |
3918 | - self.horizontal_step = 0.00 |
3919 | - |
3920 | - if series_amplitude[VERT]: |
3921 | - self.vertical_step = float (self.plot_height) / series_amplitude[VERT] |
3922 | - else: |
3923 | - self.vertical_step = 0.00 |
3924 | - |
3925 | - if series_amplitude[NORM]: |
3926 | - if self.variable_radius: |
3927 | - self.z_step = float (self.bounds[NORM][1]) / series_amplitude[NORM] |
3928 | - if self.circle_colors: |
3929 | - self.circle_color_step = tuple([float(self.circle_colors[1][i]-self.circle_colors[0][i])/series_amplitude[NORM] for i in range(4)]) |
3930 | - else: |
3931 | - self.z_step = 0.00 |
3932 | - self.circle_color_step = ( 0.0, 0.0, 0.0, 0.0 ) |
3933 | - |
3934 | - def get_circle_color(self, value): |
3935 | - return tuple( [self.circle_colors[0][i] + value*self.circle_color_step[i] for i in range(4)] ) |
3936 | - |
3937 | - def render(self): |
3938 | - self.calc_all_extents() |
3939 | - self.calc_steps() |
3940 | - self.render_background() |
3941 | - self.render_bounding_box() |
3942 | - if self.axis: |
3943 | - self.render_axis() |
3944 | - if self.grid: |
3945 | - self.render_grid() |
3946 | - self.render_labels() |
3947 | - self.render_plot() |
3948 | - if self.errors: |
3949 | - self.render_errors() |
3950 | - if self.series_legend and self.series_labels: |
3951 | - self.render_legend() |
3952 | - |
3953 | - def render_axis(self): |
3954 | - #Draws both the axis lines and their titles |
3955 | - cr = self.context |
3956 | - cr.set_source_rgba(*self.line_color) |
3957 | - cr.move_to(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) |
3958 | - cr.line_to(self.borders[HORZ], self.borders[VERT]) |
3959 | - cr.stroke() |
3960 | - |
3961 | - cr.move_to(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) |
3962 | - cr.line_to(self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) |
3963 | - cr.stroke() |
3964 | - |
3965 | - cr.set_source_rgba(*self.label_color) |
3966 | - self.context.set_font_size( 1.2 * self.font_size ) |
3967 | - if self.titles[HORZ]: |
3968 | - title_width,title_height = cr.text_extents(self.titles[HORZ])[2:4] |
3969 | - cr.move_to( self.dimensions[HORZ]/2 - title_width/2, self.borders[VERT] - title_height/2 ) |
3970 | - cr.show_text( self.titles[HORZ] ) |
3971 | - |
3972 | - if self.titles[VERT]: |
3973 | - title_width,title_height = cr.text_extents(self.titles[VERT])[2:4] |
3974 | - cr.move_to( self.dimensions[HORZ] - self.borders[HORZ] + title_height/2, self.dimensions[VERT]/2 - title_width/2) |
3975 | - cr.rotate( math.pi/2 ) |
3976 | - cr.show_text( self.titles[VERT] ) |
3977 | - cr.rotate( -math.pi/2 ) |
3978 | - |
3979 | - def render_grid(self): |
3980 | - cr = self.context |
3981 | - horizontal_step = float( self.plot_height ) / ( len( self.labels[VERT] ) - 1 ) |
3982 | - vertical_step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 ) |
3983 | - |
3984 | - x = self.borders[HORZ] + vertical_step |
3985 | - y = self.plot_top - horizontal_step |
3986 | - |
3987 | - for label in self.labels[HORZ][:-1]: |
3988 | - cr.set_source_rgba(*self.grid_color) |
3989 | - cr.move_to(x, self.dimensions[VERT] - self.borders[VERT]) |
3990 | - cr.line_to(x, self.borders[VERT]) |
3991 | - cr.stroke() |
3992 | - x += vertical_step |
3993 | - for label in self.labels[VERT][:-1]: |
3994 | - cr.set_source_rgba(*self.grid_color) |
3995 | - cr.move_to(self.borders[HORZ], y) |
3996 | - cr.line_to(self.dimensions[HORZ] - self.borders[HORZ], y) |
3997 | - cr.stroke() |
3998 | - y -= horizontal_step |
3999 | - |
4000 | - def render_labels(self): |
4001 | - self.context.set_font_size(self.font_size * 0.8) |
4002 | - self.render_horz_labels() |
4003 | - self.render_vert_labels() |
4004 | - |
4005 | - def render_horz_labels(self): |
4006 | - cr = self.context |
4007 | - step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 ) |
4008 | - x = self.borders[HORZ] |
4009 | - for item in self.labels[HORZ]: |
4010 | - cr.set_source_rgba(*self.label_color) |
4011 | - width = cr.text_extents(item)[2] |
4012 | - cr.move_to(x, self.dimensions[VERT] - self.borders[VERT] + 5) |
4013 | - cr.rotate(self.x_label_angle) |
4014 | - cr.show_text(item) |
4015 | - cr.rotate(-self.x_label_angle) |
4016 | - x += step |
4017 | - |
4018 | - def render_vert_labels(self): |
4019 | - cr = self.context |
4020 | - step = ( self.plot_height ) / ( len( self.labels[VERT] ) - 1 ) |
4021 | - y = self.plot_top |
4022 | - for item in self.labels[VERT]: |
4023 | - cr.set_source_rgba(*self.label_color) |
4024 | - width = cr.text_extents(item)[2] |
4025 | - cr.move_to(self.borders[HORZ] - width - 5,y) |
4026 | - cr.show_text(item) |
4027 | - y -= step |
4028 | - |
4029 | - def render_legend(self): |
4030 | - cr = self.context |
4031 | - cr.set_font_size(self.font_size) |
4032 | - cr.set_line_width(self.line_width) |
4033 | - |
4034 | - widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2]) |
4035 | - tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3]) |
4036 | - max_width = self.context.text_extents(widest_word)[2] |
4037 | - max_height = self.context.text_extents(tallest_word)[3] * 1.1 |
4038 | - |
4039 | - color_box_height = max_height / 2 |
4040 | - color_box_width = color_box_height * 2 |
4041 | - |
4042 | - #Draw a bounding box |
4043 | - bounding_box_width = max_width + color_box_width + 15 |
4044 | - bounding_box_height = (len(self.series_labels)+0.5) * max_height |
4045 | - cr.set_source_rgba(1,1,1) |
4046 | - cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - bounding_box_width, self.borders[VERT], |
4047 | - bounding_box_width, bounding_box_height) |
4048 | - cr.fill() |
4049 | - |
4050 | - cr.set_source_rgba(*self.line_color) |
4051 | - cr.set_line_width(self.line_width) |
4052 | - cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - bounding_box_width, self.borders[VERT], |
4053 | - bounding_box_width, bounding_box_height) |
4054 | - cr.stroke() |
4055 | - |
4056 | - for idx,key in enumerate(self.series_labels): |
4057 | - #Draw color box |
4058 | - cr.set_source_rgba(*self.series_colors[idx]) |
4059 | - cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - max_width - color_box_width - 10, |
4060 | - self.borders[VERT] + color_box_height + (idx*max_height) , |
4061 | - color_box_width, color_box_height) |
4062 | - cr.fill() |
4063 | - |
4064 | - cr.set_source_rgba(0, 0, 0) |
4065 | - cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - max_width - color_box_width - 10, |
4066 | - self.borders[VERT] + color_box_height + (idx*max_height), |
4067 | - color_box_width, color_box_height) |
4068 | - cr.stroke() |
4069 | - |
4070 | - #Draw series labels |
4071 | - cr.set_source_rgba(0, 0, 0) |
4072 | - cr.move_to(self.dimensions[HORZ] - self.borders[HORZ] - max_width - 5, self.borders[VERT] + ((idx+1)*max_height)) |
4073 | - cr.show_text(key) |
4074 | - |
4075 | - def render_errors(self): |
4076 | - cr = self.context |
4077 | - cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) |
4078 | - cr.clip() |
4079 | - radius = self.dots |
4080 | - x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
4081 | - y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
4082 | - for index, serie in enumerate(self.data): |
4083 | - cr.set_source_rgba(*self.series_colors[index]) |
4084 | - for number, tuple in enumerate(serie): |
4085 | - x = x0 + self.horizontal_step * tuple[0] |
4086 | - y = self.dimensions[VERT] - y0 - self.vertical_step * tuple[1] |
4087 | - if self.errors[HORZ]: |
4088 | - cr.move_to(x, y) |
4089 | - x1 = x - self.horizontal_step * self.errors[HORZ][0][number] |
4090 | - cr.line_to(x1, y) |
4091 | - cr.line_to(x1, y - radius) |
4092 | - cr.line_to(x1, y + radius) |
4093 | - cr.stroke() |
4094 | - if self.errors[HORZ] and len(self.errors[HORZ]) == 2: |
4095 | - cr.move_to(x, y) |
4096 | - x1 = x + self.horizontal_step * self.errors[HORZ][1][number] |
4097 | - cr.line_to(x1, y) |
4098 | - cr.line_to(x1, y - radius) |
4099 | - cr.line_to(x1, y + radius) |
4100 | - cr.stroke() |
4101 | - if self.errors[VERT]: |
4102 | - cr.move_to(x, y) |
4103 | - y1 = y + self.vertical_step * self.errors[VERT][0][number] |
4104 | - cr.line_to(x, y1) |
4105 | - cr.line_to(x - radius, y1) |
4106 | - cr.line_to(x + radius, y1) |
4107 | - cr.stroke() |
4108 | - if self.errors[VERT] and len(self.errors[VERT]) == 2: |
4109 | - cr.move_to(x, y) |
4110 | - y1 = y - self.vertical_step * self.errors[VERT][1][number] |
4111 | - cr.line_to(x, y1) |
4112 | - cr.line_to(x - radius, y1) |
4113 | - cr.line_to(x + radius, y1) |
4114 | - cr.stroke() |
4115 | - |
4116 | - |
4117 | - def render_plot(self): |
4118 | - cr = self.context |
4119 | - if self.discrete: |
4120 | - cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) |
4121 | - cr.clip() |
4122 | - x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
4123 | - y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
4124 | - radius = self.dots |
4125 | - for number, serie in enumerate (self.data): |
4126 | - cr.set_source_rgba(*self.series_colors[number]) |
4127 | - for tuple in serie : |
4128 | - if self.variable_radius: |
4129 | - radius = tuple[2]*self.z_step |
4130 | - if self.circle_colors: |
4131 | - cr.set_source_rgba( *self.get_circle_color( tuple[2]) ) |
4132 | - x = x0 + self.horizontal_step*tuple[0] |
4133 | - y = y0 + self.vertical_step*tuple[1] |
4134 | - cr.arc(x, self.dimensions[VERT] - y, radius, 0, 2*math.pi) |
4135 | - cr.fill() |
4136 | - else: |
4137 | - cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) |
4138 | - cr.clip() |
4139 | - x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
4140 | - y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
4141 | - radius = self.dots |
4142 | - for number, serie in enumerate (self.data): |
4143 | - last_tuple = None |
4144 | - cr.set_source_rgba(*self.series_colors[number]) |
4145 | - for tuple in serie : |
4146 | - x = x0 + self.horizontal_step*tuple[0] |
4147 | - y = y0 + self.vertical_step*tuple[1] |
4148 | - if self.dots: |
4149 | - if self.variable_radius: |
4150 | - radius = tuple[2]*self.z_step |
4151 | - cr.arc(x, self.dimensions[VERT] - y, radius, 0, 2*math.pi) |
4152 | - cr.fill() |
4153 | - if last_tuple : |
4154 | - old_x = x0 + self.horizontal_step*last_tuple[0] |
4155 | - old_y = y0 + self.vertical_step*last_tuple[1] |
4156 | - cr.move_to( old_x, self.dimensions[VERT] - old_y ) |
4157 | - cr.line_to( x, self.dimensions[VERT] - y) |
4158 | - cr.set_line_width(self.series_widths[number]) |
4159 | - |
4160 | - # Display line as dash line |
4161 | - if self.dash and self.dash[number]: |
4162 | - s = self.series_widths[number] |
4163 | - cr.set_dash([s*3, s*3], 0) |
4164 | - |
4165 | - cr.stroke() |
4166 | - cr.set_dash([]) |
4167 | - last_tuple = tuple |
4168 | - |
4169 | -class DotLinePlot(ScatterPlot): |
4170 | - def __init__(self, |
4171 | - surface=None, |
4172 | - data=None, |
4173 | - width=640, |
4174 | - height=480, |
4175 | - background=None, |
4176 | - border=0, |
4177 | - axis = False, |
4178 | - dash = False, |
4179 | - dots = 0, |
4180 | - grid = False, |
4181 | - series_legend = False, |
4182 | - x_labels = None, |
4183 | - y_labels = None, |
4184 | - x_bounds = None, |
4185 | - y_bounds = None, |
4186 | - x_title = None, |
4187 | - y_title = None, |
4188 | - series_colors = None): |
4189 | - |
4190 | - ScatterPlot.__init__(self, surface, data, None, None, width, height, background, border, |
4191 | - axis, dash, False, dots, grid, series_legend, x_labels, y_labels, |
4192 | - x_bounds, y_bounds, None, x_title, y_title, series_colors, None ) |
4193 | - |
4194 | - |
4195 | - def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): |
4196 | - Plot.load_series(self, data, x_labels, y_labels, series_colors) |
4197 | - for serie in self.data : |
4198 | - for index,value in enumerate(serie): |
4199 | - serie[index] = (index, value) |
4200 | - |
4201 | - self.calc_boundaries() |
4202 | - self.calc_labels() |
4203 | - |
4204 | -class FunctionPlot(ScatterPlot): |
4205 | - def __init__(self, |
4206 | - surface=None, |
4207 | - data=None, |
4208 | - width=640, |
4209 | - height=480, |
4210 | - background=None, |
4211 | - border=0, |
4212 | - axis = False, |
4213 | - discrete = False, |
4214 | - dots = 0, |
4215 | - grid = False, |
4216 | - series_legend = False, |
4217 | - x_labels = None, |
4218 | - y_labels = None, |
4219 | - x_bounds = None, |
4220 | - y_bounds = None, |
4221 | - x_title = None, |
4222 | - y_title = None, |
4223 | - series_colors = None, |
4224 | - step = 1): |
4225 | - |
4226 | - self.function = data |
4227 | - self.step = step |
4228 | - self.discrete = discrete |
4229 | - |
4230 | - data, x_bounds = self.load_series_from_function( self.function, x_bounds ) |
4231 | - |
4232 | - ScatterPlot.__init__(self, surface, data, None, None, width, height, background, border, |
4233 | - axis, False, discrete, dots, grid, series_legend, x_labels, y_labels, |
4234 | - x_bounds, y_bounds, None, x_title, y_title, series_colors, None ) |
4235 | - |
4236 | - def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): |
4237 | - Plot.load_series(self, data, x_labels, y_labels, series_colors) |
4238 | - for serie in self.data : |
4239 | - for index,value in enumerate(serie): |
4240 | - serie[index] = (self.bounds[HORZ][0] + self.step*index, value) |
4241 | - |
4242 | - self.calc_boundaries() |
4243 | - self.calc_labels() |
4244 | - |
4245 | - def load_series_from_function( self, function, x_bounds ): |
4246 | - #TODO: Add the possibility for the user to define multiple functions with different discretization parameters |
4247 | - |
4248 | - #This function converts a function, a list of functions or a dictionary |
4249 | - #of functions into its corresponding array of data |
4250 | - data = None |
4251 | - #if no bounds are provided |
4252 | - if x_bounds == None: |
4253 | - x_bounds = (0,10) |
4254 | - |
4255 | - if hasattr(function, "keys"): #dictionary: |
4256 | - data = {} |
4257 | - for key in function.keys(): |
4258 | - data[ key ] = [] |
4259 | - i = x_bounds[0] |
4260 | - while i <= x_bounds[1] : |
4261 | - data[ key ].append( function[ key ](i) ) |
4262 | - i += self.step |
4263 | - elif hasattr(function, "__delitem__"): #list of functions |
4264 | - data = [] |
4265 | - for index,f in enumerate( function ) : |
4266 | - data.append( [] ) |
4267 | - i = x_bounds[0] |
4268 | - while i <= x_bounds[1] : |
4269 | - data[ index ].append( f(i) ) |
4270 | - i += self.step |
4271 | - else: #function |
4272 | - data = [] |
4273 | - i = x_bounds[0] |
4274 | - while i <= x_bounds[1] : |
4275 | - data.append( function(i) ) |
4276 | - i += self.step |
4277 | - |
4278 | - return data, x_bounds |
4279 | - |
4280 | - def calc_labels(self): |
4281 | - if not self.labels[HORZ]: |
4282 | - self.labels[HORZ] = [] |
4283 | - i = self.bounds[HORZ][0] |
4284 | - while i<=self.bounds[HORZ][1]: |
4285 | - self.labels[HORZ].append(str(i)) |
4286 | - i += float(self.bounds[HORZ][1] - self.bounds[HORZ][0])/10 |
4287 | - ScatterPlot.calc_labels(self) |
4288 | - |
4289 | - def render_plot(self): |
4290 | - if not self.discrete: |
4291 | - ScatterPlot.render_plot(self) |
4292 | - else: |
4293 | - last = None |
4294 | - cr = self.context |
4295 | - for number, series in enumerate (self.data): |
4296 | - cr.set_source_rgba(*self.series_colors[number]) |
4297 | - x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step |
4298 | - y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step |
4299 | - for tuple in series: |
4300 | - x = x0 + self.horizontal_step * tuple[0] |
4301 | - y = y0 + self.vertical_step * tuple[1] |
4302 | - cr.move_to(x, self.dimensions[VERT] - y) |
4303 | - cr.line_to(x, self.plot_top) |
4304 | - cr.set_line_width(self.series_widths[number]) |
4305 | - cr.stroke() |
4306 | - if self.dots: |
4307 | - cr.new_path() |
4308 | - cr.arc(x, self.dimensions[VERT] - y, 3, 0, 2.1 * math.pi) |
4309 | - cr.close_path() |
4310 | - cr.fill() |
4311 | - |
4312 | -class BarPlot(Plot): |
4313 | - def __init__(self, |
4314 | - surface = None, |
4315 | - data = None, |
4316 | - width = 640, |
4317 | - height = 480, |
4318 | - background = "white light_gray", |
4319 | - border = 0, |
4320 | - display_values = False, |
4321 | - grid = False, |
4322 | - rounded_corners = False, |
4323 | - stack = False, |
4324 | - three_dimension = False, |
4325 | - x_labels = None, |
4326 | - y_labels = None, |
4327 | - x_bounds = None, |
4328 | - y_bounds = None, |
4329 | - series_colors = None, |
4330 | - main_dir = None): |
4331 | - |
4332 | - self.bounds = {} |
4333 | - self.bounds[HORZ] = x_bounds |
4334 | - self.bounds[VERT] = y_bounds |
4335 | - self.display_values = display_values |
4336 | - self.grid = grid |
4337 | - self.rounded_corners = rounded_corners |
4338 | - self.stack = stack |
4339 | - self.three_dimension = three_dimension |
4340 | - self.x_label_angle = math.pi / 2.5 |
4341 | - self.main_dir = main_dir |
4342 | - self.max_value = {} |
4343 | - self.plot_dimensions = {} |
4344 | - self.steps = {} |
4345 | - self.value_label_color = (0.5,0.5,0.5,1.0) |
4346 | - |
4347 | - Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors) |
4348 | - |
4349 | - def load_series(self, data, x_labels = None, y_labels = None, series_colors = None): |
4350 | - Plot.load_series(self, data, x_labels, y_labels, series_colors) |
4351 | - self.calc_boundaries() |
4352 | - |
4353 | - def process_colors(self, series_colors): |
4354 | - #Data for a BarPlot might be a List or a List of Lists. |
4355 | - #On the first case, colors must be generated for all bars, |
4356 | - #On the second, colors must be generated for each of the inner lists. |
4357 | - if hasattr(self.data[0], '__getitem__'): |
4358 | - length = max(len(series) for series in self.data) |
4359 | - else: |
4360 | - length = len( self.data ) |
4361 | - |
4362 | - Plot.process_colors( self, series_colors, length ) |
4363 | - |
4364 | - def calc_boundaries(self): |
4365 | - if not self.bounds[self.main_dir]: |
4366 | - if self.stack: |
4367 | - max_data_value = max(sum(serie) for serie in self.data) |
4368 | - else: |
4369 | - max_data_value = max(max(serie) for serie in self.data) |
4370 | - self.bounds[self.main_dir] = (0, max_data_value) |
4371 | - if not self.bounds[other_direction(self.main_dir)]: |
4372 | - self.bounds[other_direction(self.main_dir)] = (0, len(self.data)) |
4373 | - |
4374 | - def calc_extents(self, direction): |
4375 | - self.max_value[direction] = 0 |
4376 | - if self.labels[direction]: |
4377 | - widest_word = max(self.labels[direction], key = lambda item: self.context.text_extents(item)[2]) |
4378 | - self.max_value[direction] = self.context.text_extents(widest_word)[3 - direction] |
4379 | - self.borders[other_direction(direction)] = (2-direction)*self.max_value[direction] + self.border + direction*(5) |
4380 | - else: |
4381 | - self.borders[other_direction(direction)] = self.border |
4382 | - |
4383 | - def calc_horz_extents(self): |
4384 | - self.calc_extents(HORZ) |
4385 | - |
4386 | - def calc_vert_extents(self): |
4387 | - self.calc_extents(VERT) |
4388 | - |
4389 | - def calc_all_extents(self): |
4390 | - self.calc_horz_extents() |
4391 | - self.calc_vert_extents() |
4392 | - other_dir = other_direction(self.main_dir) |
4393 | - self.value_label = 0 |
4394 | - if self.display_values: |
4395 | - if self.stack: |
4396 | - self.value_label = self.context.text_extents(str(max(sum(serie) for serie in self.data)))[2 + self.main_dir] |
4397 | - else: |
4398 | - self.value_label = self.context.text_extents(str(max(max(serie) for serie in self.data)))[2 + self.main_dir] |
4399 | - if self.labels[self.main_dir]: |
4400 | - self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - 2*self.borders[self.main_dir] - self.value_label |
4401 | - else: |
4402 | - self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - self.borders[self.main_dir] - 1.2*self.border - self.value_label |
4403 | - self.plot_dimensions[other_dir] = self.dimensions[other_dir] - self.borders[other_dir] - self.border |
4404 | - self.plot_top = self.dimensions[VERT] - self.borders[VERT] |
4405 | - |
4406 | - def calc_steps(self): |
4407 | - other_dir = other_direction(self.main_dir) |
4408 | - self.series_amplitude = self.bounds[self.main_dir][1] - self.bounds[self.main_dir][0] |
4409 | - if self.series_amplitude: |
4410 | - self.steps[self.main_dir] = float(self.plot_dimensions[self.main_dir])/self.series_amplitude |
4411 | - else: |
4412 | - self.steps[self.main_dir] = 0.00 |
4413 | - series_length = len(self.data) |
4414 | - self.steps[other_dir] = float(self.plot_dimensions[other_dir])/(series_length + 0.1*(series_length + 1)) |
4415 | - self.space = 0.1*self.steps[other_dir] |
4416 | - |
4417 | - def render(self): |
4418 | - self.calc_all_extents() |
4419 | - self.calc_steps() |
4420 | - self.render_background() |
4421 | - self.render_bounding_box() |
4422 | - if self.grid: |
4423 | - self.render_grid() |
4424 | - if self.three_dimension: |
4425 | - self.render_ground() |
4426 | - if self.display_values: |
4427 | - self.render_values() |
4428 | - self.render_labels() |
4429 | - self.render_plot() |
4430 | - if self.series_labels: |
4431 | - self.render_legend() |
4432 | - |
4433 | - def draw_3d_rectangle_front(self, x0, y0, x1, y1, shift): |
4434 | - self.context.rectangle(x0-shift, y0+shift, x1-x0, y1-y0) |
4435 | - |
4436 | - def draw_3d_rectangle_side(self, x0, y0, x1, y1, shift): |
4437 | - self.context.move_to(x1-shift,y0+shift) |
4438 | - self.context.line_to(x1, y0) |
4439 | - self.context.line_to(x1, y1) |
4440 | - self.context.line_to(x1-shift, y1+shift) |
4441 | - self.context.line_to(x1-shift, y0+shift) |
4442 | - self.context.close_path() |
4443 | - |
4444 | - def draw_3d_rectangle_top(self, x0, y0, x1, y1, shift): |
4445 | - self.context.move_to(x0-shift,y0+shift) |
4446 | - self.context.line_to(x0, y0) |
4447 | - self.context.line_to(x1, y0) |
4448 | - self.context.line_to(x1-shift, y0+shift) |
4449 | - self.context.line_to(x0-shift, y0+shift) |
4450 | - self.context.close_path() |
4451 | - |
4452 | - def draw_round_rectangle(self, x0, y0, x1, y1): |
4453 | - self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) |
4454 | - self.context.line_to(x1-5, y0) |
4455 | - self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) |
4456 | - self.context.line_to(x1, y1-5) |
4457 | - self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) |
4458 | - self.context.line_to(x0+5, y1) |
4459 | - self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) |
4460 | - self.context.line_to(x0, y0+5) |
4461 | - self.context.close_path() |
4462 | - |
4463 | - def render_ground(self): |
4464 | - self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
4465 | - self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
4466 | - self.context.fill() |
4467 | - |
4468 | - self.draw_3d_rectangle_side (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
4469 | - self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
4470 | - self.context.fill() |
4471 | - |
4472 | - self.draw_3d_rectangle_top (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
4473 | - self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
4474 | - self.context.fill() |
4475 | - |
4476 | - def render_labels(self): |
4477 | - self.context.set_font_size(self.font_size * 0.8) |
4478 | - if self.labels[HORZ]: |
4479 | - self.render_horz_labels() |
4480 | - if self.labels[VERT]: |
4481 | - self.render_vert_labels() |
4482 | - |
4483 | - def render_legend(self): |
4484 | - cr = self.context |
4485 | - cr.set_font_size(self.font_size) |
4486 | - cr.set_line_width(self.line_width) |
4487 | - |
4488 | - widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2]) |
4489 | - tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3]) |
4490 | - max_width = self.context.text_extents(widest_word)[2] |
4491 | - max_height = self.context.text_extents(tallest_word)[3] * 1.1 + 5 |
4492 | - |
4493 | - color_box_height = max_height / 2 |
4494 | - color_box_width = color_box_height * 2 |
4495 | - |
4496 | - #Draw a bounding box |
4497 | - bounding_box_width = max_width + color_box_width + 15 |
4498 | - bounding_box_height = (len(self.series_labels)+0.5) * max_height |
4499 | - cr.set_source_rgba(1,1,1) |
4500 | - cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border, |
4501 | - bounding_box_width, bounding_box_height) |
4502 | - cr.fill() |
4503 | - |
4504 | - cr.set_source_rgba(*self.line_color) |
4505 | - cr.set_line_width(self.line_width) |
4506 | - cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border, |
4507 | - bounding_box_width, bounding_box_height) |
4508 | - cr.stroke() |
4509 | - |
4510 | - for idx,key in enumerate(self.series_labels): |
4511 | - #Draw color box |
4512 | - cr.set_source_rgba(*self.series_colors[idx]) |
4513 | - cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, |
4514 | - self.border + color_box_height + (idx*max_height) , |
4515 | - color_box_width, color_box_height) |
4516 | - cr.fill() |
4517 | - |
4518 | - cr.set_source_rgba(0, 0, 0) |
4519 | - cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, |
4520 | - self.border + color_box_height + (idx*max_height), |
4521 | - color_box_width, color_box_height) |
4522 | - cr.stroke() |
4523 | - |
4524 | - #Draw series labels |
4525 | - cr.set_source_rgba(0, 0, 0) |
4526 | - cr.move_to(self.dimensions[HORZ] - self.border - max_width - 5, self.border + ((idx+1)*max_height)) |
4527 | - cr.show_text(key) |
4528 | - |
4529 | - |
4530 | -class HorizontalBarPlot(BarPlot): |
4531 | - def __init__(self, |
4532 | - surface = None, |
4533 | - data = None, |
4534 | - width = 640, |
4535 | - height = 480, |
4536 | - background = "white light_gray", |
4537 | - border = 0, |
4538 | - series_labels = False, |
4539 | - display_values = False, |
4540 | - grid = False, |
4541 | - rounded_corners = False, |
4542 | - stack = False, |
4543 | - three_dimension = False, |
4544 | - x_labels = None, |
4545 | - y_labels = None, |
4546 | - x_bounds = None, |
4547 | - y_bounds = None, |
4548 | - series_colors = None): |
4549 | - |
4550 | - BarPlot.__init__(self, surface, data, width, height, background, border, |
4551 | - display_values, grid, rounded_corners, stack, three_dimension, |
4552 | - x_labels, y_labels, x_bounds, y_bounds, series_colors, HORZ) |
4553 | - self.series_labels = series_labels |
4554 | - |
4555 | - def calc_vert_extents(self): |
4556 | - self.calc_extents(VERT) |
4557 | - if self.labels[HORZ] and not self.labels[VERT]: |
4558 | - self.borders[HORZ] += 10 |
4559 | - |
4560 | - def draw_rectangle_bottom(self, x0, y0, x1, y1): |
4561 | - self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) |
4562 | - self.context.line_to(x0, y0+5) |
4563 | - self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) |
4564 | - self.context.line_to(x1, y0) |
4565 | - self.context.line_to(x1, y1) |
4566 | - self.context.line_to(x0+5, y1) |
4567 | - self.context.close_path() |
4568 | - |
4569 | - def draw_rectangle_top(self, x0, y0, x1, y1): |
4570 | - self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) |
4571 | - self.context.line_to(x1, y1-5) |
4572 | - self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) |
4573 | - self.context.line_to(x0, y1) |
4574 | - self.context.line_to(x0, y0) |
4575 | - self.context.line_to(x1, y0) |
4576 | - self.context.close_path() |
4577 | - |
4578 | - def draw_rectangle(self, index, length, x0, y0, x1, y1): |
4579 | - if length == 1: |
4580 | - BarPlot.draw_rectangle(self, x0, y0, x1, y1) |
4581 | - elif index == 0: |
4582 | - self.draw_rectangle_bottom(x0, y0, x1, y1) |
4583 | - elif index == length-1: |
4584 | - self.draw_rectangle_top(x0, y0, x1, y1) |
4585 | - else: |
4586 | - self.context.rectangle(x0, y0, x1-x0, y1-y0) |
4587 | - |
4588 | - #TODO: Review BarPlot.render_grid code |
4589 | - def render_grid(self): |
4590 | - self.context.set_source_rgba(0.8, 0.8, 0.8) |
4591 | - if self.labels[HORZ]: |
4592 | - self.context.set_font_size(self.font_size * 0.8) |
4593 | - step = (self.dimensions[HORZ] - 2*self.borders[HORZ] - self.value_label)/(len(self.labels[HORZ])-1) |
4594 | - x = self.borders[HORZ] |
4595 | - next_x = 0 |
4596 | - for item in self.labels[HORZ]: |
4597 | - width = self.context.text_extents(item)[2] |
4598 | - if x - width/2 > next_x and x - width/2 > self.border: |
4599 | - self.context.move_to(x, self.border) |
4600 | - self.context.line_to(x, self.dimensions[VERT] - self.borders[VERT]) |
4601 | - self.context.stroke() |
4602 | - next_x = x + width/2 |
4603 | - x += step |
4604 | - else: |
4605 | - lines = 11 |
4606 | - horizontal_step = float(self.plot_dimensions[HORZ])/(lines-1) |
4607 | - x = self.borders[HORZ] |
4608 | - for y in xrange(0, lines): |
4609 | - self.context.move_to(x, self.border) |
4610 | - self.context.line_to(x, self.dimensions[VERT] - self.borders[VERT]) |
4611 | - self.context.stroke() |
4612 | - x += horizontal_step |
4613 | - |
4614 | - def render_horz_labels(self): |
4615 | - step = (self.dimensions[HORZ] - 2*self.borders[HORZ])/(len(self.labels[HORZ])-1) |
4616 | - x = self.borders[HORZ] |
4617 | - next_x = 0 |
4618 | - |
4619 | - for item in self.labels[HORZ]: |
4620 | - self.context.set_source_rgba(*self.label_color) |
4621 | - width = self.context.text_extents(item)[2] |
4622 | - if x - width/2 > next_x and x - width/2 > self.border: |
4623 | - self.context.move_to(x - width/2, self.dimensions[VERT] - self.borders[VERT] + self.max_value[HORZ] + 3) |
4624 | - self.context.show_text(item) |
4625 | - next_x = x + width/2 |
4626 | - x += step |
4627 | - |
4628 | - def render_vert_labels(self): |
4629 | - series_length = len(self.labels[VERT]) |
4630 | - step = (self.plot_dimensions[VERT] - (series_length + 1)*self.space)/(len(self.labels[VERT])) |
4631 | - y = self.border + step/2 + self.space |
4632 | - |
4633 | - for item in self.labels[VERT]: |
4634 | - self.context.set_source_rgba(*self.label_color) |
4635 | - width, height = self.context.text_extents(item)[2:4] |
4636 | - self.context.move_to(self.borders[HORZ] - width - 5, y + height/2) |
4637 | - self.context.show_text(item) |
4638 | - y += step + self.space |
4639 | - self.labels[VERT].reverse() |
4640 | - |
4641 | - def render_values(self): |
4642 | - self.context.set_source_rgba(*self.value_label_color) |
4643 | - self.context.set_font_size(self.font_size * 0.8) |
4644 | - if self.stack: |
4645 | - for i,series in enumerate(self.data): |
4646 | - value = sum(series) |
4647 | - height = self.context.text_extents(str(value))[3] |
4648 | - x = self.borders[HORZ] + value*self.steps[HORZ] + 2 |
4649 | - y = self.borders[VERT] + (i+0.5)*self.steps[VERT] + (i+1)*self.space + height/2 |
4650 | - self.context.move_to(x, y) |
4651 | - self.context.show_text(str(value)) |
4652 | - else: |
4653 | - for i,series in enumerate(self.data): |
4654 | - inner_step = self.steps[VERT]/len(series) |
4655 | - y0 = self.border + i*self.steps[VERT] + (i+1)*self.space |
4656 | - for number,key in enumerate(series): |
4657 | - height = self.context.text_extents(str(key))[3] |
4658 | - self.context.move_to(self.borders[HORZ] + key*self.steps[HORZ] + 2, y0 + 0.5*inner_step + height/2, ) |
4659 | - self.context.show_text(str(key)) |
4660 | - y0 += inner_step |
4661 | - |
4662 | - def render_plot(self): |
4663 | - if self.stack: |
4664 | - for i,series in enumerate(self.data): |
4665 | - x0 = self.borders[HORZ] |
4666 | - y0 = self.borders[VERT] + i*self.steps[VERT] + (i+1)*self.space |
4667 | - for number,key in enumerate(series): |
4668 | - linear = cairo.LinearGradient( key*self.steps[HORZ]/2, y0, key*self.steps[HORZ]/2, y0 + self.steps[VERT] ) |
4669 | - color = self.series_colors[number] |
4670 | - linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
4671 | - linear.add_color_stop_rgba(1.0, *color) |
4672 | - self.context.set_source(linear) |
4673 | - if self.rounded_corners: |
4674 | - self.draw_rectangle(number, len(series), x0, y0, x0+key*self.steps[HORZ], y0+self.steps[VERT]) |
4675 | - self.context.fill() |
4676 | - else: |
4677 | - self.context.rectangle(x0, y0, key*self.steps[HORZ], self.steps[VERT]) |
4678 | - self.context.fill() |
4679 | - x0 += key*self.steps[HORZ] |
4680 | - else: |
4681 | - for i,series in enumerate(self.data): |
4682 | - inner_step = self.steps[VERT]/len(series) |
4683 | - x0 = self.borders[HORZ] |
4684 | - y0 = self.border + i*self.steps[VERT] + (i+1)*self.space |
4685 | - for number,key in enumerate(series): |
4686 | - linear = cairo.LinearGradient(key*self.steps[HORZ]/2, y0, key*self.steps[HORZ]/2, y0 + inner_step) |
4687 | - color = self.series_colors[number] |
4688 | - linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
4689 | - linear.add_color_stop_rgba(1.0, *color) |
4690 | - self.context.set_source(linear) |
4691 | - if self.rounded_corners and key != 0: |
4692 | - BarPlot.draw_round_rectangle(self,x0, y0, x0 + key*self.steps[HORZ], y0 + inner_step) |
4693 | - self.context.fill() |
4694 | - else: |
4695 | - self.context.rectangle(x0, y0, key*self.steps[HORZ], inner_step) |
4696 | - self.context.fill() |
4697 | - y0 += inner_step |
4698 | - |
4699 | -class VerticalBarPlot(BarPlot): |
4700 | - def __init__(self, |
4701 | - surface = None, |
4702 | - data = None, |
4703 | - width = 640, |
4704 | - height = 480, |
4705 | - background = "white light_gray", |
4706 | - border = 0, |
4707 | - series_labels = None, |
4708 | - display_values = False, |
4709 | - grid = False, |
4710 | - rounded_corners = False, |
4711 | - stack = False, |
4712 | - three_dimension = False, |
4713 | - x_labels = None, |
4714 | - y_labels = None, |
4715 | - x_bounds = None, |
4716 | - y_bounds = None, |
4717 | - series_colors = None): |
4718 | - |
4719 | - BarPlot.__init__(self, surface, data, width, height, background, border, |
4720 | - display_values, grid, rounded_corners, stack, three_dimension, |
4721 | - x_labels, y_labels, x_bounds, y_bounds, series_colors, VERT) |
4722 | - self.series_labels = series_labels |
4723 | - |
4724 | - def calc_vert_extents(self): |
4725 | - self.calc_extents(VERT) |
4726 | - if self.labels[VERT] and not self.labels[HORZ]: |
4727 | - self.borders[VERT] += 10 |
4728 | - |
4729 | - def draw_rectangle_bottom(self, x0, y0, x1, y1): |
4730 | - self.context.move_to(x1,y1) |
4731 | - self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) |
4732 | - self.context.line_to(x0+5, y1) |
4733 | - self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) |
4734 | - self.context.line_to(x0, y0) |
4735 | - self.context.line_to(x1, y0) |
4736 | - self.context.line_to(x1, y1) |
4737 | - self.context.close_path() |
4738 | - |
4739 | - def draw_rectangle_top(self, x0, y0, x1, y1): |
4740 | - self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) |
4741 | - self.context.line_to(x1-5, y0) |
4742 | - self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) |
4743 | - self.context.line_to(x1, y1) |
4744 | - self.context.line_to(x0, y1) |
4745 | - self.context.line_to(x0, y0) |
4746 | - self.context.close_path() |
4747 | - |
4748 | - def draw_rectangle(self, index, length, x0, y0, x1, y1): |
4749 | - if length == 1: |
4750 | - BarPlot.draw_rectangle(self, x0, y0, x1, y1) |
4751 | - elif index == 0: |
4752 | - self.draw_rectangle_bottom(x0, y0, x1, y1) |
4753 | - elif index == length-1: |
4754 | - self.draw_rectangle_top(x0, y0, x1, y1) |
4755 | - else: |
4756 | - self.context.rectangle(x0, y0, x1-x0, y1-y0) |
4757 | - |
4758 | - def render_grid(self): |
4759 | - self.context.set_source_rgba(0.8, 0.8, 0.8) |
4760 | - if self.labels[VERT]: |
4761 | - lines = len(self.labels[VERT]) |
4762 | - vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1) |
4763 | - y = self.borders[VERT] + self.value_label |
4764 | - else: |
4765 | - lines = 11 |
4766 | - vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1) |
4767 | - y = 1.2*self.border + self.value_label |
4768 | - for x in xrange(0, lines): |
4769 | - self.context.move_to(self.borders[HORZ], y) |
4770 | - self.context.line_to(self.dimensions[HORZ] - self.border, y) |
4771 | - self.context.stroke() |
4772 | - y += vertical_step |
4773 | - |
4774 | - def render_ground(self): |
4775 | - self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
4776 | - self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
4777 | - self.context.fill() |
4778 | - |
4779 | - self.draw_3d_rectangle_side (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
4780 | - self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
4781 | - self.context.fill() |
4782 | - |
4783 | - self.draw_3d_rectangle_top (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], |
4784 | - self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) |
4785 | - self.context.fill() |
4786 | - |
4787 | - def render_horz_labels(self): |
4788 | - series_length = len(self.labels[HORZ]) |
4789 | - step = float (self.plot_dimensions[HORZ] - (series_length + 1)*self.space)/len(self.labels[HORZ]) |
4790 | - x = self.borders[HORZ] + step/2 + self.space |
4791 | - next_x = 0 |
4792 | - |
4793 | - for item in self.labels[HORZ]: |
4794 | - self.context.set_source_rgba(*self.label_color) |
4795 | - width = self.context.text_extents(item)[2] |
4796 | - if x - width/2 > next_x and x - width/2 > self.borders[HORZ]: |
4797 | - self.context.move_to(x - width/2, self.dimensions[VERT] - self.borders[VERT] + self.max_value[HORZ] + 3) |
4798 | - self.context.show_text(item) |
4799 | - next_x = x + width/2 |
4800 | - x += step + self.space |
4801 | - |
4802 | - def render_vert_labels(self): |
4803 | - self.context.set_source_rgba(*self.label_color) |
4804 | - y = self.borders[VERT] + self.value_label |
4805 | - step = (self.dimensions[VERT] - 2*self.borders[VERT] - self.value_label)/(len(self.labels[VERT]) - 1) |
4806 | - self.labels[VERT].reverse() |
4807 | - for item in self.labels[VERT]: |
4808 | - width, height = self.context.text_extents(item)[2:4] |
4809 | - self.context.move_to(self.borders[HORZ] - width - 5, y + height/2) |
4810 | - self.context.show_text(item) |
4811 | - y += step |
4812 | - self.labels[VERT].reverse() |
4813 | - |
4814 | - def render_values(self): |
4815 | - self.context.set_source_rgba(*self.value_label_color) |
4816 | - self.context.set_font_size(self.font_size * 0.8) |
4817 | - if self.stack: |
4818 | - for i,series in enumerate(self.data): |
4819 | - value = sum(series) |
4820 | - width = self.context.text_extents(str(value))[2] |
4821 | - x = self.borders[HORZ] + (i+0.5)*self.steps[HORZ] + (i+1)*self.space - width/2 |
4822 | - y = value*self.steps[VERT] + 2 |
4823 | - self.context.move_to(x, self.plot_top-y) |
4824 | - self.context.show_text(str(value)) |
4825 | - else: |
4826 | - for i,series in enumerate(self.data): |
4827 | - inner_step = self.steps[HORZ]/len(series) |
4828 | - x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space |
4829 | - for number,key in enumerate(series): |
4830 | - width = self.context.text_extents(str(key))[2] |
4831 | - self.context.move_to(x0 + 0.5*inner_step - width/2, self.plot_top - key*self.steps[VERT] - 2) |
4832 | - self.context.show_text(str(key)) |
4833 | - x0 += inner_step |
4834 | - |
4835 | - def render_plot(self): |
4836 | - if self.stack: |
4837 | - for i,series in enumerate(self.data): |
4838 | - x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space |
4839 | - y0 = 0 |
4840 | - for number,key in enumerate(series): |
4841 | - linear = cairo.LinearGradient( x0, key*self.steps[VERT]/2, x0 + self.steps[HORZ], key*self.steps[VERT]/2 ) |
4842 | - color = self.series_colors[number] |
4843 | - linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
4844 | - linear.add_color_stop_rgba(1.0, *color) |
4845 | - self.context.set_source(linear) |
4846 | - if self.rounded_corners: |
4847 | - self.draw_rectangle(number, len(series), x0, self.plot_top - y0 - key*self.steps[VERT], x0 + self.steps[HORZ], self.plot_top - y0) |
4848 | - self.context.fill() |
4849 | - else: |
4850 | - self.context.rectangle(x0, self.plot_top - y0 - key*self.steps[VERT], self.steps[HORZ], key*self.steps[VERT]) |
4851 | - self.context.fill() |
4852 | - y0 += key*self.steps[VERT] |
4853 | - else: |
4854 | - for i,series in enumerate(self.data): |
4855 | - inner_step = self.steps[HORZ]/len(series) |
4856 | - y0 = self.borders[VERT] |
4857 | - x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space |
4858 | - for number,key in enumerate(series): |
4859 | - linear = cairo.LinearGradient( x0, key*self.steps[VERT]/2, x0 + inner_step, key*self.steps[VERT]/2 ) |
4860 | - color = self.series_colors[number] |
4861 | - linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) |
4862 | - linear.add_color_stop_rgba(1.0, *color) |
4863 | - self.context.set_source(linear) |
4864 | - if self.rounded_corners and key != 0: |
4865 | - BarPlot.draw_round_rectangle(self, x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top) |
4866 | - self.context.fill() |
4867 | - elif self.three_dimension: |
4868 | - self.draw_3d_rectangle_front(x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top, 5) |
4869 | - self.context.fill() |
4870 | - self.draw_3d_rectangle_side(x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top, 5) |
4871 | - self.context.fill() |
4872 | - self.draw_3d_rectangle_top(x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top, 5) |
4873 | - self.context.fill() |
4874 | - else: |
4875 | - self.context.rectangle(x0, self.plot_top - key*self.steps[VERT], inner_step, key*self.steps[VERT]) |
4876 | - self.context.fill() |
4877 | - |
4878 | - x0 += inner_step |
4879 | - |
4880 | -class PiePlot(Plot): |
4881 | - def __init__ (self, |
4882 | - surface = None, |
4883 | - data = None, |
4884 | - width = 640, |
4885 | - height = 480, |
4886 | - background = "white light_gray", |
4887 | - gradient = False, |
4888 | - shadow = False, |
4889 | - colors = None): |
4890 | - |
4891 | - Plot.__init__( self, surface, data, width, height, background, series_colors = colors ) |
4892 | - self.center = (self.dimensions[HORZ]/2, self.dimensions[VERT]/2) |
4893 | - self.total = sum(self.data) |
4894 | - self.radius = min(self.dimensions[HORZ]/3,self.dimensions[VERT]/3) |
4895 | - self.gradient = gradient |
4896 | - self.shadow = shadow |
4897 | - |
4898 | - def load_series(self, data, x_labels=None, y_labels=None, series_colors=None): |
4899 | - Plot.load_series(self, data, x_labels, y_labels, series_colors) |
4900 | - self.data = sorted(self.data) |
4901 | - |
4902 | - def draw_piece(self, angle, next_angle): |
4903 | - self.context.move_to(self.center[0],self.center[1]) |
4904 | - self.context.line_to(self.center[0] + self.radius*math.cos(angle), self.center[1] + self.radius*math.sin(angle)) |
4905 | - self.context.arc(self.center[0], self.center[1], self.radius, angle, next_angle) |
4906 | - self.context.line_to(self.center[0], self.center[1]) |
4907 | - self.context.close_path() |
4908 | - |
4909 | - def render(self): |
4910 | - self.render_background() |
4911 | - self.render_bounding_box() |
4912 | - if self.shadow: |
4913 | - self.render_shadow() |
4914 | - self.render_plot() |
4915 | - self.render_series_labels() |
4916 | - |
4917 | - def render_shadow(self): |
4918 | - horizontal_shift = 3 |
4919 | - vertical_shift = 3 |
4920 | - self.context.set_source_rgba(0, 0, 0, 0.5) |
4921 | - self.context.arc(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.radius, 0, 2*math.pi) |
4922 | - self.context.fill() |
4923 | - |
4924 | - def render_series_labels(self): |
4925 | - angle = 0 |
4926 | - next_angle = 0 |
4927 | - x0,y0 = self.center |
4928 | - cr = self.context |
4929 | - for number,key in enumerate(self.series_labels): |
4930 | - next_angle = angle + 2.0*math.pi*self.data[number]/self.total |
4931 | - cr.set_source_rgba(*self.series_colors[number]) |
4932 | - w = cr.text_extents(key)[2] |
4933 | - if (angle + next_angle)/2 < math.pi/2 or (angle + next_angle)/2 > 3*math.pi/2: |
4934 | - cr.move_to(x0 + (self.radius+10)*math.cos((angle+next_angle)/2), y0 + (self.radius+10)*math.sin((angle+next_angle)/2) ) |
4935 | - else: |
4936 | - cr.move_to(x0 + (self.radius+10)*math.cos((angle+next_angle)/2) - w, y0 + (self.radius+10)*math.sin((angle+next_angle)/2) ) |
4937 | - cr.show_text(key) |
4938 | - angle = next_angle |
4939 | - |
4940 | - def render_plot(self): |
4941 | - angle = 3*math.pi/2.0 |
4942 | - next_angle = 0 |
4943 | - x0,y0 = self.center |
4944 | - cr = self.context |
4945 | - for number,series in enumerate(self.data): |
4946 | - next_angle = angle + 2.0*math.pi*series/self.total |
4947 | - if self.gradient: |
4948 | - gradient_color = cairo.RadialGradient(self.center[0], self.center[1], 0, self.center[0], self.center[1], self.radius) |
4949 | - gradient_color.add_color_stop_rgba(0.3, *self.series_colors[number]) |
4950 | - gradient_color.add_color_stop_rgba(1, self.series_colors[number][0]*0.7, |
4951 | - self.series_colors[number][1]*0.7, |
4952 | - self.series_colors[number][2]*0.7, |
4953 | - self.series_colors[number][3]) |
4954 | - cr.set_source(gradient_color) |
4955 | - else: |
4956 | - cr.set_source_rgba(*self.series_colors[number]) |
4957 | - |
4958 | - self.draw_piece(angle, next_angle) |
4959 | - cr.fill() |
4960 | - |
4961 | - cr.set_source_rgba(1.0, 1.0, 1.0) |
4962 | - self.draw_piece(angle, next_angle) |
4963 | - cr.stroke() |
4964 | - |
4965 | - angle = next_angle |
4966 | - |
4967 | -class DonutPlot(PiePlot): |
4968 | - def __init__ (self, |
4969 | - surface = None, |
4970 | - data = None, |
4971 | - width = 640, |
4972 | - height = 480, |
4973 | - background = "white light_gray", |
4974 | - gradient = False, |
4975 | - shadow = False, |
4976 | - colors = None, |
4977 | - inner_radius=-1): |
4978 | - |
4979 | - Plot.__init__( self, surface, data, width, height, background, series_colors = colors ) |
4980 | - |
4981 | - self.center = ( self.dimensions[HORZ]/2, self.dimensions[VERT]/2 ) |
4982 | - self.total = sum( self.data ) |
4983 | - self.radius = min( self.dimensions[HORZ]/3,self.dimensions[VERT]/3 ) |
4984 | - self.inner_radius = inner_radius*self.radius |
4985 | - |
4986 | - if inner_radius == -1: |
4987 | - self.inner_radius = self.radius/3 |
4988 | - |
4989 | - self.gradient = gradient |
4990 | - self.shadow = shadow |
4991 | - |
4992 | - def draw_piece(self, angle, next_angle): |
4993 | - self.context.move_to(self.center[0] + (self.inner_radius)*math.cos(angle), self.center[1] + (self.inner_radius)*math.sin(angle)) |
4994 | - self.context.line_to(self.center[0] + self.radius*math.cos(angle), self.center[1] + self.radius*math.sin(angle)) |
4995 | - self.context.arc(self.center[0], self.center[1], self.radius, angle, next_angle) |
4996 | - self.context.line_to(self.center[0] + (self.inner_radius)*math.cos(next_angle), self.center[1] + (self.inner_radius)*math.sin(next_angle)) |
4997 | - self.context.arc_negative(self.center[0], self.center[1], self.inner_radius, next_angle, angle) |
4998 | - self.context.close_path() |
4999 | - |
5000 | - def render_shadow(self): |
The diff has been truncated for viewing.
Finished "translating" cairoplot to work with Series.
Created different test files:
- tests.py: Tests the retrocampatibility with the old sintax for cairoplot;
- seriestests.py: Tests the Series new feature.
All tests completed successfully.