source: sasview/plottools/src/danse/common/plottools/plottable_interactor.py @ 6c4130a

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6c4130a was 6c4130a, checked in by Jae Cho <jhjcho@…>, 12 years ago

Added more curve styles and fixed minor error on deleting a data from plot

  • Property mode set to 100644
File size: 9.1 KB
Line 
1"""
2    This module allows more interaction with the plot
3"""
4import plottables
5from BaseInteractor import _BaseInteractor
6
7class PointInteractor(_BaseInteractor):
8    """
9    """
10    def __init__(self, base, axes, color='black', zorder=3, id=''):
11        """
12        """
13        _BaseInteractor.__init__(self, base, axes, color=color)
14        self.zorder = zorder
15        self.id = id
16        self.color = color
17        self.colorlist = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
18        self.symbollist = ['o', 'x', '^', 'v', '<', '>',
19                           '+', 's', 'd', 'D', 'h', 'H', 'p', '-', '--',
20                           'vline', 'step']
21        self.markersize = None
22        self.marker = None
23        self.marker2 = None
24        self._button_down = False
25        self._context_menu = False
26        self._dragged = False
27        self.connect_markers([self.axes])
28       
29    def _color(self, c):
30        """Return a particular colour"""
31        return self.colorlist[c % len(self.colorlist)]
32   
33    def _symbol(self, s):
34        """Return a particular symbol"""
35        return self.symbollist[s % len(self.symbollist)]
36
37    def points(self, x, y, dx=None, dy=None, color=0, symbol=0, markersize=5,
38               label=None, hide_error=False):
39        """
40        """
41        #Draw curve
42        if self._symbol(symbol) == '-' or self._symbol(symbol) == '--':
43            l_width = markersize * 0.4
44            return self.curve(x=x, y=y, color=color, symbol=symbol, 
45                              label=label, width=l_width)
46            #return
47        if self._symbol(symbol) == 'vline':
48            l_width = markersize * 0.4
49            return self.vline(x=x, y=y, color=color, 
50                                label=label, width=l_width)
51        if self._symbol(symbol) == 'step':
52            l_width = markersize * 0.4
53            return self.step(x=x, y=y, color=color, 
54                                label=label, width=l_width)
55        if not self.marker == None:
56            self.base.connect.clear([self.marker])
57        self.color = self._color(color)
58        if self.markersize != None:
59            markersize = self.markersize
60        # Convert tuple (lo,hi) to array [(x-lo),(hi-x)]
61        if dx != None and type(dx) == type(()):
62            dx = nx.vstack((x-dx[0], dx[1]-x)).transpose()
63        if dy != None and type(dy) == type(()):
64            dy = nx.vstack((y-dy[0], dy[1]-y)).transpose()
65        zorder = self.zorder
66        if dx == None and dy == None:
67            #zorder = 1
68            self.marker = self.axes.plot(x, y, color=self.color,
69                                         marker=self._symbol(symbol),
70                                         markersize=markersize,
71                                         linestyle='', label=label,
72                                         zorder=zorder)[0]
73        else:
74           
75            if hide_error:
76                #zorder = 1
77                self.marker = self.axes.plot(x, y, color=self.color,
78                                             marker=self._symbol(symbol),
79                                             markersize=markersize,
80                                             linestyle='', label=label,
81                                             zorder=zorder)[0]
82            else:
83                #zorder = 2
84                self.marker = self.axes.errorbar(x, y, yerr=dy,
85                                                 xerr=None,
86                                                 ecolor=self.color,
87                                                 color=self.color,
88                                                 capsize=2,
89                                                 linestyle='',
90                                                 barsabove=False,
91                                                 marker=self._symbol(symbol),
92                                                 markersize=markersize,
93                                                 lolims=False, uplims=False,
94                                                 xlolims=False, xuplims=False,
95                                                 label=label,
96                                                 zorder=zorder)[0]
97           
98        self.connect_markers([self.marker])
99        self.update()
100       
101    def curve(self, x, y, dy=None, color=0, symbol=0, label=None, width=2.0):
102        """
103        """
104        if not self.marker == None:
105            self.base.connect.clear([self.marker])
106        self.color = self._color(color)
107        self.marker = self.axes.plot(x, y, color=self.color, lw=width,
108                                     marker='', linestyle=self._symbol(symbol),
109                                     label=label, zorder=self.zorder)[0]
110           
111        self.connect_markers([self.marker])
112        self.update()
113
114       
115    def vline(self, x, y, dy=None, color=0, symbol=0, label=None, width=2.0):
116        """
117        """
118        if not self.marker == None:
119            self.base.connect.clear([self.marker])
120        self.color = self._color(color)
121        if min(y) < 0:
122            y_min = 0.0
123        else:
124            y_min = min(y)*9/10
125        self.marker = self.axes.vlines(x=x, ymin=y_min, ymax=y, 
126                                      color=self.color, 
127                                      linestyle='-', label=label,
128                                      lw=width, zorder=self.zorder)   
129        self.connect_markers([self.marker])
130        self.update()
131
132    def step(self, x, y, dy=None, color=0, symbol=0, label=None, width=2.0):
133        """
134        """
135        if not self.marker == None:
136            self.base.connect.clear([self.marker])
137        self.color = self._color(color)
138        if self.markersize != None:
139            markersize = self.markersize
140           
141        self.marker = self.axes.step(x, y, color=self.color,
142                                         marker='',
143                                         linestyle='-', label=label,
144                                         lw=width, zorder=self.zorder)[0]
145        self.connect_markers([self.marker])
146        self.update()
147               
148    def connect_markers(self, markers):
149        """
150        Connect markers to callbacks
151        """
152        for h in markers:
153            connect = self.base.connect
154            connect('enter', h, self._on_enter)
155            connect('leave', h, self._on_leave)
156            connect('click', h, self._on_click)
157            connect('release', h, self._on_release)
158            connect('key', h, self.onKey)
159
160    def clear(self):
161        print "plottable_interactor.clear()"
162       
163    def _on_click(self, evt):
164        """
165        Called when a mouse button is clicked
166        from within the boundaries of an artist.
167        """
168        if self._context_menu == True:
169            self._context_menu = False
170            evt.artist = self.marker
171            self._on_leave(evt)
172
173    def _on_release(self, evt):
174        """
175        Called when a mouse button is released
176        within the boundaries of an artist
177        """
178        # Check to see whether we are about to pop
179        # the context menu up
180        if evt.button == 3:
181            self._context_menu = True
182       
183    def _on_enter(self, evt):
184        """
185        Called when we are entering the boundaries
186        of an artist.
187        """
188        if not evt.artist.__class__.__name__ == "AxesSubplot":
189            self.base.plottable_selected(self.id)
190           
191            if evt.artist.get_color() == 'y':
192                try:
193                    evt.artist.set_color('b')
194                except:
195                    evt.artist.set_color_cycle('b')
196                if hasattr(evt.artist, "set_facecolor"):
197                    evt.artist.set_facecolor('b')
198                if hasattr(evt.artist, "set_edgecolor"):
199                    evt.artist.set_edgecolor('b')
200            else:
201                try:
202                    evt.artist.set_color('y')
203                except:
204                    evt.artist.set_color_cycle('y')
205                if hasattr(evt.artist, "set_facecolor"):
206                    evt.artist.set_facecolor('y')
207                if hasattr(evt.artist, "set_edgecolor"):
208                    evt.artist.set_edgecolor('y')
209
210            self.axes.figure.canvas.draw_idle()
211       
212    def _on_leave(self, evt):
213        """
214        Called when we are leaving the boundaries
215        of an artist.
216        """
217        if not evt.artist.__class__.__name__ == "AxesSubplot":
218            if self._context_menu == False:
219                self.base.plottable_selected(None)
220                try:
221                    evt.artist.set_color(self.color)
222                except:
223                    evt.artist.set_color_cycle(self.color)
224                if hasattr(evt.artist, "set_facecolor"):
225                    evt.artist.set_facecolor(self.color)
226                if hasattr(evt.artist, "set_edgecolor"):
227                    evt.artist.set_edgecolor(self.color)
228                self.axes.figure.canvas.draw_idle()
229   
230    def update(self):
231        """
232        Update
233        """
234        pass
Note: See TracBrowser for help on using the repository browser.