1212
1313
1414
15+
1516# tools = LoggerTool()
1617# logger = tools.setupLogger(__name__, __name__ + '.log', 'w', logging.DEBUG)
1718logger = logging .getLogger ('main' )
1819
20+ import matplotlib .dates as mdates
21+ from matplotlib .lines import Line2D
22+ from matplotlib .text import Text
23+ from matplotlib import dates
24+
1925
2026def bind (actor ,event ,action ,id = None ):
2127 if id is not None :
@@ -74,6 +80,20 @@ def _init_toolbar(self):
7480 text , tooltip_text )
7581 bind (self , wx .EVT_TOOL , getattr (self , callback ), id = self .wx_ids [text ])
7682
83+
84+ #init hover tooltip
85+
86+ # create a long tooltip with newline to get around wx bug (in v2.6.3.3)
87+ # where newlines aren't recognized on subsequent self.tooltip.SetTip() calls
88+ self .tooltip = ToolTip (tip = 'tip with a long %s line and a newline\n ' )
89+ self .canvas .SetToolTip (self .tooltip )
90+ # self.tooltip.Enable(False)
91+ self .tooltip .SetDelay (0 )
92+
93+ self .pointPick = self .canvas .mpl_connect ('pick_event' , self ._onPick )
94+
95+
96+
7797 self .Realize ()
7898
7999
@@ -141,11 +161,17 @@ def editSeries(self, xys, edit):
141161 # enable select button
142162 self .xys = xys
143163 self .editCurve = edit
164+ self .pointPick = self .canvas .mpl_connect ('pick_event' , self ._onPick )
144165 self .select_tool .Enable (True )
145166 self .zoom_to_data .Enable (True )
146167 self .Realize ()
147168
148169 def stopEdit (self ):
170+ try :
171+ self .canvas .mpl_disconnect (self .pointPick )
172+ self .pointPick = None
173+ except AttributeError as e :
174+ logger .error (e )
149175
150176 self .canvas .mpl_disconnect (self .lassoAction )
151177 self .xys = None
@@ -252,6 +278,63 @@ def on_toggle_zoom_data_tool(self, event):
252278 self .canvas .draw ()
253279
254280
281+
282+ def _onMotion (self , event ):
283+ """
284+
285+ :type event: matplotlib.backend_bases.MouseEvent
286+ :return:
287+ """
288+ try :
289+ if event .xdata and event .ydata :
290+ xValue = dates .num2date (event .xdata ).replace (tzinfo = None )
291+ #self.toolbar.msg.SetLabelText("X= %s, Y= %.2f" % (xValue.strftime("%Y-%m-%d %H:%M:%S"), event.ydata))
292+ #self.toolbar.msg.SetLabelText("X= %s, Y= %.2f" % (xValue.strftime("%b %d, %Y %H:%M:%S"), event.ydata))
293+ self .toolbar .msg .SetLabelText ("X= %s, Y= %.2f" % (xValue .strftime ("%b %d, %Y %H:%M" ), event .ydata ))
294+ self .toolbar .msg .SetForegroundColour ((66 , 66 , 66 ))
295+ else :
296+ self .toolbar .msg .SetLabelText ("" )
297+ except ValueError :
298+ pass
299+
300+ def _onPick (self , event ):
301+ """
302+
303+ :param event:
304+ :return:
305+ """
306+
307+ if isinstance (event .artist , Line2D ):
308+ thisline = event .artist
309+ xdata = thisline .get_xdata ()
310+ ydata = thisline .get_ydata ()
311+ ind = event .ind
312+
313+ xValue = xdata [ind ][0 ]
314+ yValue = ydata [ind ][0 ]
315+ #tip = '(%s, %s)' % (xValue.strftime("%Y-%m-%d %H:%M:%S"), yValue)
316+ #tip = '(%s, %s)' % (xValue.strftime("%b %d, %Y %H:%M:%S"), yValue)
317+ tip = '(%s, %s)' % (xValue .strftime ("%b %d, %Y %H:%M" ), yValue )
318+
319+ self .tooltip .SetString (tip )
320+ self .tooltip .Enable (True )
321+ self .tooltip .SetAutoPop (10000 )
322+
323+ elif isinstance (event .artist , Text ):
324+ text = event .artist
325+ #print "Picking Label: ", text.get_text()
326+
327+ def _onFigureLeave (self , event ):
328+ """Catches mouse leaving the figure
329+
330+ :param event:
331+ :return:
332+ """
333+
334+ if self .tooltip .Window .Enabled :
335+ self .tooltip .SetTip ("" )
336+
337+
255338#must add these methods for mac functionality
256339 def release_zoom (self , event ):
257340 super (self .__class__ , self ).release_zoom (event )
@@ -268,3 +351,58 @@ def forward(self, event):
268351 def home (self , event ):
269352 super (self .__class__ , self ).home (event )
270353 self .canvas .draw ()
354+
355+
356+
357+
358+
359+
360+ def on_scroll_zoom (self , event ):
361+ axes = self .canvas .figure .axes [0 ]
362+ base_scale = 1.2
363+ # get the current x and y limits
364+ cur_xlim = axes .get_xlim ()
365+ cur_ylim = axes .get_ylim ()
366+ cur_xrange = (cur_xlim [1 ] - cur_xlim [0 ])* .5
367+ cur_yrange = (cur_ylim [1 ] - cur_ylim [0 ])* .5
368+ xdata = event .xdata # get event x location
369+ ydata = event .ydata # get event y location
370+ if event .button == 'up' :
371+ # deal with zoom in
372+ scale_factor = 1 / base_scale
373+ elif event .button == 'down' :
374+ # deal with zoom out
375+ scale_factor = base_scale
376+ else :
377+ # deal with something that should never happen
378+ scale_factor = 1
379+ print event .button
380+ # set new limits
381+ axes .set_xlim ([xdata - cur_xrange * scale_factor ,
382+ xdata + cur_xrange * scale_factor ])
383+ axes .set_ylim ([ydata - cur_yrange * scale_factor ,
384+ ydata + cur_yrange * scale_factor ])
385+ self .canvas .draw () # force re-draw
386+
387+ # fig = ax.get_figure() # get the figure of interest
388+ # attach the call back
389+
390+
391+ class ToolTip (wx .ToolTip ):
392+ """
393+ a subclass of wx.Tooltip which can be disable on mac
394+ """
395+
396+ def __init__ (self , tip ):
397+ self .tooltip_string = tip
398+ self .TooltipsEnabled = True
399+ wx .ToolTip .__init__ (self , tip )
400+
401+ def SetString (self , tip ):
402+ self .tooltip_string = tip
403+
404+ def Enable (self , x ):
405+ print ("in custom tooltip set enable" )
406+ if x : self .SetTip (self .tooltip_string )
407+ else : self .SetTip ("" )
408+
0 commit comments