2
from numpy import linspace, sin
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
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)
24
traits_view = View(Group(
25
Item("x"), Item("y"), Item("width"), Item("height"),
27
Item("dataspace", label="Data space?"),
28
orientation = "vertical"
31
def overlay(self, component, gc, view_bounds=None, mode="normal"):
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)
40
def _get_position(self):
41
return [self.x, self.y]
43
def _set_position(self, newpos):
44
self.x, self.y = newpos
46
def _anytrait_changed(self):
47
self.component.request_redraw()
49
def _dataspace_changed(self):
51
# Map our current x,y point into data space
52
self._anchor = self.component.map_data((self.x, self.y))
54
class ScatterPlotHandler(Handler):
56
def object_edit_overlay_changed(self, info):
57
info.object.plot.overlays[-1].edit_traits(parent=info.ui.control)
60
class ScatterPlot(HasTraits):
64
edit_overlay = Button('Edit Overlay')
66
traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
68
Item('edit_overlay', show_label=False,
72
handler = ScatterPlotHandler,
73
width=800, height=600, resizable=True)
75
def _plot_default(self):
76
# Create the data and the PlotData object
77
x = linspace(-14, 14, 100)
79
plotdata = ArrayPlotData(x = x, y = y)
80
# Create a Plot and associate it with the 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
89
overlay.tools.append(MoveTool(overlay, drag_button="right"))
90
plot.overlays.append(overlay)
93
#===============================================================================
94
# demo object that is used by the demo.py application.
95
#===============================================================================
97
if __name__ == "__main__":
98
demo.configure_traits()