-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathPlotlyComponent.js
77 lines (66 loc) · 2.37 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
72
73
74
75
76
77
import PropTypes from 'prop-types';
import React from 'react';
import cloneDeep from 'lodash.clonedeep';
let createPlotlyComponent = (plotlyInstance) => class Plotly extends React.Component {
static propTypes = {
data: PropTypes.array,
layout: PropTypes.object,
config: PropTypes.object,
onClick: PropTypes.func,
onBeforeHover: PropTypes.func,
onHover: PropTypes.func,
onUnHover: PropTypes.func,
onSelected: PropTypes.func,
onRelayout: PropTypes.func,
};
attachListeners() {
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);
if (this.props.onRelayout) {
this.container.on('plotly_relayout', this.props.onRelayout);
}
}
shouldComponentUpdate(nextProps) {
//TODO logic for detecting change in props
return true;
}
componentDidMount() {
let {data, layout, config} = this.props;
plotlyInstance.newPlot(this.container, data, cloneDeep(layout), config); //We clone the layout as plotly mutates it.
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, data, cloneDeep(layout), config); //We clone the layout as plotly mutates it.
this.attachListeners();
}
}
componentWillUnmount() {
plotlyInstance.purge(this.container);
}
resize() {
plotlyInstance.Plots.resize(this.container);
}
render() {
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;
delete other.onRelayout;
return <div {...other} ref={(node) => this.container=node} />
}
};
export default createPlotlyComponent;