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, Float, Range, HasTraits, Instance
9
from traitsui.api import Item, View, Group, HGroup, RangeEditor, \
12
class CustomOverlay(AbstractOverlay):
13
x = Float(10, editor=RangeEditor(low=1.0, high=600, mode="slider"))
14
y = Float(10, editor=RangeEditor(low=1.0, high=500, mode="slider"))
15
width = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
16
height = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
17
color = ColorTrait("red")
19
traits_view = View(Group(
20
Item("x"), Item("y"), Item("width"), Item("height"),
21
Item("color"), orientation = "vertical"
24
def overlay(self, component, gc, view_bounds=None, mode="normal"):
25
gc.set_fill_color(self.color_)
26
x = self.x + component.x
27
y = self.y + component.y
28
gc.rect(x, y, self.width, self.height)
31
def _anytrait_changed(self):
32
self.component.request_redraw()
34
class ScatterPlotHandler(Handler):
36
def object_edit_overlay_changed(self, info):
37
info.object.plot.overlays[-1].edit_traits(parent=info.ui.control)
40
class ScatterPlot(HasTraits):
44
edit_overlay = Button('Edit Overlay')
46
traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
48
Item('edit_overlay', show_label=False,
52
handler = ScatterPlotHandler,
53
width=800, height=600, resizable=True)
55
def _plot_default(self):
56
# Create the data and the PlotData object
57
x = linspace(-14, 14, 100)
59
plotdata = ArrayPlotData(x = x, y = y)
60
# Create a Plot and associate it with the PlotData
62
# Create a scatter plot in the Plot
63
plot.plot(("x", "y"), type="scatter", color="blue")
64
plot.tools.append(PanTool(plot))
65
# Add our custom tool to the plot
66
plot.overlays.append(CustomOverlay(plot))
69
#===============================================================================
70
# demo object that is used by the demo.py application.
71
#===============================================================================
73
if __name__ == "__main__":
74
demo.configure_traits()