From aeba63745762bbef0de85af750b2a6e0aeb00ff4 Mon Sep 17 00:00:00 2001 From: Shenhan11 <1670285296@qq.com> Date: Mon, 29 Dec 2025 15:34:47 +0800 Subject: [PATCH] refactor(echarts): extract initChart to eliminate code duplication Signed-off-by: Shenhan11 --- packages/web/src/components/Echarts-plus.vue | 35 ++++++-------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/packages/web/src/components/Echarts-plus.vue b/packages/web/src/components/Echarts-plus.vue index ca28c61..b16e5d8 100644 --- a/packages/web/src/components/Echarts-plus.vue +++ b/packages/web/src/components/Echarts-plus.vue @@ -21,7 +21,9 @@ const props = defineProps({ const echartsRef = ref(null); const myChart = ref(null); -const refresh = debounce(() => { +const initChart = () => { + if (!echartsRef.value) return; + if (myChart.value) { myChart.value.dispose(); } @@ -30,38 +32,23 @@ const refresh = debounce(() => { myChart.value.setOption(props.options); } if (props.onClick) { + myChart.value.off('click'); myChart.value.on('click', (params) => { props.onClick(params, myChart.value); }); } -}, 10); +}; -const resizeObserver = new ResizeObserver(() => { - requestAnimationFrame(() => { - refresh(); - }); -}); +const refresh = debounce(initChart, 10); + +const resizeObserver = new ResizeObserver(refresh); const currentName = ref(); watchEffect(() => { - const options = props.options; - nextTick(() => { - if (myChart.value) { - myChart.value.dispose(); - } - myChart.value = echarts.init(echartsRef.value); - if (props.options) { - myChart.value.setOption(props.options); - } - - if (props.onClick) { - myChart.value.off('click'); - myChart.value.on('click', (params) => { - props.onClick(params, myChart.value); - }); - } - }); + props.options; + props.onClick; + nextTick(initChart); }); onMounted(() => {