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

« back to all changes in this revision

Viewing changes to examples/tutorials/scipy2008/custom_overlay_movetool.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, MoveTool
6
 
from enable.component_editor import ComponentEditor
7
 
from enable.api import ColorTrait
8
 
from traits.api import Button, CArray, Bool, Float, Range, \
9
 
                                 HasTraits, Instance, Property
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
 
    position = Property
21
 
 
22
 
    _anchor = CArray
23
 
 
24
 
    traits_view = View(Group(
25
 
                        Item("x"), Item("y"), Item("width"), Item("height"),
26
 
                        Item("color"),
27
 
                        Item("dataspace", label="Data space?"),
28
 
                        orientation = "vertical"
29
 
                        ))
30
 
 
31
 
    def overlay(self, component, gc, view_bounds=None, mode="normal"):
32
 
        if self.dataspace:
33
 
            self.x, self.y = component.map_screen(self._anchor)
34
 
        gc.set_fill_color(self.color_)
35
 
        x = self.x + component.x
36
 
        y = self.y + component.y
37
 
        gc.rect(x, y, self.width, self.height)
38
 
        gc.fill_path()
39
 
 
40
 
    def _get_position(self):
41
 
        return [self.x, self.y]
42
 
 
43
 
    def _set_position(self, newpos):
44
 
        self.x, self.y = newpos
45
 
 
46
 
    def _anytrait_changed(self):
47
 
        self.component.request_redraw()
48
 
 
49
 
    def _dataspace_changed(self):
50
 
        if self.dataspace:
51
 
            # Map our current x,y point into data space
52
 
            self._anchor = self.component.map_data((self.x, self.y))
53
 
 
54
 
class ScatterPlotHandler(Handler):
55
 
 
56
 
    def object_edit_overlay_changed(self, info):
57
 
        info.object.plot.overlays[-1].edit_traits(parent=info.ui.control)
58
 
        return
59
 
 
60
 
class ScatterPlot(HasTraits):
61
 
 
62
 
    plot = Instance(Plot)
63
 
 
64
 
    edit_overlay = Button('Edit Overlay')
65
 
 
66
 
    traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
67
 
                       HGroup(spring,
68
 
                              Item('edit_overlay', show_label=False,
69
 
                                   emphasized=True,
70
 
                                   height=50),
71
 
                              spring),
72
 
                       handler = ScatterPlotHandler,
73
 
                       width=800, height=600, resizable=True)
74
 
 
75
 
    def _plot_default(self):
76
 
        # Create the data and the PlotData object
77
 
        x = linspace(-14, 14, 100)
78
 
        y = sin(x) * x**3
79
 
        plotdata = ArrayPlotData(x = x, y = y)
80
 
        # Create a Plot and associate it with the PlotData
81
 
        plot = Plot(plotdata)
82
 
        # Create a scatter plot in the Plot
83
 
        plot.plot(("x", "y"), type="scatter", color="blue")
84
 
        plot.tools.append(PanTool(plot))
85
 
        # Add our custom overlay to the plot
86
 
        overlay = CustomOverlay(plot)
87
 
        # Add the MoveTool to the overlay so it can be dragged around usin gthe
88
 
        # right mouse button.
89
 
        overlay.tools.append(MoveTool(overlay, drag_button="right"))
90
 
        plot.overlays.append(overlay)
91
 
        return plot
92
 
 
93
 
#===============================================================================
94
 
# demo object that is used by the demo.py application.
95
 
#===============================================================================
96
 
demo = ScatterPlot()
97
 
if __name__ == "__main__":
98
 
    demo.configure_traits()