Skip to content

Commit 7f74888

Browse files
committed
2 tests cases for PolygonPlot list capability
1) polygon_move.py to show that the new capability does not break the old way of working (both for plot and hottest) 2) polygon_move_list.py to show that the new capability allows to use hittest on the plotted list of polygons
1 parent 5190ef3 commit 7f74888

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed

polygon_move.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
""" Based on <<Polygon plot with drag-move>>.
2+
3+
Used as a simple example to show that the modifications made to polygon_plot.py
4+
in order to allow a list of polygon to be plotted at once do not change the
5+
original behavior of hittest when PolygonPlot is used as originally.
6+
7+
TEST: Drag the polygons using left click
8+
"""
9+
#-------------------------------------------------------------------------------
10+
# Force Qt4 since issue with wx
11+
from traits.etsconfig.api import ETSConfig
12+
ETSConfig.toolkit = 'qt4'
13+
14+
# Major library imports
15+
from numpy import transpose
16+
17+
# Enthought library imports
18+
from enable.api import Component, ComponentEditor
19+
from traits.api import HasTraits, Instance, Enum, CArray
20+
from traitsui.api import Item, Group, View
21+
22+
# Chaco imports
23+
from chaco.api import ArrayPlotData, Plot
24+
from chaco.base import n_gon
25+
from chaco.tools.api import PanTool, ZoomTool, DragTool
26+
27+
class DataspaceMoveTool(DragTool):
28+
"""
29+
Modifies the data values of a plot. Only works on instances
30+
of BaseXYPlot or its subclasses
31+
"""
32+
33+
event_state = Enum("normal", "dragging")
34+
_prev_pt = CArray
35+
36+
def is_draggable(self, x, y):
37+
return self.component.hittest((x,y))
38+
39+
def drag_start(self, event):
40+
data_pt = self.component.map_data((event.x, event.y), all_values=True)
41+
self._prev_pt = data_pt
42+
event.handled = True
43+
44+
def dragging(self, event):
45+
plot = self.component
46+
cur_pt = plot.map_data((event.x, event.y), all_values=True)
47+
dx = cur_pt[0] - self._prev_pt[0]
48+
dy = cur_pt[1] - self._prev_pt[1]
49+
index = plot.index.get_data() + dx
50+
value = plot.value.get_data() + dy
51+
plot.index.set_data(index, sort_order=plot.index.sort_order)
52+
plot.value.set_data(value, sort_order=plot.value.sort_order)
53+
self._prev_pt = cur_pt
54+
event.handled = True
55+
plot.request_redraw()
56+
57+
58+
#===============================================================================
59+
# # Create the Chaco plot.
60+
#===============================================================================
61+
def _create_plot_component():
62+
63+
# Use n_gon to compute center locations for our polygons
64+
points = n_gon(center=(0,0), r=3, nsides=4)
65+
66+
# Choose some colors for our polygons
67+
colors = {3:0xaabbcc, 4:'orange', 5:'yellow', 6:'lightgreen'}
68+
69+
# Create a PlotData object to store the polygon data
70+
pd = ArrayPlotData()
71+
72+
# Create a Polygon Plot to draw the regular polygons
73+
polyplot = Plot(pd)
74+
75+
# Store path data for each polygon, and plot
76+
nsides = 3
77+
for p in points:
78+
npoints = n_gon(center=p, r=2, nsides=nsides)
79+
nxarray, nyarray = transpose(npoints)
80+
pd.set_data("x" + str(nsides), nxarray)
81+
pd.set_data("y" + str(nsides), nyarray)
82+
plot = polyplot.plot(("x"+str(nsides), "y"+str(nsides)), type="polygon",
83+
face_color=colors[nsides], hittest_type="poly")[0]
84+
85+
plot.tools.append(DataspaceMoveTool(plot, drag_button="left"))
86+
nsides = nsides + 1
87+
88+
# Tweak some of the plot properties
89+
polyplot.padding = 50
90+
polyplot.title = "Polygon Plot"
91+
polyplot.x_axis.mapper.range.set(low=-10, high=10)
92+
polyplot.y_axis.mapper.range.set(low=-10, high=10)
93+
94+
return polyplot
95+
96+
#===============================================================================
97+
# Attributes to use for the plot view.
98+
size=(800,800)
99+
title="Polygon Plot"
100+
101+
#===============================================================================
102+
# # Demo class that is used by the demo.py application.
103+
#===============================================================================
104+
class Demo(HasTraits):
105+
plot = Instance(Component)
106+
107+
traits_view = View(
108+
Group(
109+
Item('plot', editor=ComponentEditor(size=size),
110+
show_label=False),
111+
orientation = "vertical"),
112+
resizable=True, title=title
113+
)
114+
115+
def _plot_default(self):
116+
return _create_plot_component()
117+
118+
demo = Demo()
119+
120+
if __name__ == "__main__":
121+
demo.configure_traits()
122+
123+
#--EOF---

