From d0d4955b9ad6d90cc5e68c910a3dc14a8decb66e Mon Sep 17 00:00:00 2001 From: jianneng-fit2cloud Date: Mon, 2 Sep 2024 22:56:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9B=BE=E8=A1=A8-=E8=AF=8D=E4=BA=91?= =?UTF-8?q?=E5=9B=BE):=20=E4=BF=AE=E5=A4=8D=E5=A4=A7=E5=B0=8F=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=B8=AD=E6=9C=80=E5=A4=A7=E6=9C=80=E5=B0=8F=E5=80=BC?= =?UTF-8?q?=E9=83=BD=E4=B8=BA0=E6=97=B6=E6=97=A0=E6=B3=95=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=BB=98=E8=AE=A4=E5=80=BC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/chart/components/js/util.ts | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/core/core-frontend/src/views/chart/components/js/util.ts b/core/core-frontend/src/views/chart/components/js/util.ts index fcd478a1e7..bd58bd3048 100644 --- a/core/core-frontend/src/views/chart/components/js/util.ts +++ b/core/core-frontend/src/views/chart/components/js/util.ts @@ -630,21 +630,37 @@ export const getMaxAndMinValueByData = ( minValue: number, callback: (max: number, min: number) => void ) => { - if ((minValue === 0 && maxValue === 0) || minValue === null || maxValue === null) { + // 定义一个辅助函数来计算最大值或最小值 + const calculateExtreme = (isMax: boolean) => { + return data.reduce( + (extreme, current) => { + return isMax + ? current[field] > extreme + ? current[field] + : extreme + : current[field] < extreme + ? current[field] + : extreme + }, + isMax ? Number.MIN_SAFE_INTEGER : Number.MAX_SAFE_INTEGER + ) + } + if (minValue === null || maxValue === null) { let maxResult = maxValue let minResult = minValue if (maxResult === null) { - maxResult = data.reduce((max, current) => { - return current[field] > max ? current[field] : max - }, Number.MIN_SAFE_INTEGER) + maxResult = calculateExtreme(true) } if (minResult === null) { - minResult = data.reduce((min, current) => { - return current[field] < min ? current[field] : min - }, Number.MAX_SAFE_INTEGER) + minResult = calculateExtreme(false) } callback(maxResult, minResult) } + if (minValue === 0 && maxValue === 0) { + const maxResult = calculateExtreme(true) + const minResult = calculateExtreme(false) + callback(maxResult, minResult) + } } export const stepsColor = (start, end, steps, gamma) => {