11import './App.css' ;
22import 'react-plotly.js-editor/lib/react-plotly.js-editor.css' ;
33import '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' ;
95import React , { Component } from 'react' ;
106import createPlotComponent from 'react-plotly.js/factory' ;
117import ee from 'event-emitter' ;
@@ -49,6 +45,7 @@ class Backend {
4945ee ( Backend . prototype ) ;
5046
5147const backend = new Backend ( {
48+ // eslint-disable-next-line no-magic-numbers
5249 dataSources : { col1 : [ 1 , 2 , 3 ] , col2 : [ 4 , 3 , 2 ] , col3 : [ 17 , 13 , 9 ] } ,
5350 delay : 10 ,
5451} ) ;
@@ -57,38 +54,25 @@ class App extends Component {
5754 constructor ( ) {
5855 super ( ) ;
5956
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 = {
6261 data : [ { type : 'scatter' } ] ,
6362 layout : { title : 'Room readings' } ,
6463 } ;
6564
6665 // 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+ } ;
8772
8873 this . getChartingData = this . getChartingData . bind ( this ) ;
8974 this . setChartingData = this . setChartingData . bind ( this ) ;
9075 this . setChartingDataOptions = this . setChartingDataOptions . bind ( this ) ;
91- this . onEditorUpdate = this . onEditorUpdate . bind ( this ) ;
9276 }
9377
9478 componentDidMount ( ) {
@@ -98,18 +82,18 @@ class App extends Component {
9882 this . getChartingDataColumnsNames ( ) ;
9983 }
10084
101- componentDidUnmount ( ) {
85+ componentWillUnmount ( ) {
10286 backend . off ( 'ChartingDataColumnNames' , this . setChartingDataOptions ) ;
10387 backend . off ( 'ChartingData' , this . setChartingData ) ;
10488 }
10589
10690 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+ } ) ;
11397 }
11498
11599 getChartingDataColumnsNames ( ) {
@@ -127,35 +111,34 @@ class App extends Component {
127111
128112 setChartingData ( { columnName, data} ) {
129113 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} ;
136118 } ) ;
137- this . hub . figureUpdated ( ) ;
138119 }
139120 }
140121
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 ) ;
153130 }
154131 }
155132 }
156133 }
134+ }
157135
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 } ) ) ;
159142 }
160143
161144 render ( ) {
@@ -165,17 +148,18 @@ class App extends Component {
165148 locale = "en"
166149 dataSources = { this . state . dataSources }
167150 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 ) }
171154 revision = { this . state . editorRevision }
155+ plotly = { plotly }
172156 />
173157 < Plot
174158 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 ) }
179163 revision = { this . state . plotRevision }
180164 />
181165 </ div >
0 commit comments