Skip to content

Commit 22eb1d5

Browse files
committed
Merge pull request #3968 from blink1073/web-scroll-event
ENH : Add Support for `scroll_event` in WebAgg and NbAgg
1 parent 893352f commit 22eb1d5

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def handle_event(self, event):
193193
elif e_type == 'draw':
194194
self.draw()
195195
elif e_type in ('button_press', 'button_release', 'motion_notify',
196-
'figure_enter', 'figure_leave'):
196+
'figure_enter', 'figure_leave', 'scroll'):
197197
x = event['x']
198198
y = event['y']
199199
y = self.get_renderer().height - y
@@ -219,6 +219,8 @@ def handle_event(self, event):
219219
self.enter_notify_event(xy=(x, y))
220220
elif e_type == 'figure_leave':
221221
self.leave_notify_event()
222+
elif e_type == 'scroll':
223+
self.scroll_event(x, y, event['step'])
222224
elif e_type in ('key_press', 'key_release'):
223225
key = event['key']
224226

lib/matplotlib/backends/web_backend/mpl.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ mpl.figure.prototype._init_header = function() {
9494
this.header = titletext[0];
9595
}
9696

97+
98+
9799
mpl.figure.prototype._canvas_extra_style = function(canvas_div) {
98100

99101
}
@@ -143,6 +145,17 @@ mpl.figure.prototype._init_canvas = function() {
143145
rubberband.mouseenter('figure_enter', mouse_event_fn);
144146
rubberband.mouseleave('figure_leave', mouse_event_fn);
145147

148+
canvas_div.on("wheel", function (event) {
149+
event = event.originalEvent;
150+
event['data'] = 'scroll'
151+
if (event.deltaY < 0) {
152+
event.step = 1;
153+
} else {
154+
event.step = -1;
155+
}
156+
mouse_event_fn(event);
157+
});
158+
146159
canvas_div.append(canvas);
147160
canvas_div.append(rubberband);
148161

@@ -168,6 +181,7 @@ mpl.figure.prototype._init_canvas = function() {
168181
// upon first draw.
169182
this._resize_canvas(600, 600);
170183

184+
171185
function set_focus () {
172186
canvas.focus();
173187
canvas_div.focus();
@@ -425,7 +439,8 @@ mpl.figure.prototype.mouse_event = function(event, name) {
425439
var x = canvas_pos.x;
426440
var y = canvas_pos.y;
427441

428-
this.send_message(name, {x: x, y: y, button: event.button});
442+
this.send_message(name, {x: x, y: y, button: event.button,
443+
step: event.step});
429444

430445
/* This prevents the web browser from automatically changing to
431446
* the text insertion cursor when the button is pressed. We want

lib/matplotlib/backends/web_backend/nbagg_uat.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:43daeaae8ae9fd496fc33d7a64639194bc009b755d28c23cd6329f225628197c"
4+
"signature": "sha256:7f7ec6a6e2a63837a45a88a501ba3c5b1eb88e744925456a9bfeb0d6faa896a5"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -400,9 +400,9 @@
400400
"cell_type": "markdown",
401401
"metadata": {},
402402
"source": [
403-
"### UAT 16 - key events\n",
403+
"### UAT 16 - Events\n",
404404
"\n",
405-
"Pressing any keyboard key or mouse button should cycle the line line while the figure has focus. The figure should have focus by default when it is created and re-gain it by clicking on the canvas. Clicking anywhere outside of the figure should release focus, but moving the mouse out of the figure should not release focus."
405+
"Pressing any keyboard key or mouse button (or scrolling) should cycle the line line while the figure has focus. The figure should have focus by default when it is created and re-gain it by clicking on the canvas. Clicking anywhere outside of the figure should release focus, but moving the mouse out of the figure should not release focus."
406406
]
407407
},
408408
{
@@ -423,6 +423,7 @@
423423
" fig.canvas.draw_idle()\n",
424424
"fig.canvas.mpl_connect('button_press_event', on_event)\n",
425425
"fig.canvas.mpl_connect('key_press_event', on_event)\n",
426+
"fig.canvas.mpl_connect('scroll_event', on_event)\n",
426427
"plt.show()"
427428
],
428429
"language": "python",

0 commit comments

Comments
 (0)