1
1
// Copyright (c) Jupyter Development Team.
2
2
// Distributed under the terms of the Modified BSD License.
3
3
4
- import { Widget } from ' @lumino/widgets' ;
4
+ import { Widget } from " @lumino/widgets" ;
5
5
6
- import { Message } from ' @lumino/messaging' ;
6
+ import { Message } from " @lumino/messaging" ;
7
7
8
- import { IRenderMime } from ' @jupyterlab/rendermime-interfaces' ;
8
+ import { IRenderMime } from " @jupyterlab/rendermime-interfaces" ;
9
9
10
- import ' ../style/index.css' ;
10
+ import " ../style/index.css" ;
11
11
12
12
/**
13
13
* The CSS class to add to the Plotly Widget.
14
14
*/
15
- const CSS_CLASS = ' jp-RenderedPlotly' ;
15
+ const CSS_CLASS = " jp-RenderedPlotly" ;
16
16
17
17
/**
18
18
* The CSS class for a Plotly icon.
19
19
*/
20
- const CSS_ICON_CLASS = ' jp-MaterialIcon jp-PlotlyIcon' ;
20
+ const CSS_ICON_CLASS = " jp-MaterialIcon jp-PlotlyIcon" ;
21
21
22
22
/**
23
23
* The MIME type for Plotly.
24
24
* The version of this follows the major version of Plotly.
25
25
*/
26
- export const MIME_TYPE = ' application/vnd.plotly.v1+json' ;
26
+ export const MIME_TYPE = " application/vnd.plotly.v1+json" ;
27
27
28
28
interface PlotlyHTMLElement extends HTMLElement {
29
- on ( event : ' plotly_webglcontextlost' , callback : ( ) => void ) : void ;
29
+ on ( event : " plotly_webglcontextlost" , callback : ( ) => void ) : void ;
30
30
}
31
31
32
32
type Frame = { [ key : string ] : any } ;
@@ -41,24 +41,24 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
41
41
this . _mimeType = options . mimeType ;
42
42
43
43
// Create image element
44
- this . _img_el = < HTMLImageElement > ( document . createElement ( "img" ) ) ;
45
- this . _img_el . className = ' plot-img' ;
44
+ this . _img_el = < HTMLImageElement > document . createElement ( "img" ) ;
45
+ this . _img_el . className = " plot-img" ;
46
46
this . node . appendChild ( this . _img_el ) ;
47
47
48
48
// Install image hover callback
49
- import ( /* webpackChunkName: 'plotly'*/ 'plotly.js/dist/plotly' ) . then ( Plotly => {
50
-
51
- this . _img_el . addEventListener ( 'mouseenter' , event => {
52
- this . createGraph ( this . _model , Plotly ) ;
53
- } )
54
- } )
49
+ import ( /* webpackChunkName: 'plotly'*/ "plotly.js/dist/plotly" ) . then (
50
+ ( Plotly ) => {
51
+ this . _img_el . addEventListener ( "mouseenter" , ( event ) => {
52
+ this . createGraph ( this . _model , Plotly ) ;
53
+ } ) ;
54
+ }
55
+ ) ;
55
56
}
56
57
57
58
/**
58
59
* Render Plotly into this widget's node.
59
60
*/
60
61
renderModel ( model : IRenderMime . IMimeModel ) : Promise < void > {
61
-
62
62
if ( this . hasGraphElement ( ) ) {
63
63
// We already have a graph, don't overwrite it
64
64
return Promise . resolve ( ) ;
@@ -68,23 +68,25 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
68
68
this . _model = model ;
69
69
70
70
// Check for PNG data in mime bundle
71
- const png_data = < string > model . data [ ' image/png' ] ;
72
- if ( png_data !== undefined && png_data !== null ) {
71
+ const png_data = < string > model . data [ " image/png" ] ;
72
+ if ( png_data !== undefined && png_data !== null ) {
73
73
// We have PNG data, use it
74
74
this . updateImage ( png_data ) ;
75
75
return Promise . resolve ( ) ;
76
76
} else {
77
77
// Create a new graph
78
- return import ( /* webpackChunkName: 'plotly'*/ 'plotly.js/dist/plotly' ) . then ( Plotly => {
78
+ return import (
79
+ /* webpackChunkName: 'plotly'*/ "plotly.js/dist/plotly"
80
+ ) . then ( ( Plotly ) => {
79
81
this . createGraph ( model , Plotly ) ;
80
- } )
82
+ } ) ;
81
83
}
82
84
}
83
85
84
86
private hasGraphElement ( ) {
85
87
// Check for the presence of the .plot-container element that plotly.js
86
88
// places at the top of the figure structure
87
- return this . node . querySelector ( ' .plot-container' ) !== null
89
+ return this . node . querySelector ( " .plot-container" ) !== null ;
88
90
}
89
91
90
92
private updateImage ( png_data : string ) {
@@ -95,43 +97,43 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
95
97
96
98
private hideGraph ( ) {
97
99
// Hide the graph if there is one
98
- let el = < HTMLDivElement > this . node . querySelector ( ' .plot-container' ) ;
100
+ let el = < HTMLDivElement > this . node . querySelector ( " .plot-container" ) ;
99
101
if ( el !== null && el !== undefined ) {
100
- el . style . display = "none"
102
+ el . style . display = "none" ;
101
103
}
102
104
}
103
105
104
106
private showGraph ( ) {
105
107
// Show the graph if there is one
106
- let el = < HTMLDivElement > this . node . querySelector ( ' .plot-container' ) ;
108
+ let el = < HTMLDivElement > this . node . querySelector ( " .plot-container" ) ;
107
109
if ( el !== null && el !== undefined ) {
108
- el . style . display = "block"
110
+ el . style . display = "block" ;
109
111
}
110
112
}
111
113
112
114
private hideImage ( ) {
113
115
// Hide the image element
114
- let el = < HTMLImageElement > this . node . querySelector ( ' .plot-img' ) ;
116
+ let el = < HTMLImageElement > this . node . querySelector ( " .plot-img" ) ;
115
117
if ( el !== null && el !== undefined ) {
116
- el . style . display = "none"
118
+ el . style . display = "none" ;
117
119
}
118
120
}
119
121
120
122
private showImage ( ) {
121
123
// Show the image element
122
- let el = < HTMLImageElement > this . node . querySelector ( ' .plot-img' ) ;
124
+ let el = < HTMLImageElement > this . node . querySelector ( " .plot-img" ) ;
123
125
if ( el !== null && el !== undefined ) {
124
- el . style . display = "block"
126
+ el . style . display = "block" ;
125
127
}
126
128
}
127
129
128
130
private createGraph ( model : IRenderMime . IMimeModel , Plotly : any ) {
129
131
const { data, layout, frames, config } = model . data [ this . _mimeType ] as
130
132
| any
131
- | { data : Plotly . Data ; layout : Plotly . Layout ; frames ?: Frame [ ] ; }
132
- | { data : Plotly . Data ; layout : Plotly . Layout ; frames ?: Plotly . Frame [ ] ; }
133
+ | { data : Plotly . Data ; layout : Plotly . Layout ; frames ?: Frame [ ] }
134
+ | { data : Plotly . Data ; layout : Plotly . Layout ; frames ?: Plotly . Frame [ ] } ;
133
135
134
- return Plotly . react ( this . node , data , layout , config ) . then ( plot => {
136
+ return Plotly . react ( this . node , data , layout , config ) . then ( ( plot : any ) => {
135
137
this . showGraph ( ) ;
136
138
this . hideImage ( ) ;
137
139
this . update ( ) ;
@@ -140,31 +142,31 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
140
142
}
141
143
if ( this . node . offsetWidth > 0 && this . node . offsetHeight > 0 ) {
142
144
Plotly . toImage ( plot , {
143
- format : ' png' ,
145
+ format : " png" ,
144
146
width : this . node . offsetWidth ,
145
- height : this . node . offsetHeight
147
+ height : this . node . offsetHeight ,
146
148
} ) . then ( ( url : string ) => {
147
- const imageData = url . split ( ',' ) [ 1 ] ;
148
- if ( model . data [ ' image/png' ] !== imageData ) {
149
+ const imageData = url . split ( "," ) [ 1 ] ;
150
+ if ( model . data [ " image/png" ] !== imageData ) {
149
151
model . setData ( {
150
152
data : {
151
153
...model . data ,
152
- ' image/png' : imageData
153
- }
154
+ " image/png" : imageData ,
155
+ } ,
154
156
} ) ;
155
157
}
156
158
} ) ;
157
159
}
158
160
159
161
// Handle webgl context lost events
160
- ( < PlotlyHTMLElement > ( this . node ) ) . on ( ' plotly_webglcontextlost' , ( ) => {
161
- const png_data = < string > model . data [ ' image/png' ] ;
162
- if ( png_data !== undefined && png_data !== null ) {
163
- // We have PNG data, use it
164
- this . updateImage ( png_data ) ;
165
- return Promise . resolve ( ) ;
166
- }
167
- } ) ;
162
+ ( < PlotlyHTMLElement > this . node ) . on ( " plotly_webglcontextlost" , ( ) => {
163
+ const png_data = < string > model . data [ " image/png" ] ;
164
+ if ( png_data !== undefined && png_data !== null ) {
165
+ // We have PNG data, use it
166
+ this . updateImage ( png_data ) ;
167
+ return Promise . resolve ( ) ;
168
+ }
169
+ } ) ;
168
170
} ) ;
169
171
}
170
172
@@ -187,17 +189,19 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
187
189
*/
188
190
protected onUpdateRequest ( msg : Message ) : void {
189
191
if ( this . isVisible && this . hasGraphElement ( ) ) {
190
- import ( /* webpackChunkName: 'plotly'*/ 'plotly.js/dist/plotly' ) . then ( Plotly => {
191
- Plotly . redraw ( this . node ) . then ( ( ) => {
192
- Plotly . Plots . resize ( this . node ) ;
193
- } ) ;
194
- } )
192
+ import ( /* webpackChunkName: 'plotly'*/ "plotly.js/dist/plotly" ) . then (
193
+ ( Plotly ) => {
194
+ Plotly . redraw ( this . node ) . then ( ( ) => {
195
+ Plotly . Plots . resize ( this . node ) ;
196
+ } ) ;
197
+ }
198
+ ) ;
195
199
}
196
200
}
197
201
198
202
private _mimeType : string ;
199
203
private _img_el : HTMLImageElement ;
200
- private _model : IRenderMime . IMimeModel
204
+ private _model : IRenderMime . IMimeModel ;
201
205
}
202
206
203
207
/**
@@ -206,30 +210,30 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
206
210
export const rendererFactory : IRenderMime . IRendererFactory = {
207
211
safe : true ,
208
212
mimeTypes : [ MIME_TYPE ] ,
209
- createRenderer : options => new RenderedPlotly ( options )
213
+ createRenderer : ( options ) => new RenderedPlotly ( options ) ,
210
214
} ;
211
215
212
216
const extensions : IRenderMime . IExtension | IRenderMime . IExtension [ ] = [
213
217
{
214
- id : ' @jupyterlab/plotly-extension:factory' ,
218
+ id : " @jupyterlab/plotly-extension:factory" ,
215
219
rendererFactory,
216
220
rank : 0 ,
217
- dataType : ' json' ,
221
+ dataType : " json" ,
218
222
fileTypes : [
219
223
{
220
- name : ' plotly' ,
224
+ name : " plotly" ,
221
225
mimeTypes : [ MIME_TYPE ] ,
222
- extensions : [ ' .plotly' , ' .plotly.json' ] ,
223
- iconClass : CSS_ICON_CLASS
224
- }
226
+ extensions : [ " .plotly" , " .plotly.json" ] ,
227
+ iconClass : CSS_ICON_CLASS ,
228
+ } ,
225
229
] ,
226
230
documentWidgetFactoryOptions : {
227
- name : ' Plotly' ,
228
- primaryFileType : ' plotly' ,
229
- fileTypes : [ ' plotly' , ' json' ] ,
230
- defaultFor : [ ' plotly' ]
231
- }
232
- }
231
+ name : " Plotly" ,
232
+ primaryFileType : " plotly" ,
233
+ fileTypes : [ " plotly" , " json" ] ,
234
+ defaultFor : [ " plotly" ] ,
235
+ } ,
236
+ } ,
233
237
] ;
234
238
235
- export default extensions ;
239
+ export default extensions ;
0 commit comments