From b603bc8b28893cac7651feee2967917650038d89 Mon Sep 17 00:00:00 2001 From: zhangyu Date: Mon, 14 Feb 2022 14:51:49 +0800 Subject: [PATCH] =?UTF-8?q?NEZ-1544=20fix=EF=BC=9Aaverage=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=90=8E=E6=B2=A1=E6=9C=89=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/chart/chart/legend.vue | 21 ++++++++++++++++++- .../src/components/charts/chartDataFormat.js | 4 ++-- .../src/components/common/js/tools.js | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/nezha-fronted/src/components/chart/chart/legend.vue b/nezha-fronted/src/components/chart/chart/legend.vue index 2824ccf0b..cfb4dff47 100644 --- a/nezha-fronted/src/components/chart/chart/legend.vue +++ b/nezha-fronted/src/components/chart/chart/legend.vue @@ -16,7 +16,7 @@
{{item.alias ? item.alias : item.name}}
-
{{statistics.value}}
+
{{(keepTwoDecimalFull(statistics.value))}}
@@ -194,6 +194,25 @@ export default { }) } }, + // 四舍五入保留2位小数(不够位数,则用0替补) + keepTwoDecimalFull (num) { + let result = parseFloat(num) + if (isNaN(result)) { + return '--' + } + // result = Math.round(num * 100) / 100 + // let decimal = result.toString() + // let posDecimal = decimal.indexOf('.') + // if (posDecimal < 0) { + // posDecimal = decimal.length + // decimal += '.' + // } + // while (decimal.length <= posDecimal + 2) { + // decimal += '0' + // } + result = chartDataFormat.getUnit(this.chartInfo.unit ? this.chartInfo.unit : 2).compute(result, null, -1, 2) + return result + }, getMaxValue (dataArg, chartInfo) { let maxValue = 0 let minValue = 0 diff --git a/nezha-fronted/src/components/charts/chartDataFormat.js b/nezha-fronted/src/components/charts/chartDataFormat.js index 691d58d20..b0cc702d2 100644 --- a/nezha-fronted/src/components/charts/chartDataFormat.js +++ b/nezha-fronted/src/components/charts/chartDataFormat.js @@ -18,11 +18,11 @@ function short (value, index, type = 1, dot) { } } function percent01 (value, index) { - value = parseFloat((Number(value)).toPrecision(12)) + value = parseFloat((Number(value)).toFixed(2)) return `${value} %` } function percent02 (value, index) { - value = parseFloat((Number(value) * 100).toPrecision(12)) + value = parseFloat((Number(value) * 100).toFixed(2)) return `${value} %` } function localFormat (value, index) { diff --git a/nezha-fronted/src/components/common/js/tools.js b/nezha-fronted/src/components/common/js/tools.js index f88c3beda..3c29f5d01 100644 --- a/nezha-fronted/src/components/common/js/tools.js +++ b/nezha-fronted/src/components/common/js/tools.js @@ -811,7 +811,7 @@ export function getMetricTypeValue (queryItem, type) { const max = copy.sort((x, y) => { return parseFloat(y[1]) - parseFloat(x[1]) })[0][1] return max } - case 'avg': { + case 'average': { copy = copy.map(t => parseFloat(t[1])) const sum = eval(copy.join('+')) const avg = sum / copy.length