1
1
import './App.css' ;
2
2
import 'react-plotly.js-editor/lib/react-plotly.js-editor.css' ;
3
3
import 'react-select/dist/react-select.css' ;
4
- import PlotlyEditor , {
5
- EDITOR_ACTIONS ,
6
- Hub ,
7
- dereference ,
8
- } from 'react-plotly.js-editor' ;
4
+ import PlotlyEditor , { dereference } from 'react-plotly.js-editor' ;
9
5
import React , { Component } from 'react' ;
10
6
import createPlotComponent from 'react-plotly.js/factory' ;
11
7
import ee from 'event-emitter' ;
@@ -49,6 +45,7 @@ class Backend {
49
45
ee ( Backend . prototype ) ;
50
46
51
47
const backend = new Backend ( {
48
+ // eslint-disable-next-line no-magic-numbers
52
49
dataSources : { col1 : [ 1 , 2 , 3 ] , col2 : [ 4 , 3 , 2 ] , col3 : [ 17 , 13 , 9 ] } ,
53
50
delay : 10 ,
54
51
} ) ;
@@ -57,38 +54,25 @@ class App extends Component {
57
54
constructor ( ) {
58
55
super ( ) ;
59
56
60
- // A basic starting plotly.js figure object. Instead of assigning
61
- const figure = {
57
+ // This object is passed to Plotly.js, which then causes it to be
58
+ // overwritten with a full DOM node that contains data, layout, _fullData,
59
+ // _fullLayout etc in handlePlotUpdate()
60
+ const graphDiv = {
62
61
data : [ { type : 'scatter' } ] ,
63
62
layout : { title : 'Room readings' } ,
64
63
} ;
65
64
66
65
// Store the figure, dataSource and dataSourceOptions in state.
67
- this . state = { figure, dataSources : { } } ;
68
-
69
- // Instantiate a Hub object. The hub is a convenience module that updates
70
- // the applies Editor updates to the figure object. After an update it
71
- // will call the onUpdate function with the editor and plot revision numbers.
72
- // We set these in our state and pass them to react-plotly.js-editor and
73
- // react-plotly.js plot component respectively. This is necessary because
74
- // the plotly.js library will mutate the figure object with user interactions.
75
- // The hub listens for events from plotly.js and alerts us to the mutation here.
76
- // The Editor follows the same mutation pattern (for good or ill) and the Hub
77
- // will pick up editor results in the same way.
78
- this . hub = new Hub ( {
79
- debug : true ,
80
- onUpdate : ( { editorRevision, plotRevision} ) =>
81
- this . setState ( prevState => ( {
82
- ...prevState ,
83
- editorRevision,
84
- plotRevision,
85
- } ) ) ,
86
- } ) ;
66
+ this . state = {
67
+ graphDiv,
68
+ editorRevision : 0 ,
69
+ plotRevision : 0 ,
70
+ dataSources : { } ,
71
+ } ;
87
72
88
73
this . getChartingData = this . getChartingData . bind ( this ) ;
89
74
this . setChartingData = this . setChartingData . bind ( this ) ;
90
75
this . setChartingDataOptions = this . setChartingDataOptions . bind ( this ) ;
91
- this . onEditorUpdate = this . onEditorUpdate . bind ( this ) ;
92
76
}
93
77
94
78
componentDidMount ( ) {
@@ -98,18 +82,18 @@ class App extends Component {
98
82
this . getChartingDataColumnsNames ( ) ;
99
83
}
100
84
101
- componentDidUnmount ( ) {
85
+ componentWillUnmount ( ) {
102
86
backend . off ( 'ChartingDataColumnNames' , this . setChartingDataOptions ) ;
103
87
backend . off ( 'ChartingData' , this . setChartingData ) ;
104
88
}
105
89
106
90
setChartingDataOptions ( columnNames ) {
107
- const dataSourceOptions = columnNames . map ( name => ( {
108
- value : name ,
109
- label : name ,
110
- } ) ) ;
111
- this . setState ( prevState => ( { ... prevState , dataSourceOptions } ) ) ;
112
- this . hub . editorSourcesUpdated ( ) ;
91
+ this . setState ( {
92
+ dataSourceOptions : columnNames . map ( name => ( {
93
+ value : name ,
94
+ label : name ,
95
+ } ) ) ,
96
+ } ) ;
113
97
}
114
98
115
99
getChartingDataColumnsNames ( ) {
@@ -127,35 +111,34 @@ class App extends Component {
127
111
128
112
setChartingData ( { columnName, data} ) {
129
113
if ( Array . isArray ( data ) && data . length ) {
130
- const { dataSources, ...otherState } = this . state ;
131
- dataSources [ columnName ] = data ;
132
- dereference ( this . state . figure . data , dataSources ) ;
133
- this . setState ( {
134
- dataSources,
135
- ...otherState ,
114
+ this . setState ( ( { dataSources, graphDiv} ) => {
115
+ const newDataSources = { ...dataSources , [ columnName ] : data } ;
116
+ dereference ( graphDiv . data , newDataSources ) ;
117
+ return { dataSources : newDataSources , graphDiv} ;
136
118
} ) ;
137
- this . hub . figureUpdated ( ) ;
138
119
}
139
120
}
140
121
141
- onEditorUpdate ( event ) {
142
- const { type, payload} = event ;
143
- if ( type === EDITOR_ACTIONS . UPDATE_TRACES ) {
144
- const { update} = payload ;
145
- if ( update ) {
146
- for ( const key in update ) {
147
- if ( key . substr ( key . length - 3 ) === 'src' ) {
148
- const columnId = update [ key ] ;
149
- const data = this . state . dataSources [ columnId ] ;
150
- if ( ! Array . isArray ( data ) . length || ! data . length ) {
151
- this . getChartingData ( columnId ) ;
152
- }
122
+ handleEditorUpdateTraces ( { update} ) {
123
+ if ( update ) {
124
+ for ( const key in update ) {
125
+ if ( key . substr ( key . length - 3 ) === 'src' ) {
126
+ const columnId = update [ key ] ;
127
+ const data = this . state . dataSources [ columnId ] ;
128
+ if ( ! Array . isArray ( data ) . length || ! data . length ) {
129
+ this . getChartingData ( columnId ) ;
153
130
}
154
131
}
155
132
}
156
133
}
134
+ }
157
135
158
- this . hub . handleEditorUpdate ( event ) ;
136
+ handlePlotUpdate ( graphDiv ) {
137
+ this . setState ( ( { editorRevision : x } ) => ( { editorRevision : x + 1 , graphDiv} ) ) ;
138
+ }
139
+
140
+ handleEditorUpdate ( ) {
141
+ this . setState ( ( { plotRevision : x } ) => ( { plotRevision : x + 1 } ) ) ;
159
142
}
160
143
161
144
render ( ) {
@@ -165,17 +148,18 @@ class App extends Component {
165
148
locale = "en"
166
149
dataSources = { this . state . dataSources }
167
150
dataSourceOptions = { this . state . dataSourceOptions }
168
- graphDiv = { this . hub . graphDiv }
169
- onUpdate = { this . onEditorUpdate }
170
- plotly = { plotly }
151
+ graphDiv = { this . state . graphDiv }
152
+ onUpdate = { this . handleEditorUpdate . bind ( this ) }
153
+ onUpdateTraces = { this . handleEditorUpdateTraces . bind ( this ) }
171
154
revision = { this . state . editorRevision }
155
+ plotly = { plotly }
172
156
/>
173
157
< Plot
174
158
debug
175
- data = { this . state . figure . data }
176
- layout = { this . state . figure . layout }
177
- onUpdate = { this . hub . handlePlotUpdate }
178
- onInitialized = { this . hub . handlePlotInitialized }
159
+ data = { this . state . graphDiv . data }
160
+ layout = { this . state . graphDiv . layout }
161
+ onUpdate = { this . handlePlotUpdate . bind ( this ) }
162
+ onInitialized = { this . handlePlotUpdate . bind ( this ) }
179
163
revision = { this . state . plotRevision }
180
164
/>
181
165
</ div >
0 commit comments