1 | import wx |
---|
2 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg, _convert_agg_to_wx_bitmap |
---|
3 | from matplotlib.backends.backend_agg import FigureCanvasAgg |
---|
4 | |
---|
5 | class FigureCanvas(FigureCanvasWxAgg): |
---|
6 | """ |
---|
7 | Add features to the wx agg canvas for better support of AUI and |
---|
8 | faster plotting. |
---|
9 | """ |
---|
10 | |
---|
11 | def __init__(self, *args, **kw): |
---|
12 | super(FigureCanvas,self).__init__(*args, **kw) |
---|
13 | self._isRendered = False |
---|
14 | |
---|
15 | # Create an timer for handling draw_idle requests |
---|
16 | # If there are events pending when the timer is |
---|
17 | # complete, reset the timer and continue. The |
---|
18 | # alternative approach, binding to wx.EVT_IDLE, |
---|
19 | # doesn't behave as nicely. |
---|
20 | self.idletimer = wx.CallLater(1,self._onDrawIdle) |
---|
21 | |
---|
22 | def draw_idle(self, *args, **kwargs): |
---|
23 | """ |
---|
24 | Render after a delay if no other render requests have been made. |
---|
25 | """ |
---|
26 | self.idletimer.Restart(5, *args, **kwargs) # Delay by 5 ms |
---|
27 | |
---|
28 | def _onDrawIdle(self, *args, **kwargs): |
---|
29 | if False and wx.GetApp().Pending(): |
---|
30 | self.idletimer.Restart(5, *args, **kwargs) |
---|
31 | else: |
---|
32 | self.draw(*args, **kwargs) |
---|
33 | |
---|
34 | def draw(self, drawDC=None): |
---|
35 | """ |
---|
36 | Render the figure using agg. |
---|
37 | """ |
---|
38 | |
---|
39 | # Only draw if window is shown, otherwise graph will bleed through |
---|
40 | # on the notebook style AUI widgets. |
---|
41 | if self.IsShownOnScreen(): |
---|
42 | self._isRendered = True |
---|
43 | FigureCanvasWxAgg.draw(self) |
---|
44 | self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) |
---|
45 | self.gui_repaint(drawDC=drawDC) |
---|
46 | else: |
---|
47 | self._isRendered = False |
---|
48 | |
---|
49 | def _onPaint(self, evt): |
---|
50 | """ |
---|
51 | Called when wxPaintEvt is generated |
---|
52 | """ |
---|
53 | |
---|
54 | if not self._isRealized: |
---|
55 | self.realize() |
---|
56 | |
---|
57 | # Need to draw the graph the first time it is shown otherwise |
---|
58 | # it is a black canvas. After that we can use the rendered |
---|
59 | # bitmap for updates. |
---|
60 | if self._isRendered: |
---|
61 | self.gui_repaint(drawDC=wx.PaintDC(self)) |
---|
62 | else: |
---|
63 | self.draw(drawDC=wx.PaintDC(self)) |
---|
64 | |
---|
65 | evt.Skip() |
---|
66 | |
---|