Skip to content

Commit

Permalink
feat: plot 更新优化,减少非必要更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Leannechn committed Dec 6, 2021
1 parent 2709315 commit 9f0cdce
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bizcharts",
"version": "4.1.15-beta.1",
"version": "4.1.15",
"description": "bizcharts",
"keywords": [
"bizcharts",
Expand Down
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ registerEngine('svg', SVGEngine);

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



Expand Down
20 changes: 11 additions & 9 deletions src/createPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
polyfillTitleEvent,
polyfillDescriptionEvent,
} from './plots/core/polyfill';
import { debounce, isArray, isFunction, isNil, isEqual } from '@antv/util';
import { debounce, isArray, isFunction, isNil } from '@antv/util';
import isEqual from './utils/isEqual';
import warn from 'warning';

// 国际化处理
Expand Down Expand Up @@ -100,13 +101,13 @@ class BasePlot extends React.Component<any> {
}
polyfillEvents(this.g2Instance, {}, this.props);
this.g2Instance.data = this.props.data;
this.preConfig = pickWithout(this.props, [
this.preConfig = cloneDeep(pickWithout(this.props, [
...REACT_PIVATE_PROPS,
'container',
'PlotClass',
'onGetG2Instance',
'data',
]);
]));
}
componentDidUpdate(prevProps) {
if (this.props.children && this.g2Instance.chart) {
Expand Down Expand Up @@ -149,11 +150,12 @@ class BasePlot extends React.Component<any> {
this.g2Instance.destroy();
this.initInstance();
this.g2Instance.render();
} else if (this.diffConfig()) {
// 对比options是否更新
if (!isEqual(currentConfig, this.preConfig)) {
this.g2Instance.update(currentConfig);
}
} else if (this.diffConfig() ) {
// options更新
this.g2Instance.update({
...currentConfig,
data: this.props.data
});
} else if (this.diffData()) {
this.g2Instance.changeData(this.props.data);
}
Expand All @@ -180,7 +182,7 @@ class BasePlot extends React.Component<any> {
'onGetG2Instance',
'data',
]);
return !shallowEqual(preConfig, currentConfig);
return !isEqual(preConfig, currentConfig);
}
diffData() {

This comment has been minimized.

Copy link
@hugo1o1

hugo1o1 Dec 15, 2021

直接对比引用就好了,如果使用测试数据 两次数据都一样, 明明setState更改了 data, 但是图表没有重绘。

This comment has been minimized.

Copy link
@hugo1o1

hugo1o1 Dec 15, 2021

以前用的4.1.9 版本,每次组件rerender 都会重新绘制, 改到4.1.15 setData 都不会重绘了

// 只有数据更新就不重绘,其他全部直接重新创建实例。
Expand Down
42 changes: 42 additions & 0 deletions src/utils/isEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import { isObject, isArray } from '@antv/util';

const isEqual = (value: any, other: any): boolean => {
if (isObject(value) && isObject(other)) {
const valueKeys = Object.keys(value);
const otherKeys = Object.keys(other);
if (valueKeys.length !== otherKeys.length) {
return false;
}
let rst = true;
for (let i = 0; i < valueKeys.length; i++) {
rst = isEqual(value[valueKeys[i]], other[valueKeys[i]]);
if (!rst) {
break;
}
}
return rst;
}
if (isArray(value) && isArray(other)) {
if (value.length !== other.length) {
return false;
}
let rst = true;
for (let i = 0; i < value.length; i++) {
rst = isEqual(value[i], other[i]);
if (!rst) {
break;
}
}
return rst;
}
if (value === other) {
return true;
}
if (!value || !other) {
return false;
}
return false;
};

export default isEqual;
4 changes: 4 additions & 0 deletions unittest/plots/_update-option-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const Demo = () => {
setState({...opt, seriesField: undefined });
console.log('change')
}}>click me change option seriesField</div>
<div onClick={() => {
setState({...opt, data: [] });
console.log('change')
}}>click me change option seriesField</div>

<BarChart
{...option}
Expand Down

0 comments on commit 9f0cdce

Please sign in to comment.