Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #446, #447 issues. 1) Only refresh the events bound by echart when the event changes. 2) If the style or classname changes, it's more reasonable to refresh the data after performing charts.resize first. #444

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,41 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
// 以下属性修改的时候,需要 dispose 之后再新建
// 1. 切换 theme 的时候
// 2. 修改 opts 的时候
// 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
if (
!isEqual(prevProps.theme, this.props.theme) ||
!isEqual(prevProps.opts, this.props.opts) ||
!isEqual(prevProps.onEvents, this.props.onEvents)
!isEqual(prevProps.opts, this.props.opts)
) {
this.dispose();

this.renderNewEcharts(); // 重建
return;
}

// when thoes props isEqual, do not update echarts
const pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
if (isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
return;
// 修改 onEvent 的时候先移除历史事件再添加
const echartInstance = this.getEchartsInstance();
hustcc marked this conversation as resolved.
Show resolved Hide resolved
if (!isEqual(prevProps.onEvents, this.props.onEvents)) {
this.offEvents(echartInstance, prevProps.onEvents);
this.bindEvents(echartInstance, this.props.onEvents || {});
hustcc marked this conversation as resolved.
Show resolved Hide resolved
}

const echartsInstance = this.updateEChartsOption();
/**
* when style or class name updated, change size.
*/
if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
try {
echartsInstance.resize();
echartInstance.resize();
} catch (e) {
console.warn(e);
}
}

// when thoes props isEqual, do not update echarts
const pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
if (isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
return;
}

this.updateEChartsOption();
}

componentWillUnmount() {
Expand Down Expand Up @@ -151,6 +157,17 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
}
}

// off the events
private offEvents(instance, events: EChartsReactProps['onEvents']) {
if (!events) return;
// loop and off
for (const eventName in events) {
if (isString(eventName)) {
instance.off(eventName);
}
}
}

/**
* render the echarts
*/
Expand Down