fix(metrics): improve chart management in metrics dashboard

- Added logic to destroy existing charts before creating new ones, preventing memory leaks and ensuring proper chart updates.
- Updated the currentCharts object to maintain references to the newly created charts, enhancing the management of chart instances.
- These changes improve the performance and reliability of the metrics dashboard by ensuring that charts are correctly handled during updates.
This commit is contained in:
wood chen 2024-12-05 01:22:58 +08:00
parent 660e60576d
commit b5a3cfeee4

View File

@ -700,7 +700,13 @@ var metricsTemplate = `
};
chartInstance.update();
} else {
chartInstance = new Chart(ctx, {
// 创建新图表前先销毁旧的
if (currentCharts[canvasId.replace('Chart', '')]) {
currentCharts[canvasId.replace('Chart', '')].destroy();
}
// 创建新图表
const newChart = new Chart(ctx, {
type: 'line',
data: chartData,
options: {
@ -722,6 +728,9 @@ var metricsTemplate = `
}
}
});
// 更新 currentCharts 对象
currentCharts[canvasId.replace('Chart', '')] = newChart;
}
}