Skip to content

Commit 3897729

Browse files
committed
perf: optimize data update
1 parent f8385d2 commit 3897729

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

docs/guide/get-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ export default () => {
254254
const [restConfig, setConfig] = useState({})
255255

256256
const handleBtnClick = useCallback(() => {
257-
setConfig({
257+
setConfig((config) => ({
258258
point: {
259-
visible: false,
259+
visible: config.point ? !config.point.visible : false,
260260
},
261-
})
261+
}))
262262
}, [])
263263

264264
return (

package-lock.json

Lines changed: 15 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"postversion": "npm publish"
4848
},
4949
"devDependencies": {
50-
"@antv/g2plot": "^1.1.7",
50+
"@antv/g2plot": "^1.1.14",
5151
"@babel/cli": "^7.10.5",
5252
"@commitlint/cli": "^9.1.2",
5353
"@commitlint/config-conventional": "^9.1.1",
@@ -56,7 +56,7 @@
5656
"@opd/jest-preset-pangu": "^1.5.0",
5757
"@opd/prettier-config-pangu": "^1.2.0",
5858
"@types/jest": "^26.0.5",
59-
"@types/lodash": "^4.14.157",
59+
"@types/lodash": "^4.14.158",
6060
"@types/react": "^16.9.43",
6161
"@types/react-dom": "^16.9.8",
6262
"@types/react-test-renderer": "^16.9.2",

src/components/base/index.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ViewLayer,
1818
PlotConfig,
1919
ViewConfig,
20+
DataItem,
2021
} from '@antv/g2plot'
2122
import { StateManagerContext } from '../state-manager'
2223
import { RecursivePartial } from '@antv/g2plot/lib/interface/types'
@@ -52,7 +53,7 @@ interface StateManagerCfg {
5253
interface ChartConfig extends PlotConfig, ViewConfig {}
5354

5455
export interface Plot<C extends PlotConfig> {
55-
new (container: HTMLElement, props: C): BasePlot<C, LayerCtor<C>>
56+
new (container: HTMLElement, config: C): BasePlot<C, LayerCtor<C>>
5657
}
5758

5859
const syncRef = <C extends PlotConfig>(
@@ -92,18 +93,21 @@ const BaseChart = <C extends PlotConfig>(
9293
const containerRef = useRef<HTMLDivElement>(null)
9394
const stateManager = useContext(StateManagerContext)
9495
const isFirstRenderRef = useRef<boolean>(true)
96+
const dataRef = useRef<DataItem[]>([])
9597

9698
useImperativeHandle(ref, () => containerRef.current)
9799

98100
useEffect(() => {
99101
const { current: container } = containerRef
100102
/* istanbul ignore else */
101103
if (container) {
102-
const { data = [], ...config } = restProps as ChartConfig
104+
const { data, ...config } = restProps as ChartConfig
103105
configRef.current = cloneDeep(config)
106+
const normalizedData = data || []
107+
dataRef.current = normalizedData
104108
const mergedConfig = {
105109
...config,
106-
data,
110+
data: normalizedData,
107111
} as any
108112
chartRef.current = new Chart(container, mergedConfig)
109113
chartRef.current.render()
@@ -131,17 +135,19 @@ const BaseChart = <C extends PlotConfig>(
131135
// avoid update in first time
132136
if (!isFirstRenderRef.current) {
133137
const { data, ...config } = restProps as ChartConfig
134-
if (!isEqual(config, configRef.current)) {
138+
const normalizedData = data || []
139+
if (!isEqual(config, configRef.current) || !dataRef.current.length) {
135140
configRef.current = cloneDeep(config)
136-
chart.updateConfig(config as RecursivePartial<C>)
141+
const mergedConfig = {
142+
...config,
143+
data: normalizedData,
144+
} as RecursivePartial<C>
145+
chart.updateConfig(mergedConfig)
137146
chart.render()
138147
} else {
139-
if (data) {
140-
chart.changeData(data)
141-
} else {
142-
chart.changeData([])
143-
}
148+
chart.changeData(normalizedData)
144149
}
150+
dataRef.current = normalizedData
145151
} else {
146152
isFirstRenderRef.current = false
147153
}

0 commit comments

Comments
 (0)