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

« back to all changes in this revision

Viewing changes to chaco/horizon_plot.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import with_statement
 
2
 
 
3
from numpy import array, transpose, ndarray, empty
 
4
from traits.api import Instance, DelegatesTo, Bool, Int
 
5
 
 
6
from enable.api import transparent_color_trait
 
7
from chaco.color_mapper import ColorMapper
 
8
from chaco.base_xy_plot import BaseXYPlot
 
9
from chaco.linear_mapper import LinearMapper
 
10
 
 
11
class BandedMapper(LinearMapper):
 
12
    bands = Int(4)
 
13
 
 
14
    def map_screen(self, data_array):
 
15
        self._compute_scale()
 
16
 
 
17
        if self._null_data_range:
 
18
            if isinstance(data_array, (tuple, list, ndarray)):
 
19
                x = empty(data_array.shape)
 
20
                x.fill(self.low_pos)
 
21
                return x
 
22
            else:
 
23
                return array([self.low_pos])
 
24
        else:
 
25
            # Scale the data by the number of bands
 
26
            return (data_array*self.bands - self.range.low) * self._scale + self.low_pos
 
27
 
 
28
class HorizonPlot(BaseXYPlot):
 
29
 
 
30
    bands = DelegatesTo('value_mapper')
 
31
    color_mapper = Instance(ColorMapper)
 
32
 
 
33
    mirror = Bool(False)
 
34
 
 
35
    # FIXME There should be a way to automatically detect whether the data has
 
36
    # negative bands
 
37
    negative_bands = Bool(True)
 
38
 
 
39
    # Override parent traits
 
40
 
 
41
    orientation = 'h'
 
42
 
 
43
    def _color_mapper_changed(self, new):
 
44
        # change the number of steps to match the number of bands
 
45
        if not self.negative_bands:
 
46
            new.steps = self.bands+1
 
47
        else:
 
48
            new.steps = self.bands*2+1
 
49
 
 
50
    def _gather_points(self):
 
51
        """ Collects the data points that are within the bounds of the plot and
 
52
        caches them.
 
53
        """
 
54
        if self._cache_valid:
 
55
            return
 
56
 
 
57
        index = self.index.get_data()
 
58
        value = self.value.get_data()
 
59
 
 
60
        if not self.index or not self.value:
 
61
            return
 
62
 
 
63
        if len(index) == 0 or len(value) == 0 or len(index) != len(value):
 
64
            self._cached_data_pts = []
 
65
            self._cache_valid = True
 
66
            return
 
67
 
 
68
        points = transpose(array((index,value)))
 
69
        self._cached_data_pts = points
 
70
 
 
71
        self._cache_valid = True
 
72
 
 
73
    def _render(self, gc, points):
 
74
        if len(points) == 0:
 
75
            return
 
76
 
 
77
        ox, oy = self.map_screen([[0,0]])[0]
 
78
        ylow, yhigh = self.value_mapper.screen_bounds
 
79
 
 
80
        y_plus_height = yhigh - oy
 
81
 
 
82
        # Get color bands
 
83
        bands = array(self.color_mapper._get_color_bands())
 
84
 
 
85
        with gc: 
 
86
            gc.clip_to_rect(self.x, self.y, self.width, self.height)
 
87
            # draw positive bands
 
88
            inc = -1 * array([0, y_plus_height])
 
89
            if self.negative_bands: render_bands = bands[self.bands+1:]
 
90
            else: render_bands = bands[1:]
 
91
            for i, col in enumerate(render_bands):
 
92
                self._render_fill(gc, col, points+i*inc, ox, oy)
 
93
 
 
94
            # draw negative bands
 
95
            if self.negative_bands:
 
96
                if self.mirror: 
 
97
                    points[:,1] = oy - points[:,1]
 
98
                    zeroy = oy
 
99
                else: 
 
100
                    points[:,1] += y_plus_height
 
101
                    inc *= -1
 
102
                    zeroy = int(yhigh) + 2
 
103
                for i, col in enumerate(bands[self.bands-1::-1]):
 
104
                    self._render_fill(gc, col, points+i*inc, ox, zeroy)
 
105
 
 
106
            gc.set_stroke_color((.75, .75, .75))
 
107
            gc.set_line_width(2)
 
108
            gc.begin_path()
 
109
            gc.move_to(self.x, self.y)
 
110
            gc.line_to(self.x+self.width, self.y)
 
111
            gc.stroke_path()
 
112
 
 
113
    def _render_fill(self, gc, face_col, points, ox, oy):
 
114
        gc.set_fill_color(tuple(face_col))
 
115
        gc.begin_path()
 
116
        startx, starty = points[0]
 
117
        gc.move_to(startx, oy) 
 
118
        gc.line_to(startx, starty)
 
119
 
 
120
        gc.lines(points)
 
121
 
 
122
        endx, endy = points[-1]
 
123
        gc.line_to(endx, oy) 
 
124
        gc.line_to(startx, oy) 
 
125
 
 
126
        gc.close_path()
 
127
        gc.fill_path()