Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 254f062

Browse files
authored
Merge pull request #18479 from tacaswell/nbagg_backports
Nbagg backports
2 parents 0b1eb01 + 9dda79c commit 254f062

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ mpl.figure = function (figure_id, websocket, ondownload, parent_element) {
6262
this.ws.onopen = function () {
6363
fig.send_message('supports_binary', { value: fig.supports_binary });
6464
fig.send_message('send_image_mode', {});
65-
if (mpl.ratio !== 1) {
66-
fig.send_message('set_dpi_ratio', { dpi_ratio: mpl.ratio });
65+
if (fig.ratio !== 1) {
66+
fig.send_message('set_dpi_ratio', { dpi_ratio: fig.ratio });
6767
}
6868
fig.send_message('refresh', {});
6969
};
@@ -156,7 +156,10 @@ mpl.figure.prototype._init_canvas = function () {
156156
this.context.backingStorePixelRatio ||
157157
1;
158158

159-
mpl.ratio = (window.devicePixelRatio || 1) / backingStore;
159+
this.ratio = (window.devicePixelRatio || 1) / backingStore;
160+
if (this.ratio !== 1) {
161+
fig.send_message('set_dpi_ratio', { dpi_ratio: this.ratio });
162+
}
160163

161164
var rubberband_canvas = (this.rubberband_canvas = document.createElement(
162165
'canvas'
@@ -200,8 +203,8 @@ mpl.figure.prototype._init_canvas = function () {
200203
entry.devicePixelContentBoxSize[0].blockSize
201204
);
202205
} else {
203-
canvas.setAttribute('width', width * mpl.ratio);
204-
canvas.setAttribute('height', height * mpl.ratio);
206+
canvas.setAttribute('width', width * fig.ratio);
207+
canvas.setAttribute('height', height * fig.ratio);
205208
}
206209
canvas.setAttribute(
207210
'style',
@@ -399,10 +402,10 @@ mpl.figure.prototype.handle_resize = function (fig, msg) {
399402
};
400403

401404
mpl.figure.prototype.handle_rubberband = function (fig, msg) {
402-
var x0 = msg['x0'] / mpl.ratio;
403-
var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;
404-
var x1 = msg['x1'] / mpl.ratio;
405-
var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;
405+
var x0 = msg['x0'] / fig.ratio;
406+
var y0 = (fig.canvas.height - msg['y0']) / fig.ratio;
407+
var x1 = msg['x1'] / fig.ratio;
408+
var y1 = (fig.canvas.height - msg['y1']) / fig.ratio;
406409
x0 = Math.floor(x0) + 0.5;
407410
y0 = Math.floor(y0) + 0.5;
408411
x1 = Math.floor(x1) + 0.5;
@@ -415,8 +418,8 @@ mpl.figure.prototype.handle_rubberband = function (fig, msg) {
415418
fig.rubberband_context.clearRect(
416419
0,
417420
0,
418-
fig.canvas.width / mpl.ratio,
419-
fig.canvas.height / mpl.ratio
421+
fig.canvas.width / fig.ratio,
422+
fig.canvas.height / fig.ratio
420423
);
421424

422425
fig.rubberband_context.strokeRect(min_x, min_y, width, height);
@@ -599,8 +602,8 @@ mpl.figure.prototype.mouse_event = function (event, name) {
599602
this.canvas_div.focus();
600603
}
601604

602-
var x = canvas_pos.x * mpl.ratio;
603-
var y = canvas_pos.y * mpl.ratio;
605+
var x = canvas_pos.x * this.ratio;
606+
var y = canvas_pos.y * this.ratio;
604607

605608
this.send_message(name, {
606609
x: x,

lib/matplotlib/backends/web_backend/js/nbagg_mpl.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ mpl.mpl_figure_comm = function (comm, msg) {
4848
console.error('Failed to find cell for figure', id, fig);
4949
return;
5050
}
51+
fig.cell_info[0].output_area.element.one(
52+
'cleared',
53+
{ fig: fig },
54+
fig._remove_fig_handler
55+
);
5156
};
5257

5358
mpl.figure.prototype.handle_close = function (fig, msg) {
54-
var width = fig.canvas.width / mpl.ratio;
55-
fig.root.removeEventListener('remove', this._remove_fig_handler);
59+
var width = fig.canvas.width / fig.ratio;
60+
fig.cell_info[0].output_area.element.off(
61+
'cleared',
62+
fig._remove_fig_handler
63+
);
5664

5765
// Update the output cell to use the data from the current canvas.
5866
fig.push_to_output();
@@ -72,7 +80,7 @@ mpl.figure.prototype.close_ws = function (fig, msg) {
7280

7381
mpl.figure.prototype.push_to_output = function (_remove_interactive) {
7482
// Turn the data on the canvas into data in the output cell.
75-
var width = this.canvas.width / mpl.ratio;
83+
var width = this.canvas.width / this.ratio;
7684
var dataURL = this.canvas.toDataURL();
7785
this.cell_info[1]['text/html'] =
7886
'<img src="' + dataURL + '" width="' + width + '">';
@@ -171,13 +179,13 @@ mpl.figure.prototype._init_toolbar = function () {
171179
titlebar.insertBefore(buttongrp, titlebar.firstChild);
172180
};
173181

174-
mpl.figure.prototype._remove_fig_handler = function () {
175-
this.close_ws(this, {});
182+
mpl.figure.prototype._remove_fig_handler = function (event) {
183+
var fig = event.data.fig;
184+
fig.close_ws(fig, {});
176185
};
177186

178187
mpl.figure.prototype._root_extra_style = function (el) {
179188
el.style.boxSizing = 'content-box'; // override notebook setting of border-box.
180-
el.addEventListener('remove', this._remove_fig_handler);
181189
};
182190

183191
mpl.figure.prototype._canvas_extra_style = function (el) {

requirements/testing/travis_extra.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Extra pip requirements for the travis python 3.7+ builds
22

33
ipykernel
4-
nbconvert[execute]
4+
nbconvert[execute]!=6.0.0,!=6.0.1
55
nbformat!=5.0.0,!=5.0.1
66
pandas!=0.25.0
77
pikepdf

0 commit comments

Comments
 (0)