2
from numpy import linspace, sin
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,\
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)
23
traits_view = View(Group(
24
Item("x"), Item("y"), Item("width"), Item("height"),
26
Item("dataspace", label="Data space?"),
27
orientation = "vertical"
30
def overlay(self, component, gc, view_bounds=None, mode="normal"):
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)
39
def _anytrait_changed(self):
40
self.component.request_redraw()
42
def _dataspace_changed(self):
44
# Map our current x,y point into data space
45
self._anchor = self.component.map_data((self.x, self.y))
47
class ScatterPlotHandler(Handler):
49
def object_edit_overlay_changed(self, info):
50
info.object.plot.overlays[-1].edit_traits(parent=info.ui.control)
53
class ScatterPlot(HasTraits):
57
edit_overlay = Button('Edit Overlay')
59
traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
61
Item('edit_overlay', show_label=False,
65
handler = ScatterPlotHandler,
66
width=800, height=600, resizable=True)
68
def _plot_default(self):
69
# Create the data and the PlotData object
70
x = linspace(-14, 14, 100)
72
plotdata = ArrayPlotData(x = x, y = y)
73
# Create a Plot and associate it with the 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))
82
#===============================================================================
83
# demo object that is used by the demo.py application.
84
#===============================================================================
86
if __name__ == "__main__":
87
demo.configure_traits()