9
9
privateDefaultProps ,
10
10
} from '../fragments/Graph.privateprops' ;
11
11
12
- const EMPTY_EXTEND_DATA = [ ] ;
12
+ const EMPTY_DATA = [ ] ;
13
13
14
14
/**
15
15
* Graph can be used to render any plotly.js-powered data visualization.
@@ -22,13 +22,19 @@ class PlotlyGraph extends Component {
22
22
super ( props ) ;
23
23
24
24
this . state = {
25
+ prependData : [ ] ,
25
26
extendData : [ ] ,
26
27
} ;
27
28
28
- this . clearExtendData = this . clearExtendData . bind ( this ) ;
29
+ this . clearState = this . clearState . bind ( this ) ;
29
30
}
30
31
31
32
componentDidMount ( ) {
33
+ if ( this . props . prependData ) {
34
+ this . setState ( {
35
+ prependData : [ this . props . prependData ] ,
36
+ } ) ;
37
+ }
32
38
if ( this . props . extendData ) {
33
39
this . setState ( {
34
40
extendData : [ this . props . extendData ] ,
@@ -38,15 +44,37 @@ class PlotlyGraph extends Component {
38
44
39
45
componentWillUnmount ( ) {
40
46
this . setState ( {
47
+ prependData : [ ] ,
41
48
extendData : [ ] ,
42
49
} ) ;
43
50
}
44
51
45
52
UNSAFE_componentWillReceiveProps ( nextProps ) {
53
+ let prependData = this . state . prependData . slice ( 0 ) ;
54
+
55
+ if ( this . props . figure !== nextProps . figure ) {
56
+ prependData = EMPTY_DATA ;
57
+ }
58
+
59
+ if (
60
+ nextProps . prependData &&
61
+ this . props . prependData !== nextProps . prependData
62
+ ) {
63
+ prependData . push ( nextProps . prependData ) ;
64
+ } else {
65
+ prependData = EMPTY_DATA ;
66
+ }
67
+
68
+ if ( prependData !== EMPTY_DATA ) {
69
+ this . setState ( {
70
+ prependData,
71
+ } ) ;
72
+ }
73
+
46
74
let extendData = this . state . extendData . slice ( 0 ) ;
47
75
48
76
if ( this . props . figure !== nextProps . figure ) {
49
- extendData = EMPTY_EXTEND_DATA ;
77
+ extendData = EMPTY_DATA ;
50
78
}
51
79
52
80
if (
@@ -55,22 +83,23 @@ class PlotlyGraph extends Component {
55
83
) {
56
84
extendData . push ( nextProps . extendData ) ;
57
85
} else {
58
- extendData = EMPTY_EXTEND_DATA ;
86
+ extendData = EMPTY_DATA ;
59
87
}
60
88
61
- if ( extendData !== EMPTY_EXTEND_DATA ) {
89
+ if ( extendData !== EMPTY_DATA ) {
62
90
this . setState ( {
63
91
extendData,
64
92
} ) ;
65
93
}
66
94
}
67
95
68
- clearExtendData ( ) {
69
- this . setState ( ( { extendData} ) => {
96
+ clearState ( dataKey ) {
97
+ this . setState ( props => {
98
+ var data = props [ dataKey ] ;
70
99
const res =
71
- extendData && extendData . length
100
+ data && data . length
72
101
? {
73
- extendData : EMPTY_EXTEND_DATA ,
102
+ [ dataKey ] : EMPTY_DATA ,
74
103
}
75
104
: undefined ;
76
105
@@ -82,8 +111,9 @@ class PlotlyGraph extends Component {
82
111
return (
83
112
< ControlledPlotlyGraph
84
113
{ ...this . props }
114
+ prependData = { this . state . prependData }
85
115
extendData = { this . state . extendData }
86
- clearExtendData = { this . clearExtendData }
116
+ clearState = { this . clearState }
87
117
/>
88
118
) ;
89
119
}
@@ -191,6 +221,18 @@ PlotlyGraph.propTypes = {
191
221
*/
192
222
extendData : PropTypes . oneOfType ( [ PropTypes . array , PropTypes . object ] ) ,
193
223
224
+ /**
225
+ * Data that should be prepended to existing traces. Has the form
226
+ * `[updateData, traceIndices, maxPoints]`, where `updateData` is an object
227
+ * containing the data to prepend, `traceIndices` (optional) is an array of
228
+ * trace indices that should be prepended, and `maxPoints` (optional) is
229
+ * either an integer defining the maximum number of points allowed or an
230
+ * object with key:value pairs matching `updateData`
231
+ * Reference the Plotly.prependTraces API for full usage:
232
+ * https://plotly.com/javascript/plotlyjs-function-reference/#plotlyprependtraces
233
+ */
234
+ prependData : PropTypes . oneOfType ( [ PropTypes . array , PropTypes . object ] ) ,
235
+
194
236
/**
195
237
* Data from latest restyle event which occurs
196
238
* when the user toggles a legend item, changes
@@ -523,6 +565,7 @@ PlotlyGraph.defaultProps = {
523
565
hoverData : null ,
524
566
selectedData : null ,
525
567
relayoutData : null ,
568
+ prependData : null ,
526
569
extendData : null ,
527
570
restyleData : null ,
528
571
figure : {
0 commit comments