|
| 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