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

« back to all changes in this revision

Viewing changes to examples/tutorials/scipy2008/custom_overlay_dataspace.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
 
 
2
 
from numpy import linspace, sin
3
 
 
4
 
from chaco.api import ArrayPlotData, Plot, AbstractOverlay
5
 
from chaco.tools.api import PanTool
6
 
from enable.component_editor import ComponentEditor
7
 
from enable.api import ColorTrait
8
 
from traits.api import Button, CArray, Bool, Float, Range, HasTraits,\
9
 
                                 Instance
10
 
from traitsui.api import Item, View, Group, RangeEditor, \
11
 
                                    HGroup, Handler, spring
12
 
 
13
 
class CustomOverlay(AbstractOverlay):
14
 
    x = Float(10, editor=RangeEditor(low=1.0, high=600, mode="slider"))
15
 
    y = Float(10, editor=RangeEditor(low=1.0, high=500, mode="slider"))
16
 
    width = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
17
 
    height = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
18
 
    color = ColorTrait("red")
19
 
    dataspace = Bool(False)
20
 
 
21
 
    _anchor = CArray
22
 
 
23
 
    traits_view = View(Group(
24
 
                        Item("x"), Item("y"), Item("width"), Item("height"),
25
 
                        Item("color"),
26
 
                        Item("dataspace", label="Data space?"),
27
 
                        orientation = "vertical"
28
 
                        ))
29
 
 
30
 
    def overlay(self, component, gc, view_bounds=None, mode="normal"):
31
 
        if self.dataspace:
32
 
            self.x, self.y = component.map_screen(self._anchor)
33
 
        gc.set_fill_color(self.color_)
34
 
        x = self.x + component.x
35
 
        y = self.y + component.y
36
 
        gc.rect(x, y, self.width, self.height)
37
 
        gc.fill_path()
38
 
 
39
 
    def _anytrait_changed(self):
40
 
        self.component.request_redraw()
41
 
 
42
 
    def _dataspace_changed(self):
43
 
        if self.dataspace:
44
 
            # Map our current x,y point into data space
45
 
            self._anchor = self.component.map_data((self.x, self.y))
46
 
 
47
 
class ScatterPlotHandler(Handler):
48
 
 
49
 
    def object_edit_overlay_changed(self, info):
50
 
        info.object.plot.overlays[-1].edit_traits(parent=info.ui.control)
51
 
        return
52
 
 
53
 
class ScatterPlot(HasTraits):
54
 
 
55
 
    plot = Instance(Plot)
56
 
 
57
 
    edit_overlay = Button('Edit Overlay')
58
 
 
59
 
    traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
60
 
                       HGroup(spring,
61
 
                              Item('edit_overlay', show_label=False,
62
 
                                   emphasized=True,
63
 
                                   height=50),
64
 
                              spring),
65
 
                       handler = ScatterPlotHandler,
66
 
                       width=800, height=600, resizable=True)
67
 
 
68
 
    def _plot_default(self):
69
 
        # Create the data and the PlotData object
70
 
        x = linspace(-14, 14, 100)
71
 
        y = sin(x) * x**3
72
 
        plotdata = ArrayPlotData(x = x, y = y)
73
 
        # Create a Plot and associate it with the PlotData
74
 
        plot = Plot(plotdata)
75
 
        # Create a scatter plot in the Plot
76
 
        plot.plot(("x", "y"), type="scatter", color="blue")
77
 
        plot.tools.append(PanTool(plot))
78
 
        # Add our custom tool to the plot
79
 
        plot.overlays.append(CustomOverlay(plot))
80
 
        return plot
81
 
 
82
 
#===============================================================================
83
 
# demo object that is used by the demo.py application.
84
 
#===============================================================================
85
 
demo = ScatterPlot()
86
 
if __name__ == "__main__":
87
 
    demo.configure_traits()