Launchpad automatic translations update.
[odoo/odoo.git] / openerp / pychart / line_plot.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
4
5 # Jockey is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option) any
8 # later version.
9 #
10 # Jockey is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 # for more details.
14 #
15 import tick_mark
16 import line_style
17 import pychart_util
18 import error_bar
19 import chart_object
20 import legend
21 import object_set
22 import line_plot_doc
23 import theme
24 from pychart_types import *
25 from types import *
26
27 default_width = 1.2
28 line_style_itr = None
29
30
31 _keys = {
32     "data" : (AnyType, None, pychart_util.data_desc),
33     "label": (StringType, "???", pychart_util.label_desc),
34     "data_label_offset": (CoordType, (0, 5),
35                           """The location of data labels relative to the sample point. Meaningful only when data_label_format != None."""),
36     "data_label_format": (FormatType, None,
37                           """The format string for the label printed 
38                           beside a sample point.
39                           It can be a `printf' style format string, or 
40                           a two-parameter function that takes the (x, y)
41                           values and returns a string. """
42                           + pychart_util.string_desc),
43     "xcol" : (IntType, 0, pychart_util.xcol_desc),
44     "ycol": (IntType, 1, pychart_util.ycol_desc),
45     "y_error_minus_col": (IntType, 2,
46                           """The column (within "data") from which the depth of the errorbar is extracted. Meaningful only when error_bar != None. <<error_bar>>"""),
47     "y_error_plus_col": (IntType, -1,
48                          """The column (within "data") from which the height of the errorbar is extracted. Meaningful only when error_bar != None. <<error_bar>>"""),
49     "y_qerror_minus_col":  (IntType, -1, "<<error_bar>>"),
50     "y_qerror_plus_col":  (IntType, -1, "<<error_bar>>"),
51
52     "line_style": (line_style.T, lambda: line_style_itr.next(), pychart_util.line_desc,
53                    "By default, a style is picked from standard styles round-robin. <<line_style>>"),
54
55     "tick_mark": (tick_mark.T, None, pychart_util.tick_mark_desc),
56     "error_bar": (error_bar.T, None,
57                   "The style of the error bar. <<error_bar>>"),
58     }
59
60 class T(chart_object.T):
61     __doc__ = line_plot_doc.doc
62     keys =  _keys
63     def check_integrity(self):
64         self.type_check()
65         
66 ##AUTOMATICALLY GENERATED
67
68 ##END AUTOMATICALLY GENERATED
69     def get_data_range(self, which):
70         if which == 'X':
71             return pychart_util.get_data_range(self.data, self.xcol)
72         else:
73             return pychart_util.get_data_range(self.data, self.ycol)
74     def get_legend_entry(self):
75         if self.label:
76             return legend.Entry(line_style=self.line_style,
77                                 tick_mark=self.tick_mark,
78                                 fill_style=None,
79                                 label=self.label)
80         return None
81     
82     def draw(self, ar, can):
83
84         # Draw the line
85
86         clipbox = theme.adjust_bounding_box([ar.loc[0], ar.loc[1],
87                                              ar.loc[0] + ar.size[0],
88                                              ar.loc[1] + ar.size[1]]);
89         
90         can.clip(clipbox[0],clipbox[1],clipbox[2],clipbox[3])
91         if self.line_style:
92             points = []
93             for pair in self.data:
94                 yval = pychart_util.get_sample_val(pair, self.ycol)
95                 xval = pair[self.xcol]
96                 if None not in (xval, yval):
97                     points.append((ar.x_pos(xval), ar.y_pos(yval)))
98             can.lines(self.line_style, points)
99         can.endclip()
100         
101         # Draw tick marks and error bars
102         can.clip(ar.loc[0] - 10, ar.loc[1] - 10,
103                 ar.loc[0] + ar.size[0] + 10,
104                 ar.loc[1] + ar.size[1] + 10)
105         for pair in self.data:
106             x = pair[self.xcol]
107             y = pychart_util.get_sample_val(pair, self.ycol)
108             if None in (x, y): continue
109             
110             x_pos = ar.x_pos(x)
111             y_pos = ar.y_pos(y)
112
113             if self.error_bar:
114                 plus = pair[self.y_error_plus_col or self.y_error_minus_col]
115                 minus = pair[self.y_error_minus_col or self.y_error_plus_col]
116                 if self.y_qerror_minus_col or self.y_qerror_plus_col:
117                     q_plus = pair[self.y_qerror_plus_col or self.y_qerror_minus_col]
118                     q_minus = pair[self.y_qerror_minus_col or self.y_qerror_plus_col]
119                     if None not in (minus,plus,q_minus,q_plus):
120                         self.error_bar.draw(can, (x_pos, y_pos),
121                                             ar.y_pos(y - minus),
122                                             ar.y_pos(y + plus),
123                                             ar.y_pos(y - q_minus),
124                                             ar.y_pos(y + q_plus))
125                 else:
126                     if None not in (minus,plus): #PDS
127                         self.error_bar.draw(can, (x_pos, y_pos),
128                                             ar.y_pos(y - minus),
129                                             ar.y_pos(y + plus))
130                         
131             if self.tick_mark:
132                 self.tick_mark.draw(can, x_pos, y_pos)
133             if self.data_label_format:
134                 can.show(x_pos + self.data_label_offset[0],
135                             y_pos + self.data_label_offset[1],
136                             "/hC" + pychart_util.apply_format(self.data_label_format, (x, y), 1))
137
138         can.endclip()
139
140 def init():
141     global line_style_itr
142     line_styles = object_set.T()
143     for org_style in line_style.standards.list():
144         style = line_style.T(width = default_width, color = org_style.color,
145                              dash = org_style.dash)
146         line_styles.add(style)
147
148     line_style_itr = line_styles.iterate()
149
150 theme.add_reinitialization_hook(init)
151