Skip to content

Commit b9eb1ff

Browse files
committed
Merge pull request matplotlib#3968 from blink1073/web-scroll-event
ENH : Add Support for `scroll_event` in WebAgg and NbAgg
2 parents ee84682 + b41a040 commit b9eb1ff

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/matplotlib/backends/backend_webagg_core.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def handle_event(self, event):
192192
elif e_type == 'draw':
193193
self.draw()
194194
elif e_type in ('button_press', 'button_release', 'motion_notify',
195-
'figure_enter', 'figure_leave'):
195+
'figure_enter', 'figure_leave', 'scroll'):
196196
x = event['x']
197197
y = event['y']
198198
y = self.get_renderer().height - y
@@ -218,6 +218,8 @@ def handle_event(self, event):
218218
self.enter_notify_event(xy=(x, y))
219219
elif e_type == 'figure_leave':
220220
self.leave_notify_event()
221+
elif e_type == 'scroll':
222+
self.scroll_event(x, y, event['step'])
221223
elif e_type in ('key_press', 'key_release'):
222224
key = event['key']
223225

lib/matplotlib/backends/web_backend/mpl.js

+16-1
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

+4-3
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)