Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mousewheel zooming in PlotCanvas #2476

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions wx/lib/plot/plotcanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
# Create some mouse events for zooming
self.canvas.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
self.canvas.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.OnWheel)
self.canvas.Bind(wx.EVT_MOTION, self.OnMotion)
self.canvas.Bind(wx.EVT_LEFT_DCLICK, self.OnMouseDoubleClick)
self.canvas.Bind(wx.EVT_RIGHT_DOWN, self.OnMouseRightDown)
Expand Down Expand Up @@ -112,8 +113,8 @@ def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
self._screenCoordinates = np.array([0.0, 0.0])

# Zooming variables
self._zoomInFactor = 0.5
self._zoomOutFactor = 2
self._zoomInFactor = 1 / 1.2
self._zoomOutFactor = 1.2
self._zoomCorner1 = np.array([0.0, 0.0]) # left mouse down corner
self._zoomCorner2 = np.array([0.0, 0.0]) # left mouse up corner
self._zoomEnabled = False
Expand Down Expand Up @@ -1964,20 +1965,25 @@ def Clear(self):
dc.SetTextBackground(self.GetBackgroundColour())
self.last_draw = None

def Zoom(self, Center, Ratio):
def Zoom(self, Mouse, Ratio):
"""
Zoom on the plot
Centers on the X,Y coords given in Center
Zooms on the X,Y coords given in Mouse
Zooms by the Ratio = (Xratio, Yratio) given
"""
self.last_PointLabel = None # reset maker
x, y = Center
x, y = Mouse
if self.last_draw is not None:
(graphics, xAxis, yAxis) = self.last_draw

xr = (x-xAxis[0])/(xAxis[1]-xAxis[0])
yr = (y-yAxis[0])/(yAxis[1]-yAxis[0])

w = (xAxis[1] - xAxis[0]) * Ratio[0]
xAxis = ( x - w*xr, x + w*(1-xr) )
h = (yAxis[1] - yAxis[0]) * Ratio[1]
xAxis = (x - w / 2, x + w / 2)
yAxis = (y - h / 2, y + h / 2)
yAxis = ( y - h*yr, y + h*(1-yr) )

self._Draw(graphics, xAxis, yAxis)

def GetClosestPoints(self, pntXY, pointScaled=True):
Expand Down Expand Up @@ -2072,6 +2078,16 @@ def UpdatePointLabel(self, mDataDict):
#
# What this change would do is remove most of the if statements
# within these event handlers.

def OnWheel(self, evt):
X,Y = self._getXY(evt)
if evt.GetWheelRotation() < 0:
zoom = [1.0 if evt.ShiftDown() else self._zoomOutFactor, 1.0 if evt.ControlDown() else self._zoomOutFactor]
self.Zoom((X, Y), zoom)
else:
zoom = [1.0 if evt.ShiftDown() else self._zoomInFactor, 1.0 if evt.ControlDown() else self._zoomInFactor]
self.Zoom((X, Y), zoom)

def OnMotion(self, event):
if self._zoomEnabled and event.LeftIsDown():
if self._hasDragged:
Expand Down