Skip to content

Commit aeba637

Browse files
committed
refactor(echarts): extract initChart to eliminate code duplication
Signed-off-by: Shenhan11 <han.shen@dynamia.ai>
1 parent 15a7507 commit aeba637

1 file changed

Lines changed: 11 additions & 24 deletions

File tree

‎packages/web/src/components/Echarts-plus.vue‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const props = defineProps({
2121
const echartsRef = ref(null);
2222
const myChart = ref(null);
2323
24-
const refresh = debounce(() => {
24+
const initChart = () => {
25+
if (!echartsRef.value) return;
26+
2527
if (myChart.value) {
2628
myChart.value.dispose();
2729
}
@@ -30,38 +32,23 @@ const refresh = debounce(() => {
3032
myChart.value.setOption(props.options);
3133
}
3234
if (props.onClick) {
35+
myChart.value.off('click');
3336
myChart.value.on('click', (params) => {
3437
props.onClick(params, myChart.value);
3538
});
3639
}
37-
}, 10);
40+
};
3841
39-
const resizeObserver = new ResizeObserver(() => {
40-
requestAnimationFrame(() => {
41-
refresh();
42-
});
43-
});
42+
const refresh = debounce(initChart, 10);
43+
44+
const resizeObserver = new ResizeObserver(refresh);
4445
4546
const currentName = ref();
4647
4748
watchEffect(() => {
48-
const options = props.options;
49-
nextTick(() => {
50-
if (myChart.value) {
51-
myChart.value.dispose();
52-
}
53-
myChart.value = echarts.init(echartsRef.value);
54-
if (props.options) {
55-
myChart.value.setOption(props.options);
56-
}
57-
58-
if (props.onClick) {
59-
myChart.value.off('click');
60-
myChart.value.on('click', (params) => {
61-
props.onClick(params, myChart.value);
62-
});
63-
}
64-
});
49+
props.options;
50+
props.onClick;
51+
nextTick(initChart);
6552
});
6653
6754
onMounted(() => {

0 commit comments

Comments
 (0)