@@ -21,35 +21,46 @@ const props = defineProps({
2121});
2222
2323const echartsRef = ref (null );
24+ const myChart = ref (null );
2425
2526const refresh = debounce (() => {
26- const myChart = echarts .init (echartsRef .value );
27- myChart .setOption (props .options );
28- myChart .on (' finished' , () => {
29- myChart .resize ();
30- myChart .off (' finished' );
31- });
27+ if (myChart .value ) {
28+ myChart .value .dispose ();
29+ }
30+ myChart .value = echarts .init (echartsRef .value );
31+ if (props .options ) {
32+ myChart .value .setOption (props .options );
33+ }
3234 if (props .onClick ) {
33- // myChart.on('click', props.onClick);
35+ myChart .value .on (' click' , (params ) => {
36+ props .onClick (params, myChart .value );
37+ });
3438 }
3539}, 10 );
3640
37- const resizeObserver = new ResizeObserver ((e ) => {
38- refresh ();
41+ const resizeObserver = new ResizeObserver (() => {
42+ requestAnimationFrame (() => {
43+ refresh ();
44+ });
3945});
4046
4147const currentName = ref ();
4248
4349watchEffect (() => {
4450 const options = props .options ;
4551 nextTick (() => {
46- const myChart = echarts .init (echartsRef .value );
47- myChart .setOption (props .options );
52+ if (myChart .value ) {
53+ myChart .value .dispose ();
54+ }
55+ myChart .value = echarts .init (echartsRef .value );
56+ if (props .options ) {
57+ myChart .value .setOption (props .options );
58+ }
4859
4960 if (props .onClick ) {
50- myChart .off (' click' );
51- myChart .on (' click' , (params ) => {
52- props .onClick (params, myChart);
61+ myChart .value . off (' click' );
62+ myChart .value . on (' click' , (params ) => {
63+ props .onClick (params, myChart . value );
5364 });
5465 }
5566 });
@@ -60,6 +71,9 @@ onMounted(() => {
6071});
6172
6273onUnmounted (() => {
74+ if (myChart .value ) {
75+ myChart .value .dispose ();
76+ }
6377 resizeObserver .disconnect ();
6478});
6579
0 commit comments