~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to enthought/chaco/filled_line_plot.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 19:03:54 UTC
  • mfrom: (7.2.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110406190354-rwd55l2ezjecfo41
Tags: 3.4.0-2
d/rules: fix pyshared directory path (Closes: #621116)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
 
2
from __future__ import with_statement
2
3
 
 
4
from numpy import empty
3
5
from enthought.traits.api import Property, Enum
4
6
 
5
7
# Local imports
 
8
from lineplot import LinePlot
6
9
from polygon_plot import PolygonPlot
7
10
 
8
11
 
20
23
    # Direction to fill. Down is towards the origin, up is towards the max
21
24
    fill_direction = Enum("down", "up")
22
25
 
 
26
    # The rendering style of the line plot.
 
27
    #
 
28
    # connectedpoints
 
29
    #     "normal" style (default); each point is connected to subsequent and 
 
30
    #     prior points by line segments
 
31
    # hold
 
32
    #     each point is represented by a line segment parallel to the abscissa
 
33
    #     (index axis) and spanning the length between the point and its
 
34
    #     subsequent point.
 
35
    # connectedhold
 
36
    #     like "hold" style, but line segments are drawn at each point of the
 
37
    #     plot to connect the hold lines of the prior point and the current
 
38
    #     point.  Also called a "right angle plot".
 
39
    render_style = Enum("connectedpoints", "hold", "connectedhold")
 
40
 
23
41
    def _render(self, gc, points):
24
42
        if len(points) == 0:
25
43
            return
26
44
 
 
45
        render_method_dict = {
 
46
                "hold": LinePlot._render_hold,
 
47
                "connectedhold": LinePlot._render_connected_hold,
 
48
                "connectedpoints": LinePlot._render_normal
 
49
                }
 
50
        render_lines = render_method_dict.get(self.render_style, LinePlot._render_normal)
 
51
        
27
52
        if self.fill_direction == 'down':
28
53
            ox, oy = self.map_screen([[0,0]])[0]
29
54
        else:
30
55
            ox, oy = self.map_screen([[self.x_mapper.range.high, 
31
56
                                      self.y_mapper.range.high]])[0]
32
 
        gc.save_state()
33
 
        
34
 
        try:
 
57
 
 
58
        with gc:
35
59
            gc.clip_to_rect(self.x, self.y, self.width, self.height)
36
60
    
37
61
            # If the fill color is not transparent, then draw the fill polygon first
38
62
            face_col = self.face_color_
39
63
            if not (len(face_col) == 4 and face_col[-1] == 0):
40
 
                gc.set_fill_color(face_col)
41
 
                gc.begin_path()
42
 
                startx, starty = points[0]
43
 
                if self.orientation == "h":
44
 
                    gc.move_to(startx, oy)
45
 
                    gc.line_to(startx, starty)
46
 
                else:
47
 
                    gc.move_to(ox, starty)
48
 
                    gc.line_to(startx, starty)
49
 
                
50
 
                gc.lines(points)
51
 
    
52
 
                endx, endy = points[-1]
53
 
                if self.orientation == "h":
54
 
                    gc.line_to(endx, oy)
55
 
                    gc.line_to(startx, oy)
56
 
                else:
57
 
                    gc.line_to(ox, endy)
58
 
                    gc.line_to(ox, starty)
59
 
    
60
 
                gc.close_path()
61
 
                gc.fill_path()
 
64
                if self.render_style in ("hold","connectedhold"):
 
65
                    # Modify the points array before passing it in to render_polys:
 
66
                    # Between every two points, create an intermediate point with
 
67
                    # the first point's Y and the second point's X.  (For vertical
 
68
                    # plots, use the first point's X and the second point's Y.)
 
69
                    new_points = empty((points.shape[0]*2-1, 2))
 
70
                    new_points[::2] = points
 
71
                    if self.orientation == "h":
 
72
                        new_points[1::2,0] = points[1:,0]
 
73
                        new_points[1::2,1] = points[:-1,1]
 
74
                    else:
 
75
                        new_points[1::2,0] = points[:-1,0]
 
76
                        new_points[1::2,1] = points[1:,1]
 
77
                    points = new_points
 
78
 
 
79
                self._render_polys(gc, points, ox, oy)
62
80
    
63
81
            # If the line color is not transparent, or tha same color
64
82
            # as the filled area:
67
85
                gc.set_stroke_color(edge_col)
68
86
                gc.set_line_width(self.edge_width)
69
87
                gc.set_line_dash(self.edge_style_)
70
 
                gc.begin_path()
71
 
                gc.move_to(*points[0])
72
 
                gc.lines(points)
73
 
                gc.stroke_path()
74
 
                
75
 
        finally:
76
 
            gc.restore_state()
 
88
                # Create a list around points because the LinePlot supports
 
89
                # Nans, and its rendering methods expect lists of disjoint arrays.
 
90
                render_lines(gc, [points], self.orientation)
 
91
 
 
92
 
 
93
    def _render_polys(self, gc, points, ox, oy):
 
94
        face_col = self.face_color_
 
95
        gc.set_fill_color(face_col)
 
96
        gc.begin_path()
 
97
        startx, starty = points[0]
 
98
        if self.orientation == "h":
 
99
            gc.move_to(startx, oy)
 
100
            gc.line_to(startx, starty)
 
101
        else:
 
102
            gc.move_to(ox, starty)
 
103
            gc.line_to(startx, starty)
 
104
        
 
105
        gc.lines(points)
 
106
 
 
107
        endx, endy = points[-1]
 
108
        if self.orientation == "h":
 
109
            gc.line_to(endx, oy)
 
110
            gc.line_to(startx, oy)
 
111
        else:
 
112
            gc.line_to(ox, endy)
 
113
            gc.line_to(ox, starty)
 
114
 
 
115
        gc.close_path()
 
116
        gc.fill_path()
77
117
 
78
118
 
79
119