Skip to content

Commit c31383d

Browse files
authored
Merge pull request #58 from Shenhan11/fix/chart-fixes
fix: fix null value access and memory leaks in chart component and monitor page
2 parents 2b20696 + c339f29 commit c31383d

4 files changed

Lines changed: 50 additions & 25 deletions

File tree

packages/web/projects/vgpu/hooks/useInstantVector.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const useInstantVector = (configs, parseQuery = (query) => query, times) => {
4242
},
4343
});
4444

45-
data.value[index].data = percentData.data[0]?.values;
45+
data.value[index].data = percentData.data[0]?.values || [];
4646
}
4747
},
4848
);
@@ -67,7 +67,7 @@ const useInstantVector = (configs, parseQuery = (query) => query, times) => {
6767
},
6868
});
6969

70-
data.value[index].data = percentData.data[0]?.values;
70+
data.value[index].data = percentData.data[0]?.values || [];
7171
}
7272
},
7373
);
@@ -79,13 +79,17 @@ const useInstantVector = (configs, parseQuery = (query) => query, times) => {
7979
fetchInstantData();
8080
});
8181

82-
watch(
83-
times,
84-
() => {
85-
fetchRangeData();
86-
},
87-
// { immediate: true },
88-
);
82+
if (times) {
83+
watch(
84+
() => times.value,
85+
() => {
86+
// Only fetch data when both start and end times are available
87+
if (times?.value?.[0] && times?.value?.[1]) {
88+
fetchRangeData();
89+
}
90+
},
91+
);
92+
}
8993

9094
return data;
9195
};

packages/web/projects/vgpu/views/monitor/overview/getOptions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ export const getLineOptions = ({
477477
};
478478

479479
export const getRangeOptions = (data) => {
480+
if (!data || !data[0] || !data[0].data) {
481+
return {
482+
series: [],
483+
};
484+
}
485+
480486
return {
481487
legend: {
482488
// data: [],
@@ -487,6 +493,7 @@ export const getRangeOptions = (data) => {
487493
type: 'cross',
488494
},
489495
formatter: function (params) {
496+
if (!params || params.length === 0) return '';
490497
var res = params[0].name + '<br/>';
491498
for (var i = 0; i < params.length; i++) {
492499
res +=

packages/web/projects/vgpu/views/monitor/overview/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ const fetchRangeData = () => {
395395
query: v.query,
396396
})
397397
.then((res) => {
398-
v.data = res.data[0].values;
398+
v.data = res.data?.[0]?.values || [];
399399
});
400400
}
401401
}
@@ -407,7 +407,7 @@ const fetchRangeData = () => {
407407
query: `sum({__name__=~"alert:.*:count"})`,
408408
})
409409
.then((res) => {
410-
alarmData.value = res.data[0].values;
410+
alarmData.value = res.data?.[0]?.values || [];
411411
});
412412
413413
};

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,46 @@ const props = defineProps({
2121
});
2222
2323
const echartsRef = ref(null);
24+
const myChart = ref(null);
2425
2526
const 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
4147
const currentName = ref();
4248
4349
watchEffect(() => {
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
6273
onUnmounted(() => {
74+
if (myChart.value) {
75+
myChart.value.dispose();
76+
}
6377
resizeObserver.disconnect();
6478
});
6579

0 commit comments

Comments
 (0)