@@ -17,6 +17,41 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
17
17
}
18
18
return t ;
19
19
} ;
20
+ var __awaiter = ( this && this . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
21
+ return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
22
+ function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
23
+ function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
24
+ function step ( result ) { result . done ? resolve ( result . value ) : new P ( function ( resolve ) { resolve ( result . value ) ; } ) . then ( fulfilled , rejected ) ; }
25
+ step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
26
+ } ) ;
27
+ } ;
28
+ var __generator = ( this && this . __generator ) || function ( thisArg , body ) {
29
+ var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
30
+ return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
31
+ function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
32
+ function step ( op ) {
33
+ if ( f ) throw new TypeError ( "Generator is already executing." ) ;
34
+ while ( _ ) try {
35
+ if ( f = 1 , y && ( t = y [ op [ 0 ] & 2 ? "return" : op [ 0 ] ? "throw" : "next" ] ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
36
+ if ( y = 0 , t ) op = [ 0 , t . value ] ;
37
+ switch ( op [ 0 ] ) {
38
+ case 0 : case 1 : t = op ; break ;
39
+ case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
40
+ case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
41
+ case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
42
+ default :
43
+ if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
44
+ if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
45
+ if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
46
+ if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
47
+ if ( t [ 2 ] ) _ . ops . pop ( ) ;
48
+ _ . trys . pop ( ) ; continue ;
49
+ }
50
+ op = body . call ( thisArg , _ ) ;
51
+ } catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
52
+ if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
53
+ }
54
+ } ;
20
55
var __rest = ( this && this . __rest ) || function ( s , e ) {
21
56
var t = { } ;
22
57
for ( var p in s ) if ( Object . prototype . hasOwnProperty . call ( s , p ) && e . indexOf ( p ) < 0 )
@@ -27,9 +62,8 @@ var __rest = (this && this.__rest) || function (s, e) {
27
62
return t ;
28
63
} ;
29
64
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
65
+ var plotly = require ( "plotly.js" ) ;
30
66
var React = require ( "react" ) ;
31
- var plotlyInstance = require ( "plotly.js/dist/plotly.js" ) ;
32
- var lodash_1 = require ( "lodash" ) ;
33
67
/***
34
68
* Usage:
35
69
* <PlotlyChart data={toJS(this.model_data)}
@@ -42,23 +76,34 @@ var PlotlyChart = /** @class */ (function (_super) {
42
76
var _this = _super !== null && _super . apply ( this , arguments ) || this ;
43
77
_this . container = null ;
44
78
_this . resize = function ( ) {
45
- plotlyInstance . Plots . resize ( _this . container ) ;
46
- } ;
47
- _this . draw = function ( props ) {
48
- var data = props . data , layout = props . layout , config = props . config ;
49
- // We clone the layout as plotly mutates it.
50
- plotlyInstance . newPlot ( _this . container , data , lodash_1 . cloneDeep ( layout ) , config ) ;
51
- _this . attachListeners ( ) ;
79
+ if ( _this . container ) {
80
+ plotly . Plots . resize ( _this . container ) ;
81
+ }
52
82
} ;
83
+ _this . draw = function ( props ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
84
+ var data , layout , config , _a ;
85
+ return __generator ( this , function ( _b ) {
86
+ switch ( _b . label ) {
87
+ case 0 :
88
+ data = props . data , layout = props . layout , config = props . config ;
89
+ if ( ! this . container ) return [ 3 /*break*/ , 2 ] ;
90
+ // plotly.react will not destroy the old plot: https://plot.ly/javascript/plotlyjs-function-reference/#plotlyreact
91
+ _a = this ;
92
+ return [ 4 /*yield*/ , plotly . react ( this . container , data , Object . assign ( { } , layout ) , config ) ] ;
93
+ case 1 :
94
+ // plotly.react will not destroy the old plot: https://plot.ly/javascript/plotlyjs-function-reference/#plotlyreact
95
+ _a . container = _b . sent ( ) ;
96
+ _b . label = 2 ;
97
+ case 2 : return [ 2 /*return*/ ] ;
98
+ }
99
+ } ) ;
100
+ } ) ; } ;
53
101
return _this ;
54
102
}
55
103
PlotlyChart . prototype . attachListeners = function ( ) {
56
104
if ( this . props . onClick ) {
57
105
this . container . on ( 'plotly_click' , this . props . onClick ) ;
58
106
}
59
- if ( this . props . onBeforeHover ) {
60
- this . container . on ( 'plotly_beforehover' , this . props . onBeforeHover ) ;
61
- }
62
107
if ( this . props . onHover ) {
63
108
this . container . on ( 'plotly_hover' , this . props . onHover ) ;
64
109
}
@@ -77,15 +122,30 @@ var PlotlyChart = /** @class */ (function (_super) {
77
122
this . draw ( this . props ) ;
78
123
} ;
79
124
PlotlyChart . prototype . componentWillUnmount = function ( ) {
80
- plotlyInstance . purge ( this . container ) ;
125
+ if ( this . container ) {
126
+ plotly . purge ( this . container ) ;
127
+ }
81
128
window . removeEventListener ( 'resize' , this . resize ) ;
82
129
} ;
83
130
PlotlyChart . prototype . render = function ( ) {
84
131
var _this = this ;
85
- var _a = this . props , data = _a . data , layout = _a . layout , config = _a . config , onClick = _a . onClick , onBeforeHover = _a . onBeforeHover , onHover = _a . onHover , onSelected = _a . onSelected , onUnHover = _a . onUnHover , other = __rest ( _a , [ "data" , "layout" , "config" , "onClick" , "onBeforeHover" , "onHover" , "onSelected" , "onUnHover" ] ) ;
86
- return React . createElement ( "div" , __assign ( { } , other , { ref : function ( node ) { return _this . container = node ; } } ) ) ;
132
+ var _a = this . props , data = _a . data , layout = _a . layout , config = _a . config , onClick = _a . onClick , onHover = _a . onHover , onSelected = _a . onSelected , onUnHover = _a . onUnHover , other = __rest ( _a , [ "data" , "layout" , "config" , "onClick" , "onHover" , "onSelected" , "onUnHover" ] ) ;
133
+ return ( React . createElement ( "div" , __assign ( { } , other , { ref : function ( node ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
134
+ var _a ;
135
+ return __generator ( this , function ( _b ) {
136
+ switch ( _b . label ) {
137
+ case 0 :
138
+ if ( ! ( node && ! this . container ) ) return [ 3 /*break*/ , 2 ] ;
139
+ _a = this ;
140
+ return [ 4 /*yield*/ , plotly . newPlot ( node , data , Object . assign ( { } , layout ) , config ) ] ;
141
+ case 1 :
142
+ _a . container = _b . sent ( ) ;
143
+ _b . label = 2 ;
144
+ case 2 : return [ 2 /*return*/ ] ;
145
+ }
146
+ } ) ;
147
+ } ) ; } } ) ) ) ;
87
148
} ;
88
149
return PlotlyChart ;
89
150
} ( React . Component ) ) ;
90
- ;
91
151
exports . default = PlotlyChart ;
0 commit comments