-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathPlotlyComponent.js
71 lines (61 loc) · 2.3 KB
/
PlotlyComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from 'react';
import cloneDeep from 'lodash.clonedeep';
let createPlotlyComponent = (plotlyInstance) => React.createClass({
displayName: 'Plotly',
propTypes: {
data: React.PropTypes.array,
layout: React.PropTypes.object,
config: React.PropTypes.object,
onClick: React.PropTypes.func,
onBeforeHover: React.PropTypes.func,
onHover: React.PropTypes.func,
onUnHover: React.PropTypes.func,
onSelected: React.PropTypes.func
},
attachListeners: function() {
if (this.props.onClick)
this.container.on('plotly_click', this.props.onClick);
if (this.props.onBeforeHover)
this.container.on('plotly_beforehover', this.props.onBeforeHover);
if (this.props.onHover)
this.container.on('plotly_hover', this.props.onHover);
if (this.props.onUnHover)
this.container.on('plotly_unhover', this.props.onUnHover);
if (this.props.onSelected)
this.container.on('plotly_selected', this.props.onSelected);
},
shouldComponentUpdate(nextProps) {
//TODO logic for detecting change in props
return true;
},
componentDidMount() {
let {data, layout, config} = this.props;
plotlyInstance.newPlot(this.container, cloneDeep(data), cloneDeep(layout), config); //We clone the data and layout as plotly mutates them.
this.attachListeners();
},
componentDidUpdate(prevProps) {
//TODO use minimal update for given changes
if (prevProps.data !== this.props.data || prevProps.layout !== this.props.layout || prevProps.config !== this.props.config) {
let {data, layout, config} = this.props;
plotlyInstance.newPlot(this.container, cloneDeep(data), cloneDeep(layout), config); //We clone the data and layout as plotly mutates them.
this.attachListeners();
}
},
componentWillUnmount: function() {
plotlyInstance.purge(this.container);
},
resize: function() {
plotlyInstance.Plots.resize(this.container);
},
render: function () {
let {data, layout, config, ...other } = this.props;
//Remove props that would cause React to warn for unknown props.
delete other.onClick;
delete other.onBeforeHover;
delete other.onHover;
delete other.onUnHover;
delete other.onSelected;
return <div {...other} ref={(node) => this.container=node} />
}
});
export default createPlotlyComponent;