~ubuntu-branches/ubuntu/precise/python-chaco/precise

« back to all changes in this revision

Viewing changes to examples/tutorials/scipy2008/custom_overlay_dataspace.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-12-29 02:34:05 UTC
  • Revision ID: james.westby@ubuntu.com-20081229023405-x7i4kp9mdxzmdnvu
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from numpy import linspace, sin
 
3
 
 
4
from enthought.chaco.api import ArrayPlotData, Plot, AbstractOverlay
 
5
from enthought.chaco.tools.api import PanTool
 
6
from enthought.enable.component_editor import ComponentEditor
 
7
from enthought.enable.api import ColorTrait
 
8
from enthought.traits.api import CArray, Bool, Float, Range, HasTraits, Instance
 
9
from enthought.traits.ui.api import Item, View, Group, RangeEditor
 
10
 
 
11
class CustomOverlay(AbstractOverlay):
 
12
    x = Float(10, editor=RangeEditor(low=1.0, high=600, mode="slider"))
 
13
    y = Float(10, editor=RangeEditor(low=1.0, high=500, mode="slider"))
 
14
    width = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
 
15
    height = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
 
16
    color = ColorTrait("red")
 
17
    dataspace = Bool(False)
 
18
    
 
19
    _anchor = CArray
 
20
    
 
21
    traits_view = View(Group(
 
22
                        Item("x"), Item("y"), Item("width"), Item("height"),
 
23
                        Item("color"), 
 
24
                        Item("dataspace", label="Data space?"),
 
25
                        orientation = "vertical"
 
26
                        ))
 
27
    
 
28
    def overlay(self, component, gc, view_bounds=None, mode="normal"):
 
29
        if self.dataspace:
 
30
            self.x, self.y = component.map_screen(self._anchor)
 
31
        gc.set_fill_color(self.color_)
 
32
        x = self.x + component.x
 
33
        y = self.y + component.y
 
34
        gc.rect(x, y, self.width, self.height)
 
35
        gc.fill_path()
 
36
 
 
37
    def _anytrait_changed(self):
 
38
        self.component.request_redraw()
 
39
 
 
40
    def _dataspace_changed(self):
 
41
        if self.dataspace:
 
42
            # Map our current x,y point into data space
 
43
            self._anchor = self.component.map_data((self.x, self.y))
 
44
    
 
45
class ScatterPlot(HasTraits):
 
46
 
 
47
    plot = Instance(Plot)
 
48
 
 
49
    traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False), 
 
50
                       width=800, height=600, resizable=True)
 
51
 
 
52
    def _plot_default(self):
 
53
        # Create the data and the PlotData object
 
54
        x = linspace(-14, 14, 100)
 
55
        y = sin(x) * x**3
 
56
        plotdata = ArrayPlotData(x = x, y = y)
 
57
        # Create a Plot and associate it with the PlotData
 
58
        plot = Plot(plotdata)
 
59
        # Create a scatter plot in the Plot
 
60
        plot.plot(("x", "y"), type="scatter", color="blue")
 
61
        plot.tools.append(PanTool(plot))
 
62
        # Add our custom tool to the plot
 
63
        plot.overlays.append(CustomOverlay(plot))
 
64
        return plot
 
65
 
 
66
if __name__ == "__main__":
 
67
    # Create the main plot and bring it up via calling edit_traits()
 
68
    plot = ScatterPlot()
 
69
    plot.edit_traits(kind="live")
 
70
    # Get a handle to the overlay and edit the traits on it as well
 
71
    overlay = plot.plot.overlays[-1]
 
72
    overlay.configure_traits()