@@ -41,17 +41,90 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
41
41
super ( ) ;
42
42
this . addClass ( CSS_CLASS ) ;
43
43
this . _mimeType = options . mimeType ;
44
+
45
+ this . _img_el = < HTMLImageElement > ( document . createElement ( "img" ) ) ;
46
+ this . _img_el . className = 'plot-img' ;
47
+ this . node . appendChild ( this . _img_el ) ;
48
+
49
+ // Install hover callback
50
+ this . _img_el . addEventListener ( 'mouseenter' , event => {
51
+ this . createGraph ( this . _model ) ;
52
+ } )
44
53
}
45
54
46
55
/**
47
56
* Render Plotly into this widget's node.
48
57
*/
49
58
renderModel ( model : IRenderMime . IMimeModel ) : Promise < void > {
59
+
60
+ if ( this . hasGraphElement ( ) ) {
61
+ // We already have a graph, don't overwrite it
62
+ return Promise . resolve ( ) ;
63
+ }
64
+
65
+ this . _model = model ;
66
+
67
+ const png_data = < string > model . data [ 'image/png' ] ;
68
+ if ( png_data !== undefined && png_data !== null ) {
69
+ // We have PNG data, use it
70
+ this . createImage ( png_data ) ;
71
+ return Promise . resolve ( ) ;
72
+ } else {
73
+ // Create a new graph
74
+ return this . createGraph ( model ) ;
75
+ }
76
+ }
77
+
78
+ private hasGraphElement ( ) {
79
+ return this . node . querySelector ( '.plot-container' ) !== null
80
+ }
81
+
82
+ private createImage ( png_data : string ) {
83
+ this . hideGraph ( ) ;
84
+ this . _img_el . src = "data:image/png;base64," + < string > png_data ;
85
+ this . showImage ( ) ;
86
+ }
87
+
88
+ private hideGraph ( ) {
89
+ // Hide any graph
90
+ let el = < HTMLDivElement > this . node . querySelector ( '.plot-container' ) ;
91
+ if ( el !== null && el !== undefined ) {
92
+ el . style . display = "none"
93
+ }
94
+ }
95
+
96
+ private showGraph ( ) {
97
+ // Hide any graph
98
+ let el = < HTMLDivElement > this . node . querySelector ( '.plot-container' ) ;
99
+ if ( el !== null && el !== undefined ) {
100
+ el . style . display = "block"
101
+ }
102
+ }
103
+
104
+ private hideImage ( ) {
105
+ // Hide any graph
106
+ let el = < HTMLImageElement > this . node . querySelector ( '.plot-img' ) ;
107
+ if ( el !== null && el !== undefined ) {
108
+ el . style . display = "none"
109
+ }
110
+ }
111
+
112
+ private showImage ( ) {
113
+ // Hide any graph
114
+ let el = < HTMLImageElement > this . node . querySelector ( '.plot-img' ) ;
115
+ if ( el !== null && el !== undefined ) {
116
+ el . style . display = "block"
117
+ }
118
+ }
119
+
120
+ private createGraph ( model : IRenderMime . IMimeModel ) {
50
121
const { data, layout, frames, config } = model . data [ this . _mimeType ] as
51
122
| any
52
123
| IPlotlySpec ;
53
- // const metadata = model.metadata[this._mimeType] as any || {};
124
+
54
125
return Plotly . react ( this . node , data , layout , config ) . then ( plot => {
126
+ this . showGraph ( ) ;
127
+ this . hideImage ( ) ;
55
128
this . update ( ) ;
56
129
if ( frames ) {
57
130
Plotly . addFrames ( this . node , frames ) . then ( ( ) => {
@@ -96,14 +169,16 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
96
169
* A message handler invoked on an `'update-request'` message.
97
170
*/
98
171
protected onUpdateRequest ( msg : Message ) : void {
99
- if ( this . isVisible ) {
172
+ if ( this . isVisible && this . hasGraphElement ( ) ) {
100
173
Plotly . redraw ( this . node ) . then ( ( ) => {
101
174
Plotly . Plots . resize ( this . node ) ;
102
175
} ) ;
103
176
}
104
177
}
105
178
106
179
private _mimeType : string ;
180
+ private _img_el : HTMLImageElement ;
181
+ private _model : IRenderMime . IMimeModel
107
182
}
108
183
109
184
/**
0 commit comments