From b5a3cfeee4f9a9e7374a64224b5b82671b3375f2 Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 5 Dec 2024 01:22:58 +0800 Subject: [PATCH] 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. --- internal/handler/metrics.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/handler/metrics.go b/internal/handler/metrics.go index ce8c757..a3e024e 100644 --- a/internal/handler/metrics.go +++ b/internal/handler/metrics.go @@ -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; } }