@@ -17,6 +17,7 @@ import {
1717 ViewLayer ,
1818 PlotConfig ,
1919 ViewConfig ,
20+ DataItem ,
2021} from '@antv/g2plot'
2122import { StateManagerContext } from '../state-manager'
2223import { RecursivePartial } from '@antv/g2plot/lib/interface/types'
@@ -52,7 +53,7 @@ interface StateManagerCfg {
5253interface ChartConfig extends PlotConfig , ViewConfig { }
5354
5455export 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
5859const 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