~openerp/openobject-server/web-dashboard

« back to all changes in this revision

Viewing changes to bin/pychart/line_plot.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

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