File: horizon_plot.py

package info (click to toggle)
python-chaco 4.4.1-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,664 kB
  • ctags: 4,246
  • sloc: python: 25,890; ansic: 1,217; makefile: 18; sh: 5
file content (127 lines) | stat: -rw-r--r-- 3,863 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from __future__ import with_statement

from numpy import array, transpose, ndarray, empty
from traits.api import Instance, DelegatesTo, Bool, Int

from enable.api import transparent_color_trait
from chaco.color_mapper import ColorMapper
from chaco.base_xy_plot import BaseXYPlot
from chaco.linear_mapper import LinearMapper

class BandedMapper(LinearMapper):
    bands = Int(4)

    def map_screen(self, data_array):
        self._compute_scale()

        if self._null_data_range:
            if isinstance(data_array, (tuple, list, ndarray)):
                x = empty(data_array.shape)
                x.fill(self.low_pos)
                return x
            else:
                return array([self.low_pos])
        else:
            # Scale the data by the number of bands
            return (data_array*self.bands - self.range.low) * self._scale + self.low_pos

class HorizonPlot(BaseXYPlot):

    bands = DelegatesTo('value_mapper')
    color_mapper = Instance(ColorMapper)

    mirror = Bool(False)

    # FIXME There should be a way to automatically detect whether the data has
    # negative bands
    negative_bands = Bool(True)

    # Override parent traits

    orientation = 'h'

    def _color_mapper_changed(self, new):
        # change the number of steps to match the number of bands
        if not self.negative_bands:
            new.steps = self.bands+1
        else:
            new.steps = self.bands*2+1

    def _gather_points(self):
        """ Collects the data points that are within the bounds of the plot and
        caches them.
        """
        if self._cache_valid:
            return

        index = self.index.get_data()
        value = self.value.get_data()

        if not self.index or not self.value:
            return

        if len(index) == 0 or len(value) == 0 or len(index) != len(value):
            self._cached_data_pts = []
            self._cache_valid = True
            return

        points = transpose(array((index,value)))
        self._cached_data_pts = points

        self._cache_valid = True

    def _render(self, gc, points):
        if len(points) == 0:
            return

        ox, oy = self.map_screen([[0,0]])[0]
        ylow, yhigh = self.value_mapper.screen_bounds

        y_plus_height = yhigh - oy

        # Get color bands
        bands = array(self.color_mapper._get_color_bands())

        with gc: 
            gc.clip_to_rect(self.x, self.y, self.width, self.height)
            # draw positive bands
            inc = -1 * array([0, y_plus_height])
            if self.negative_bands: render_bands = bands[self.bands+1:]
            else: render_bands = bands[1:]
            for i, col in enumerate(render_bands):
                self._render_fill(gc, col, points+i*inc, ox, oy)

            # draw negative bands
            if self.negative_bands:
                if self.mirror: 
                    points[:,1] = oy - points[:,1]
                    zeroy = oy
                else: 
                    points[:,1] += y_plus_height
                    inc *= -1
                    zeroy = int(yhigh) + 2
                for i, col in enumerate(bands[self.bands-1::-1]):
                    self._render_fill(gc, col, points+i*inc, ox, zeroy)

            gc.set_stroke_color((.75, .75, .75))
            gc.set_line_width(2)
            gc.begin_path()
            gc.move_to(self.x, self.y)
            gc.line_to(self.x+self.width, self.y)
            gc.stroke_path()

    def _render_fill(self, gc, face_col, points, ox, oy):
        gc.set_fill_color(tuple(face_col))
        gc.begin_path()
        startx, starty = points[0]
        gc.move_to(startx, oy) 
        gc.line_to(startx, starty)

        gc.lines(points)

        endx, endy = points[-1]
        gc.line_to(endx, oy) 
        gc.line_to(startx, oy) 

        gc.close_path()
        gc.fill_path()