@@ -42,11 +42,12 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
42
42
this . addClass ( CSS_CLASS ) ;
43
43
this . _mimeType = options . mimeType ;
44
44
45
+ // Create image element
45
46
this . _img_el = < HTMLImageElement > ( document . createElement ( "img" ) ) ;
46
47
this . _img_el . className = 'plot-img' ;
47
48
this . node . appendChild ( this . _img_el ) ;
48
49
49
- // Install hover callback
50
+ // Install image hover callback
50
51
this . _img_el . addEventListener ( 'mouseenter' , event => {
51
52
this . createGraph ( this . _model ) ;
52
53
} )
@@ -62,12 +63,14 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
62
63
return Promise . resolve ( ) ;
63
64
}
64
65
66
+ // Save off reference to model so that we can regenerate the plot later
65
67
this . _model = model ;
66
68
69
+ // Check for PNG data in mime bundle
67
70
const png_data = < string > model . data [ 'image/png' ] ;
68
71
if ( png_data !== undefined && png_data !== null ) {
69
72
// We have PNG data, use it
70
- this . createImage ( png_data ) ;
73
+ this . updateImage ( png_data ) ;
71
74
return Promise . resolve ( ) ;
72
75
} else {
73
76
// Create a new graph
@@ -76,41 +79,43 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
76
79
}
77
80
78
81
private hasGraphElement ( ) {
82
+ // Check for the presence of the .plot-container element that plotly.js
83
+ // places at the top of the figure structure
79
84
return this . node . querySelector ( '.plot-container' ) !== null
80
85
}
81
86
82
- private createImage ( png_data : string ) {
87
+ private updateImage ( png_data : string ) {
83
88
this . hideGraph ( ) ;
84
89
this . _img_el . src = "data:image/png;base64," + < string > png_data ;
85
90
this . showImage ( ) ;
86
91
}
87
92
88
93
private hideGraph ( ) {
89
- // Hide any graph
94
+ // Hide the graph if there is one
90
95
let el = < HTMLDivElement > this . node . querySelector ( '.plot-container' ) ;
91
96
if ( el !== null && el !== undefined ) {
92
97
el . style . display = "none"
93
98
}
94
99
}
95
100
96
101
private showGraph ( ) {
97
- // Hide any graph
102
+ // Show the graph if there is one
98
103
let el = < HTMLDivElement > this . node . querySelector ( '.plot-container' ) ;
99
104
if ( el !== null && el !== undefined ) {
100
105
el . style . display = "block"
101
106
}
102
107
}
103
108
104
109
private hideImage ( ) {
105
- // Hide any graph
110
+ // Hide the image element
106
111
let el = < HTMLImageElement > this . node . querySelector ( '.plot-img' ) ;
107
112
if ( el !== null && el !== undefined ) {
108
113
el . style . display = "none"
109
114
}
110
115
}
111
116
112
117
private showImage ( ) {
113
- // Hide any graph
118
+ // Show the image element
114
119
let el = < HTMLImageElement > this . node . querySelector ( '.plot-img' ) ;
115
120
if ( el !== null && el !== undefined ) {
116
121
el . style . display = "block"
@@ -147,12 +152,12 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
147
152
} ) ;
148
153
}
149
154
150
-
155
+ // Handle webgl context lost events
151
156
( < Plotly . PlotlyHTMLElement > ( this . node ) ) . on ( 'plotly_webglcontextlost' , ( ) => {
152
157
const png_data = < string > model . data [ 'image/png' ] ;
153
158
if ( png_data !== undefined && png_data !== null ) {
154
159
// We have PNG data, use it
155
- this . createImage ( png_data ) ;
160
+ this . updateImage ( png_data ) ;
156
161
return Promise . resolve ( ) ;
157
162
}
158
163
} ) ;
0 commit comments