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

« back to all changes in this revision

Viewing changes to examples/demo/basic/polygon_move.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
 
#!/usr/bin/env python
2
 
"""
3
 
Shares same basic interactions as polygon_plot.py, but adds a new one:
4
 
 - Right click and drag to move a polygon around.
5
 
"""
6
 
 
7
 
# Major library imports
8
 
from numpy import transpose
9
 
 
10
 
# Enthought library imports
11
 
from enable.api import Component, ComponentEditor
12
 
from traits.api import HasTraits, Instance, Enum, CArray
13
 
from traitsui.api import Item, Group, View
14
 
 
15
 
# Chaco imports
16
 
from chaco.api import ArrayPlotData, Plot
17
 
from chaco.base import n_gon
18
 
from chaco.tools.api import PanTool, ZoomTool, DragTool
19
 
 
20
 
class DataspaceMoveTool(DragTool):
21
 
    """
22
 
    Modifies the data values of a plot.  Only works on instances
23
 
    of BaseXYPlot or its subclasses
24
 
    """
25
 
 
26
 
    event_state = Enum("normal", "dragging")
27
 
    _prev_pt = CArray
28
 
 
29
 
    def is_draggable(self, x, y):
30
 
        return self.component.hittest((x,y))
31
 
 
32
 
    def drag_start(self, event):
33
 
        data_pt = self.component.map_data((event.x, event.y), all_values=True)
34
 
        self._prev_pt = data_pt
35
 
        event.handled = True
36
 
 
37
 
    def dragging(self, event):
38
 
        plot = self.component
39
 
        cur_pt = plot.map_data((event.x, event.y), all_values=True)
40
 
        dx = cur_pt[0] - self._prev_pt[0]
41
 
        dy = cur_pt[1] - self._prev_pt[1]
42
 
        index = plot.index.get_data() + dx
43
 
        value = plot.value.get_data() + dy
44
 
        plot.index.set_data(index, sort_order=plot.index.sort_order)
45
 
        plot.value.set_data(value, sort_order=plot.value.sort_order)
46
 
        self._prev_pt = cur_pt
47
 
        event.handled = True
48
 
        plot.request_redraw()
49
 
 
50
 
 
51
 
#===============================================================================
52
 
# # Create the Chaco plot.
53
 
#===============================================================================
54
 
def _create_plot_component():
55
 
 
56
 
    # Use n_gon to compute center locations for our polygons
57
 
    points = n_gon(center=(0,0), r=4, nsides=8)
58
 
 
59
 
    # Choose some colors for our polygons
60
 
    colors = {3:0xaabbcc,   4:'orange', 5:'yellow',    6:'lightgreen',
61
 
              7:'green', 8:'blue',   9:'lavender', 10:'purple'}
62
 
 
63
 
        # Create a PlotData object to store the polygon data
64
 
    pd = ArrayPlotData()
65
 
 
66
 
    # Create a Polygon Plot to draw the regular polygons
67
 
    polyplot = Plot(pd)
68
 
 
69
 
    # Store path data for each polygon, and plot
70
 
    nsides = 3
71
 
    for p in points:
72
 
        npoints = n_gon(center=p, r=2, nsides=nsides)
73
 
        nxarray, nyarray = transpose(npoints)
74
 
        pd.set_data("x" + str(nsides), nxarray)
75
 
        pd.set_data("y" + str(nsides), nyarray)
76
 
        plot = polyplot.plot(("x"+str(nsides), "y"+str(nsides)),
77
 
                      type="polygon",
78
 
                      face_color=colors[nsides],
79
 
                      hittest_type="poly")[0]
80
 
        plot.tools.append(DataspaceMoveTool(plot, drag_button="right"))
81
 
        nsides = nsides + 1
82
 
 
83
 
    # Tweak some of the plot properties
84
 
    polyplot.padding = 50
85
 
    polyplot.title = "Polygon Plot"
86
 
 
87
 
    # Attach some tools to the plot
88
 
    polyplot.tools.append(PanTool(polyplot))
89
 
    zoom = ZoomTool(polyplot, tool_mode="box", always_on=False)
90
 
    polyplot.overlays.append(zoom)
91
 
 
92
 
    return polyplot
93
 
 
94
 
#===============================================================================
95
 
# Attributes to use for the plot view.
96
 
size=(800,800)
97
 
title="Polygon Plot"
98
 
 
99
 
#===============================================================================
100
 
# # Demo class that is used by the demo.py application.
101
 
#===============================================================================
102
 
class Demo(HasTraits):
103
 
    plot = Instance(Component)
104
 
 
105
 
    traits_view = View(
106
 
                    Group(
107
 
                        Item('plot', editor=ComponentEditor(size=size),
108
 
                             show_label=False),
109
 
                        orientation = "vertical"),
110
 
                    resizable=True, title=title
111
 
                    )
112
 
 
113
 
    def _plot_default(self):
114
 
         return _create_plot_component()
115
 
 
116
 
demo = Demo()
117
 
 
118
 
if __name__ == "__main__":
119
 
    demo.configure_traits()
120
 
 
121
 
#--EOF---