forked from hustcc/echarts-for-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecharts-for-react.js
101 lines (83 loc) · 3.38 KB
/
echarts-for-react.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
var _echarts = require('echarts');
var _echarts2 = _interopRequireDefault(_echarts);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _elementResizeEvent = require('element-resize-event');
var _elementResizeEvent2 = _interopRequireDefault(_elementResizeEvent);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var ReactEcharts = _react2['default'].createClass({
displayName: 'ReactEcharts',
propTypes: {
option: _react2['default'].PropTypes.object.isRequired,
notMerge: _react2['default'].PropTypes.bool,
lazyUpdate: _react2['default'].PropTypes.bool,
style: _react2['default'].PropTypes.object,
className: _react2['default'].PropTypes.string,
theme: _react2['default'].PropTypes.string,
onChartReady: _react2['default'].PropTypes.func,
showLoading: _react2['default'].PropTypes.bool,
onEvents: _react2['default'].PropTypes.object
},
// first add
componentDidMount: function componentDidMount() {
var echartObj = this.renderEchartDom();
var onEvents = this.props.onEvents || {};
this.bindEvents(echartObj, onEvents);
// on chart ready
if (typeof this.props.onChartReady === 'function') this.props.onChartReady(echartObj);
// on resize
(0, _elementResizeEvent2['default'])(this.refs.echartsDom, function () {
echartObj.resize();
});
},
// update
componentDidUpdate: function componentDidUpdate() {
this.renderEchartDom();
this.bindEvents(this.getEchartsInstance(), this.props.onEvents || []);
},
// remove
componentWillUnmount: function componentWillUnmount() {
_echarts2['default'].dispose(this.refs.echartsDom);
},
//bind the events
bindEvents: function bindEvents(instance, events) {
var _loop = function _loop(eventName) {
// ignore the event config which not satisfy
if (typeof eventName === 'string' && typeof events[eventName] === 'function') {
// binding event
instance.off(eventName);
instance.on(eventName, function (param) {
events[eventName](param, instance);
});
}
};
for (var eventName in events) {
_loop(eventName);
}
},
// render the dom
renderEchartDom: function renderEchartDom() {
// init the echart object
var echartObj = this.getEchartsInstance();
// set the echart option
echartObj.setOption(this.props.option, this.props.notMerge || false, this.props.lazyUpdate || false);
// set loading mask
if (this.props.showLoading) echartObj.showLoading();else echartObj.hideLoading();
return echartObj;
},
getEchartsInstance: function getEchartsInstance() {
// return the echart object
return _echarts2['default'].getInstanceByDom(this.refs.echartsDom) || _echarts2['default'].init(this.refs.echartsDom, this.props.theme);
},
render: function render() {
var style = this.props.style || {
height: '300px'
};
// for render
return _react2['default'].createElement('div', { ref: 'echartsDom',
className: this.props.className,
style: style });
}
});
module.exports = ReactEcharts;