Skip to content

Commit 9f0cdce

Browse files
committed
feat: plot 更新优化,减少非必要更新
1 parent 2709315 commit 9f0cdce

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bizcharts",
3-
"version": "4.1.15-beta.1",
3+
"version": "4.1.15",
44
"description": "bizcharts",
55
"keywords": [
66
"bizcharts",

src/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ registerEngine('svg', SVGEngine);
1717

1818
// @ts-ignore
1919
export * from '@antv/g2/lib/core';
20-
export const VERSION = '4.1.15-beta.1';
20+
export const VERSION = '4.1.15';
2121

2222

2323

src/createPlot.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
polyfillTitleEvent,
2121
polyfillDescriptionEvent,
2222
} from './plots/core/polyfill';
23-
import { debounce, isArray, isFunction, isNil, isEqual } from '@antv/util';
23+
import { debounce, isArray, isFunction, isNil } from '@antv/util';
24+
import isEqual from './utils/isEqual';
2425
import warn from 'warning';
2526

2627
// 国际化处理
@@ -100,13 +101,13 @@ class BasePlot extends React.Component<any> {
100101
}
101102
polyfillEvents(this.g2Instance, {}, this.props);
102103
this.g2Instance.data = this.props.data;
103-
this.preConfig = pickWithout(this.props, [
104+
this.preConfig = cloneDeep(pickWithout(this.props, [
104105
...REACT_PIVATE_PROPS,
105106
'container',
106107
'PlotClass',
107108
'onGetG2Instance',
108109
'data',
109-
]);
110+
]));
110111
}
111112
componentDidUpdate(prevProps) {
112113
if (this.props.children && this.g2Instance.chart) {
@@ -149,11 +150,12 @@ class BasePlot extends React.Component<any> {
149150
this.g2Instance.destroy();
150151
this.initInstance();
151152
this.g2Instance.render();
152-
} else if (this.diffConfig()) {
153-
// 对比options是否更新
154-
if (!isEqual(currentConfig, this.preConfig)) {
155-
this.g2Instance.update(currentConfig);
156-
}
153+
} else if (this.diffConfig() ) {
154+
// options更新
155+
this.g2Instance.update({
156+
...currentConfig,
157+
data: this.props.data
158+
});
157159
} else if (this.diffData()) {
158160
this.g2Instance.changeData(this.props.data);
159161
}
@@ -180,7 +182,7 @@ class BasePlot extends React.Component<any> {
180182
'onGetG2Instance',
181183
'data',
182184
]);
183-
return !shallowEqual(preConfig, currentConfig);
185+
return !isEqual(preConfig, currentConfig);
184186
}
185187
diffData() {
186188
// 只有数据更新就不重绘,其他全部直接重新创建实例。

src/utils/isEqual.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import { isObject, isArray } from '@antv/util';
3+
4+
const isEqual = (value: any, other: any): boolean => {
5+
if (isObject(value) && isObject(other)) {
6+
const valueKeys = Object.keys(value);
7+
const otherKeys = Object.keys(other);
8+
if (valueKeys.length !== otherKeys.length) {
9+
return false;
10+
}
11+
let rst = true;
12+
for (let i = 0; i < valueKeys.length; i++) {
13+
rst = isEqual(value[valueKeys[i]], other[valueKeys[i]]);
14+
if (!rst) {
15+
break;
16+
}
17+
}
18+
return rst;
19+
}
20+
if (isArray(value) && isArray(other)) {
21+
if (value.length !== other.length) {
22+
return false;
23+
}
24+
let rst = true;
25+
for (let i = 0; i < value.length; i++) {
26+
rst = isEqual(value[i], other[i]);
27+
if (!rst) {
28+
break;
29+
}
30+
}
31+
return rst;
32+
}
33+
if (value === other) {
34+
return true;
35+
}
36+
if (!value || !other) {
37+
return false;
38+
}
39+
return false;
40+
};
41+
42+
export default isEqual;

unittest/plots/_update-option-spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const Demo = () => {
3131
setState({...opt, seriesField: undefined });
3232
console.log('change')
3333
}}>click me change option seriesField</div>
34+
<div onClick={() => {
35+
setState({...opt, data: [] });
36+
console.log('change')
37+
}}>click me change option seriesField</div>
3438

3539
<BarChart
3640
{...option}

0 commit comments

Comments
 (0)