polygon_move_list.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
""" Based on <<Polygon plot with drag-move>>.
2+
3+
Used as a simple example to show that the modifications made to polygon_plot.py
4+
in order to allow a list of polygon to be plotted at once also allows each list
5+
to be consider as one "list of polygon" when using hittest.
6+
7+
TEST: Drag the "list of polygons" using left click
8+
"""
9+
#-------------------------------------------------------------------------------
10+
# Force Qt4 since issue
11+
from traits.etsconfig.api import ETSConfig
12+
ETSConfig.toolkit = 'qt4'
13+
14+
# Major library imports
15+
from numpy import transpose, nan
16+
17+
# Enthought library imports
18+
from enable.api import Component, ComponentEditor
19+
from traits.api import HasTraits, Instance, Enum, CArray
20+
from traitsui.api import Item, Group, View
21+
22+
# Chaco imports
23+
from chaco.api import ArrayPlotData, Plot
24+
from chaco.base import n_gon
25+
from chaco.tools.api import DragTool
26+
27+
class DataspaceMoveTool(DragTool):
28+
"""
29+
Modifies the data values of a plot. Only works on instances
30+
of BaseXYPlot or its subclasses
31+
"""
32+
33+
event_state = Enum("normal", "dragging")
34+
_prev_pt = CArray
35+
36+
def is_draggable(self, x, y):
37+
return self.component.hittest((x,y))
38+
39+
def drag_start(self, event):
40+
data_pt = self.component.map_data((event.x, event.y), all_values=True)
41+
self._prev_pt = data_pt
42+
event.handled = True
43+
44+
def dragging(self, event):
45+
plot = self.component
46+
cur_pt = plot.map_data((event.x, event.y), all_values=True)
47+
dx = cur_pt[0] - self._prev_pt[0]
48+
dy = cur_pt[1] - self._prev_pt[1]
49+
index = plot.index.get_data() + dx
50+
value = plot.value.get_data() + dy
51+
plot.index.set_data(index, sort_order=plot.index.sort_order)
52+
plot.value.set_data(value, sort_order=plot.value.sort_order)
53+
self._prev_pt = cur_pt
54+
event.handled = True
55+
plot.request_redraw()
56+
57+
58+
#===============================================================================
59+
# # Create the Chaco plot.
60+
#===============================================================================
61+
def _create_plot_component():
62+
63+
# Use n_gon to compute center locations of the "global" polygons
64+
points = n_gon(center=(0,0), r=8, nsides=4)
65+
66+
# Choose some colors for our polygons
67+
colors = {0:0xaabbcc, 1:'orange', 2:'yellow', 3:'lightgreen'}
68+
69+
# Create a PlotData object to store the polygon data
70+
pd = ArrayPlotData()
71+
72+
# Create a Polygon Plot to draw the regular polygons
73+
polyplot = Plot(pd)
74+
75+
# Store path data for each polygon, and plot
76+
for n, ctr in enumerate(points):
77+
78+
nsides = 3
79+
nodes_x = []
80+
nodes_y = []
81+
82+
# Use n_gon again to compute center locations of the "local" polygons
83+
polyg_pnts = n_gon(center=ctr, r=3, nsides=4)
84+
85+
for p in polyg_pnts:
86+
npoints = n_gon(center=p, r=2, nsides=nsides)
87+
nxarray, nyarray = transpose(npoints)
88+
nodes_x = nodes_x + list(nxarray) + [nan] # Updates list of polygons
89+
nodes_y = nodes_y + list(nyarray) + [nan] # Updates list of polygons
90+
nsides = nsides + 1
91+
pd.set_data("x"+str(n), nodes_x) # The list of 6 local polygons
92+
pd.set_data("y"+str(n), nodes_y) # The list of 6 local polygons
93+
94+
plot = polyplot.plot(("x"+str(n), "y"+str(n)), type="polygon",
95+
face_color=colors[n], hittest_type="poly")[0]
96+
97+
plot.tools.append(DataspaceMoveTool(plot, drag_button="left"))
98+
99+
# Tweak some of the plot properties
100+
polyplot.padding = 50
101+
polyplot.title = "Polygon Plot"
102+
polyplot.x_axis.mapper.range.set(low=-20, high=20)
103+
polyplot.y_axis.mapper.range.set(low=-20, high=20)
104+
105+
return polyplot
106+
107+
#===============================================================================
108+
# Attributes to use for the plot view.
109+
size=(800,800)
110+
title="Polygon Plot"
111+
112+
#===============================================================================
113+
# # Demo class that is used by the demo.py application.
114+
#===============================================================================
115+
class Demo(HasTraits):
116+
plot = Instance(Component)
117+
118+
def _plot_default(self):
119+
return _create_plot_component()
120+
121+
traits_view = View(
122+
Group(
123+
Item('plot', editor=ComponentEditor(size=size),
124+
show_label=False),
125+
orientation = "vertical"),
126+
resizable=True, title=title
127+
)
128+
129+
demo = Demo()
130+
131+
if __name__ == "__main__":
132+
demo.configure_traits()
133+
134+
#--EOF---

0 commit comments

Comments
 (0)