-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (86 loc) · 2.56 KB
/
index.js
File metadata and controls
100 lines (86 loc) · 2.56 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React from 'react';
import echarts from 'echarts';
import {extend} from 'util-toolkit';
let increaseId = 0,
resizeEventSetted = false,
resizeInsArr = [];
const defStyle = {
width: '100%'
};
const defGraphOption = {
series: []
};
function resizeHandler() {
resizeInsArr.forEach((rc)=> {
if (!rc.isDisposed()) {
rc.resize();
}
})
}
export default class ReactEcharts extends React.Component {
constructor(props) {
super(props);
this.domId = increaseId++;
this.chartIns = null;
this.loading = false;
this.resize = props.resize;
if (this.resize && !resizeEventSetted) {
resizeEventSetted = true;
window.addEventListener('resize', resizeHandler.bind(this));
}
}
componentWillReceiveProps(nextProps) {
this.updateDataOption(nextProps.dataSource);
this.updateGraphOption(nextProps.graphOption);
if (nextProps.loading && !this.loading) {
this.loading = true;
this.chartIns.showLoading();
} else if (!nextProps.loading && this.loading) {
this.chartIns.hideLoading();
}
return nextProps;
}
renderEchart(dom) {
if (!dom) {
return false;
}
this.chartIns = echarts.init(dom);
if (this.props.resize) {
resizeInsArr.push(this.chartIns);
}
this.updateGraphOption(this.props.graphOption);
this.updateDataOption(this.props.dataSource);
}
updateGraphOption(graphOption) {
this.chartIns.setOption(graphOption);
}
updateDataOption(dataSource) {
this.chartIns.setOption({
series: dataSource || this.props.dataSource || []
});
}
shouldComponentUpdate(nextProps) {
// render仅与style有关
return JSON.stringify(nextProps.style) !== JSON.stringify(this.props.style);
}
componentWillUnmount() {
// 清理环境
if (this.chartIns) {
this.chartIns.dispose();
resizeInsArr.some((ins, index)=> {
if (this.chartIns === ins) {
resizeInsArr.splice(index);
return true;
}
});
}
if (!resizeInsArr.length) {
resizeEventSetted = false;
window.removeEventListener('resize', resizeHandler);
}
}
render() {
const style = extend({}, defStyle, this.props.style);
return <div id={'rc-echart-' + this.domId} ref={(dom)=>this.renderEchart(dom)} style={style}></div>
}
}