11import { defineComponent , Ref } from 'vue-demi'
22import { Plot as BasePlot } from '@antv/g2plot'
33import isEqual from 'lodash/isEqual'
4- import cloneDeep from 'lodash/cloneDeep'
54import { HTMLAttributes } from '@vue/runtime-dom'
65
76interface Options {
@@ -17,82 +16,82 @@ type PickedAttrs = 'class' | 'style'
1716export interface BaseChartProps < C extends Options >
1817 extends Pick < HTMLAttributes , PickedAttrs > {
1918 chart : any
20- chartRef ?: Ref < BasePlot < C > | null >
21- }
22-
23- interface ChartOptions {
2419 data : any [ ]
25- config : any
20+ chartRef ?: Ref < BasePlot < C > | null >
2621}
2722
28- interface ComputedOptions {
29- attrConfig : BaseChartRawBindings < any > & BaseChartProps < any >
23+ interface ComputedOptions < C extends Options > {
24+ attrConfig : BaseChartRawBindings < C > & BaseChartProps < C >
25+ chartData : any [ ]
26+ chartConfig : C
3027}
3128
3229export interface BaseChartRawBindings < C extends Options > {
3330 plot : BasePlot < C >
34- config : C
35- data : any [ ]
36- getChartConfig : ( ) => ChartOptions
3731}
3832
3933const BaseChart = defineComponent <
4034 BaseChartProps < any > ,
4135 BaseChartRawBindings < any > ,
42- ComputedOptions
36+ ComputedOptions < any >
4337> ( {
4438 inheritAttrs : false ,
4539 name : 'BaseChart' ,
4640 computed : {
4741 attrConfig ( ) {
4842 return this . $attrs
4943 } ,
44+ chartData ( ) {
45+ const { chart, chartRef, ...restProps } = this . attrConfig
46+ const { data } = restProps
47+ return data || [ ]
48+ } ,
49+ chartConfig ( ) {
50+ const { chart, chartRef, ...restProps } = this . attrConfig
51+ const { data, ...config } = restProps
52+ return config
53+ } ,
5054 } ,
5155 mounted ( ) {
5256 const { chart : Chart } = this . $attrs as {
5357 chart : Plot < Options >
5458 }
55- const { data, config } = this . getChartConfig ( )
56- this . config = cloneDeep ( config )
57- const normalizedData = data || [ ]
58- this . data = normalizedData
5959 this . plot = new Chart ( this . $el as HTMLElement , {
60- data : normalizedData ,
61- ...config ,
60+ data : this . chartData ,
61+ ...this . chartConfig ,
6262 } )
6363 this . plot . render ( )
6464 } ,
65- beforeUpdate ( ) {
66- const { data, config } = this . getChartConfig ( )
67- const normalizedData = data || [ ]
68- /* istanbul ignore else */
69- if ( this . plot ) {
70- if ( ! isEqual ( config , this . config ) || ! this . data . length ) {
71- this . config = cloneDeep ( config )
72- this . plot . update ( {
73- data : normalizedData ,
74- ...config ,
75- } )
76- this . plot . render ( )
77- } else {
78- this . plot . changeData ( normalizedData )
79- }
80- this . data = normalizedData
81- }
82- } ,
8365 beforeUnmount ( ) {
8466 /* istanbul ignore else */
8567 if ( this . plot ) {
8668 this . plot . destroy ( )
8769 }
8870 } ,
89- methods : {
90- getChartConfig ( ) : ChartOptions {
91- const { chart, chartRef, ...restProps } = this . attrConfig
92- const { data, ...config } = restProps
93- return {
94- data,
95- config,
71+ watch : {
72+ chartData ( data , oldData ) {
73+ /* istanbul ignore else */
74+ if ( this . plot ) {
75+ if ( ! oldData . length ) {
76+ this . plot . update ( {
77+ data : data ,
78+ ...this . chartConfig ,
79+ } )
80+ this . plot . render ( )
81+ } else {
82+ this . plot . changeData ( data )
83+ }
84+ }
85+ } ,
86+ chartConfig ( config , oldConfig ) {
87+ /* istanbul ignore else */
88+ if ( this . plot ) {
89+ if ( ! isEqual ( config , oldConfig ) ) {
90+ this . plot . update ( {
91+ data : this . chartData ,
92+ ...config ,
93+ } )
94+ }
9695 }
9796 } ,
9897 } ,
0 commit comments