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

« back to all changes in this revision

Viewing changes to examples/tutorials/scipy2008/custom_overlay.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, Float, Range, HasTraits, Instance
9
 
from traitsui.api import Item, View, Group, HGroup, RangeEditor, \
10
 
                                    spring, Handler
11
 
 
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")
18
 
 
19
 
    traits_view = View(Group(
20
 
                        Item("x"), Item("y"), Item("width"), Item("height"),
21
 
                        Item("color"), orientation = "vertical"
22
 
                        ))
23
 
 
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)
29
 
        gc.fill_path()
30
 
 
31
 
    def _anytrait_changed(self):
32
 
        self.component.request_redraw()
33
 
 
34
 
class ScatterPlotHandler(Handler):
35
 
 
36
 
    def object_edit_overlay_changed(self, info):
37
 
        info.object.plot.overlays[-1].edit_traits(parent=info.ui.control)
38
 
        return
39
 
 
40
 
class ScatterPlot(HasTraits):
41
 
 
42
 
    plot = Instance(Plot)
43
 
 
44
 
    edit_overlay = Button('Edit Overlay')
45
 
 
46
 
    traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
47
 
                       HGroup(spring,
48
 
                              Item('edit_overlay', show_label=False,
49
 
                                   emphasized=True,
50
 
                                   height=50),
51
 
                              spring),
52
 
                       handler = ScatterPlotHandler,
53
 
                       width=800, height=600, resizable=True)
54
 
 
55
 
    def _plot_default(self):
56
 
        # Create the data and the PlotData object
57
 
        x = linspace(-14, 14, 100)
58
 
        y = sin(x) * x**3
59
 
        plotdata = ArrayPlotData(x = x, y = y)
60
 
        # Create a Plot and associate it with the PlotData
61
 
        plot = Plot(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))
67
 
        return plot
68
 
 
69
 
#===============================================================================
70
 
# demo object that is used by the demo.py application.
71
 
#===============================================================================
72
 
demo = ScatterPlot()
73
 
if __name__ == "__main__":
74
 
    demo.configure_traits()