Skip to content

Commit fcc1268

Browse files
iitodoWang Sen
andauthored
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)
* 1. 仅事件变化时只刷新echart绑定的事件;2. 如果style或者className变化,先进行echarts.resize再刷新数据更合理 * 检视意见修改 * 增加测试用例 --------- Co-authored-by: Wang Sen <13710729@qq.com>
1 parent 670d9b1 commit fcc1268

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

__tests__/charts/event-spec.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import React, { useState, useEffect } from 'react';
2+
import { act } from 'react-dom/test-utils';
3+
import ReactECharts from '../../src';
4+
import { render, destroy, createDiv, removeDom } from '../utils';
5+
6+
describe('chart', () => {
7+
it('event change', async () => {
8+
let echartsInstance;
9+
const div = createDiv();
10+
let eventParam = null;
11+
const ChartEventChange: React.FC<any> = (props) => {
12+
const option = {
13+
title : {
14+
text: '某站点用户访问来源',
15+
subtext: '纯属虚构',
16+
x:'center'
17+
},
18+
tooltip : {
19+
trigger: 'item',
20+
formatter: "{a} <br/>{b} : {c} ({d}%)"
21+
},
22+
legend: {
23+
orient: 'vertical',
24+
left: 'left',
25+
data: ['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
26+
},
27+
series : [
28+
{
29+
name: '访问来源',
30+
type: 'pie',
31+
radius : '55%',
32+
center: ['50%', '60%'],
33+
data:[
34+
{value:335, name:'直接访问'},
35+
{value:310, name:'邮件营销'},
36+
{value:234, name:'联盟广告'},
37+
{value:135, name:'视频广告'},
38+
{value:1548, name:'搜索引擎'}
39+
],
40+
itemStyle: {
41+
emphasis: {
42+
shadowBlur: 10,
43+
shadowOffsetX: 0,
44+
shadowColor: 'rgba(0, 0, 0, 0.5)'
45+
}
46+
}
47+
}
48+
]
49+
};
50+
51+
const [onEvents, setOnEvents] = useState(null);
52+
53+
useEffect(() => {
54+
setTimeout(() => {
55+
setOnEvents({
56+
mousedown: param => eventParam = param
57+
});
58+
}, 500)
59+
}, []);
60+
61+
return (
62+
<ReactECharts { ...props } option={option} onEvents={onEvents} />
63+
);
64+
};
65+
const Comp = <ChartEventChange onChartReady={echarts => (echartsInstance = echarts)} />;
66+
render(Comp, div);
67+
68+
expect(echartsInstance).toBeDefined();
69+
70+
let e = new MouseEvent('mousedown', {
71+
clientX: div.offsetLeft + div.offsetWidth / 2,
72+
clientY: div.offsetTop + div.offsetHeight / 2,
73+
bubbles: true,
74+
cancelable: false
75+
});
76+
div.querySelector('canvas').dispatchEvent(e);
77+
expect(eventParam).toBe(null);
78+
79+
await act(async () => {
80+
await new Promise(resolve => setTimeout(resolve, 600));
81+
div.querySelector('canvas').dispatchEvent(e);
82+
expect(eventParam).toBeDefined();
83+
expect(eventParam.type).toEqual('mousedown');
84+
});
85+
86+
destroy(div);
87+
removeDom(div);
88+
});
89+
});

src/core.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,23 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
5252
// 以下属性修改的时候,需要 dispose 之后再新建
5353
// 1. 切换 theme 的时候
5454
// 2. 修改 opts 的时候
55-
// 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
5655
if (
5756
!isEqual(prevProps.theme, this.props.theme) ||
58-
!isEqual(prevProps.opts, this.props.opts) ||
59-
!isEqual(prevProps.onEvents, this.props.onEvents)
57+
!isEqual(prevProps.opts, this.props.opts)
6058
) {
6159
this.dispose();
6260

6361
this.renderNewEcharts(); // 重建
6462
return;
6563
}
6664

65+
// 修改 onEvent 的时候先移除历史事件再添加
66+
const echartsInstance = this.getEchartsInstance();
67+
if (!isEqual(prevProps.onEvents, this.props.onEvents)) {
68+
this.offEvents(echartsInstance, prevProps.onEvents);
69+
this.bindEvents(echartsInstance, this.props.onEvents);
70+
}
71+
6772
// when these props are not isEqual, update echarts
6873
const pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
6974
if (!isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
@@ -179,6 +184,17 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
179184
}
180185
}
181186

187+
// off the events
188+
private offEvents(instance, events: EChartsReactProps['onEvents']) {
189+
if (!events) return;
190+
// loop and off
191+
for (const eventName in events) {
192+
if (isString(eventName)) {
193+
instance.off(eventName);
194+
}
195+
}
196+
}
197+
182198
/**
183199
* render the echarts
184200
*/

0 commit comments

Comments
 (0)