This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.js
70 lines (58 loc) · 1.71 KB
/
base.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
import React from 'react';
import TuiChart from 'tui-chart';
const creator = {
bar: TuiChart.barChart,
column: TuiChart.columnChart,
line: TuiChart.lineChart,
area: TuiChart.areaChart,
bubble: TuiChart.bubbleChart,
scatter: TuiChart.scatterChart,
pie: TuiChart.pieChart,
combo: TuiChart.comboChart,
map: TuiChart.mapChart,
heatmap: TuiChart.heatmapChart,
treemap: TuiChart.treemapChart,
radial: TuiChart.radialChart,
boxplot: TuiChart.boxplotChart,
bullet: TuiChart.bulletChart
};
export default function(chartType) {
return class ChartFactory extends React.Component {
rootEl = React.createRef();
chartInst = null;
getRootElement() {
return this.rootEl.current;
}
getInstance() {
return this.chartInst;
}
bindEventHandlers(props, prevProps) {
Object.keys(props)
.filter((key) => /on[A-Z][a-zA-Z]+/.test(key))
.forEach((key) => {
const eventName = key[2].toLowerCase() + key.slice(3);
if(prevProps && prevProps[key] !== props[key]) {
this.chartInst.off(eventName);
}
this.chartInst.on(eventName, this.props[key]);
});
}
componentDidMount() {
const {data, options} = this.props;
this.chartInst = new creator[chartType](this.rootEl.current, data, options);
this.bindEventHandlers(this.props);
}
shouldComponentUpdate(nextProps) {
const currentData = this.props.data;
const nextData = nextProps.data;
if (currentData !== nextData) {
this.getInstance().setData(nextData);
}
this.bindEventHandlers(nextProps, this.props);
return false;
}
render() {
return <div ref={this.rootEl} />;
}
};
}