Skip to content

Commit 9f9992a

Browse files
committed
Fix for GH 160.
When opening a plotly figure from a file the `this.node.offsetWidth` and `this.node.offsetHeight` values are 0, which causes an error in `Plotly.toImage`. Add guards to not call `Plotly.toImage` unless width and height are positive numbers.
1 parent ecb0c95 commit 9f9992a

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

packages/plotly-extension/src/index.ts

+31-22
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,40 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
5656
return Plotly.addFrames(this.node, frames).then(() => {
5757
Plotly.animate(this.node);
5858
this.update();
59-
return Plotly.toImage(plot, {
60-
format: 'png',
61-
width: this.node.offsetWidth,
62-
height: this.node.offsetHeight
63-
}).then((url: string) => {
64-
const imageData = url.split(',')[1];
65-
if (model.data['image/png'] !== imageData) {
66-
model.setData({
67-
data: { ...model.data, 'image/png': imageData }
68-
});
69-
}
70-
});
59+
if (this.node.offsetWidth > 0 && this.node.offsetHeight > 0) {
60+
return Plotly.toImage(plot, {
61+
format: 'png',
62+
width: this.node.offsetWidth,
63+
height: this.node.offsetHeight
64+
}).then((url: string) => {
65+
const imageData = url.split(',')[1];
66+
if (model.data['image/png'] !== imageData) {
67+
model.setData({
68+
data: { ...model.data, 'image/png': imageData }
69+
});
70+
}
71+
});
72+
}
7173
});
7274
}
7375
this.update();
74-
return Plotly.toImage(plot, {
75-
format: 'png',
76-
width: this.node.offsetWidth,
77-
height: this.node.offsetHeight
78-
}).then((url: string) => {
79-
const imageData = url.split(',')[1];
80-
if (model.data['image/png'] !== imageData) {
81-
model.setData({ data: { ...model.data, 'image/png': imageData } });
82-
}
83-
});
76+
if (this.node.offsetWidth > 0 && this.node.offsetHeight > 0) {
77+
return Plotly.toImage(plot, {
78+
format: 'png',
79+
width: this.node.offsetWidth,
80+
height: this.node.offsetHeight
81+
}).then((url: string) => {
82+
const imageData = url.split(',')[1];
83+
if (model.data['image/png'] !== imageData) {
84+
model.setData({
85+
data: {
86+
...model.data,
87+
'image/png': imageData
88+
}
89+
});
90+
}
91+
});
92+
}
8493
});
8594
}
8695

0 commit comments

Comments
 (0)