This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #350
- Loading branch information
Showing
5 changed files
with
1,141 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import plotly from 'plotly.js/dist/plotly'; | ||
import createPlotComponent from 'react-plotly.js/factory'; | ||
import PlotlyEditor from 'react-chart-editor'; | ||
import 'react-chart-editor/lib/react-chart-editor.css'; | ||
|
||
const Plot = createPlotComponent(plotly); | ||
|
||
export default class ChartEditor extends React.Component { | ||
static propTypes = { | ||
columnNames: PropTypes.array, | ||
rows: PropTypes.array | ||
} | ||
|
||
/** | ||
* ChartEditor displays a Plotly.js chart using the query results | ||
* | ||
* @param {object} props - Component properties | ||
* | ||
* @param {Array} props.columnNames - Array of column names | ||
* @param {Array.<Array>} props.rows - Array of rows | ||
*/ | ||
constructor(props) { | ||
super(props); | ||
|
||
this.computeDerivedProps(); | ||
|
||
/** | ||
* @member {object} state - Component state | ||
* @property {object} state.gd - Graph div | ||
* @property {number} state.revision - Revision number | ||
*/ | ||
this.state = { | ||
gd: {}, | ||
revision: 0 | ||
}; | ||
|
||
/** | ||
* Increment revision counter | ||
* @returns {undefined} | ||
*/ | ||
this.onEditorUpdate = () => { | ||
this.setState(({revision: prevRevision}) => ({revision: prevRevision + 1})); | ||
}; | ||
|
||
/** | ||
* Updates graph div | ||
* @param {object} gd - Graph div | ||
* @returns {undefined} | ||
*/ | ||
this.onPlotUpdate = (gd) => { | ||
this.setState({gd}); | ||
}; | ||
} | ||
|
||
computeDerivedProps() { | ||
/** | ||
* @member {object} dataSources - Create a data source per column (used by <PlotlyEditor>) | ||
*/ | ||
this.dataSources = {}; | ||
this.props.columnNames.forEach(name => { | ||
this.dataSources[name] = []; | ||
}); | ||
this.props.rows.forEach(row => { | ||
row.forEach((v, i) => { | ||
const columnName = this.props.columnNames[i]; | ||
this.dataSources[columnName].push(v); | ||
}); | ||
}); | ||
|
||
/** | ||
* @member {Array.<object>} dataSourceOptions - Assign a label to each data source (used by <PlotlyEditor>) | ||
*/ | ||
this.dataSourceOptions = this.props.columnNames.map(name => ({value: name, label: name})); | ||
} | ||
|
||
componentWillReceiveProps(newProps) { | ||
// Did columnNames change? | ||
const {columnNames} = this.props; | ||
const {columnNames: newColumnNames} = newProps; | ||
|
||
let isEqual = (columnNames.length === newColumnNames.length); | ||
for (let i = 0; isEqual && (i < columnNames); i++) { | ||
isEqual = isEqual && (columnNames[i] === newColumnNames[i]); | ||
} | ||
|
||
// If it did, update the derived props | ||
if (!isEqual) { | ||
this.computeDerivedProps(); | ||
} | ||
} | ||
|
||
render() { | ||
const {gd, revision} = this.state; | ||
const {data, layout} = gd; | ||
const config = {editable: true}; | ||
|
||
return ( | ||
<div> | ||
<div style={{ | ||
float: 'left', | ||
width: 466 | ||
}}> | ||
<PlotlyEditor | ||
graphDiv={gd} | ||
onUpdate={this.onEditorUpdate} | ||
dataSources={this.dataSources} | ||
dataSourceOptions={this.dataSourceOptions} | ||
plotly={plotly} | ||
/> | ||
</div> | ||
<div style={{ | ||
float: 'right', | ||
height: 400, | ||
width: 'calc(-466px + 100%)' | ||
}}> | ||
<Plot | ||
debug | ||
useResizeHandler | ||
config={config} | ||
data={data} | ||
layout={layout} | ||
onUpdate={this.onPlotUpdate} | ||
onInitialized={this.onPlotUpdate} | ||
revision={revision} | ||
style={{width: '100%', height: '100%'}} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.