Skip to content

Commit

Permalink
Add zoom in/out using the mouse wheel.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni committed Apr 28, 2024
1 parent e2f222a commit d51227d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/UI/plots_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>

import math
import tkinter as tk
from tkinter import font, ttk
from typing import Any, List, Optional, Tuple
Expand Down Expand Up @@ -195,6 +196,23 @@ def on_draw(self, event):
for text in ax.texts:
ax.draw_artist(text)

def on_scroll(self, event: MouseEvent):
ax0 = event.inaxes

if ax0:
xmin, xmax = ax0.get_xbound()
dxl = event.xdata - xmin
dxr = xmax - event.xdata
factor = math.pow(1.5, -event.step)
tmax = ax0.lines[0].get_xdata()[-1]
xl = max(event.xdata - dxl * factor, 0.0)
xr = min(event.xdata + dxr * factor, tmax)

for ax in event.canvas.figure.axes:
ax.set_xlim(xl, xr)

self.reset_and_redraw()

def add_properties(self, properties: List[FGPropertyNode], event: tk.Event):
# Check if the properties are dropped on a subplot
canvas = self.canvas
Expand Down Expand Up @@ -236,6 +254,7 @@ def initialize_canvas(self):
self.canvas.mpl_connect("motion_notify_event", self.on_move)
self.canvas.mpl_connect("button_press_event", self.on_click)
self.canvas.mpl_connect("key_press_event", self.on_key_press)
self.canvas.mpl_connect("scroll_event", self.on_scroll)
self.selected_line = SelectedLine(
self.canvas.figure, linewidth=4, color="red"
)
Expand Down

0 comments on commit d51227d

Please sign in to comment.