diff --git a/nezha-fronted/src/components/charts/chart-bar-statistics.vue b/nezha-fronted/src/components/charts/chart-bar-statistics.vue index f94a9b275..ce84ed9cc 100644 --- a/nezha-fronted/src/components/charts/chart-bar-statistics.vue +++ b/nezha-fronted/src/components/charts/chart-bar-statistics.vue @@ -189,7 +189,8 @@ export default { extraCssText: 'z-index:1000;' }, series: null, - stepWidth: null + stepWidth: null, + chartDot: 2 } } }, @@ -327,7 +328,7 @@ export default { } } const maxValueCopies = this.getMaxValue(seriesItem, chartItem) - let dot = maxValueCopies.dot + let dot = this.chartDot = maxValueCopies.dot const maxValue = maxValueCopies.maxValue const copies = maxValueCopies.copies const unit = maxValueCopies.unit @@ -349,11 +350,11 @@ export default { value = Number(val).toExponential(2) } } - const flag = JSON.stringify(value).length > JSON.stringify(chartDataFormat.Interval(maxValue, copies, unit.type)).length + // const flag = JSON.stringify(value).length > JSON.stringify(chartDataFormat.Interval(maxValue, copies, unit.type)).length if (chartDataFormat.Interval(maxValue, copies, unit.type, 'min') < 1 && dot < 2) { dot = 2 } - if (flag || dot == 0) { + if (dot == 0) { dot = 1 } return unit.compute(value, index, -1, dot) @@ -754,7 +755,7 @@ export default {
${this.legend[params.dataIndex].alias}
value
-
${chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(params.value[1], null, -1, 2)}
+
${chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(params.value[1], null, -1, this.chartDot)}
diff --git a/nezha-fronted/src/components/charts/line-chart-block.vue b/nezha-fronted/src/components/charts/line-chart-block.vue index 43b60bcfd..4a897498a 100644 --- a/nezha-fronted/src/components/charts/line-chart-block.vue +++ b/nezha-fronted/src/components/charts/line-chart-block.vue @@ -314,7 +314,7 @@ export default { hasLegendOptions: false, /* legendOptions:[], */ screenLegendOptions: [], - + chartDot: 2, stepWidth: null } }, @@ -619,7 +619,7 @@ export default { dataArg[j].showAllSymbol = false } } - let dot = maxValueCopies.dot + let dot = this.chartDot = maxValueCopies.dot const copies = maxValueCopies.copies const unit = maxValueCopies.unit const oldDot = maxValueCopies.oldDot @@ -772,20 +772,26 @@ export default { minusFlag = false } const previousItem = params.find((series) => ('Previous ' + item.seriesName) === series.seriesName) - let val = item.data[1] ? parseFloat(Number(item.data[1]).toFixed(2)) : '' + let paramsDot = bus.countDecimals(item.data[1]) + if (paramsDot < self.chartDot) { + paramsDot = self.chartDot + } else if (paramsDot > 6) { + paramsDot = 6 + } + let val = item.data[1] ? parseFloat(Number(item.data[1]).toFixed(paramsDot)) : '' if (val === 0) { - val = Number(item.data[1]).toExponential(2) + val = Number(item.data[1]).toExponential(paramsDot) } sum += self.numberWithEConvent(val) str += '
' str += `
${tip ? (tip.alias ? tip.alias : tip.name) : item.seriesName}
` str += '
' - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(val, null, -1, oldDot) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(val, null, -1, paramsDot) if (previousItem) { str += '' - let previousval = parseFloat(Number(previousItem.data[1]).toFixed(2)) + let previousval = parseFloat(Number(previousItem.data[1]).toFixed(paramsDot)) if (previousval === 0) { - previousval = Number(item.data[1]).toExponential(2) + previousval = Number(item.data[1]).toExponential(paramsDot) } let minusVal = 0 if (previousval <= val) { @@ -796,7 +802,7 @@ export default { str += '-' } - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(minusVal, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(minusVal, null, -1, paramsDot) str += '' } else if (tip.alias.indexOf('Previous ') !== -1) { str += '' @@ -816,7 +822,7 @@ export default { str += self.$t('dashboard.panel.chartTotal') str += '
' str += '
' - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(sum, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(sum, null, self.chartDot) str += '
' str += '
' } @@ -926,11 +932,11 @@ export default { let chartUnit = chartInfo.unit chartUnit = chartUnit || 2 const unit = chartDataFormat.getUnit(chartUnit) - const flag = JSON.stringify(value).length > JSON.stringify(chartDataFormat.Interval(maxValue, copies, unit.type)).length + // const flag = JSON.stringify(value).length > JSON.stringify(chartDataFormat.Interval(maxValue, copies, unit.type)).length if (chartDataFormat.Interval(maxValue, copies, unit.type, 'min') < 1 && dot < 2) { dot = 2 } - if (flag || dot == 0) { + if (dot == 0) { dot = 1 } return unit.compute(value, index, -1, dot) @@ -1046,6 +1052,12 @@ export default { params.forEach((item, i) => { const tip = self.legendMagicType[item.seriesIndex] const color = self.bgColorList[item.seriesIndex] + let paramsDot = bus.countDecimals(item.data[1]) + if (paramsDot < self.chartDot) { + paramsDot = self.chartDot + } else if (paramsDot > 6) { + paramsDot = 6 + } if (i === 0) { const value = bus.computeTimezone(item.data[0]) const tData = new Date(value) @@ -1061,7 +1073,7 @@ export default { str += '
' str += `
${tip ? (tip.alias ? tip.alias : tip.name) : item.seriesName}
` str += '
' - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(val, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(val, null, -1, paramsDot) str += '
' str += '
' }) @@ -1241,7 +1253,7 @@ export default { str += '
' str += `
${tip ? (tip.alias ? tip.alias : tip.name) : item.seriesName}
` str += '
' - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(val, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(val, null, self.chartDot) if (previousItem) { str += '' let previousval = parseFloat(Number(previousItem.data[1]).toFixed(2)) @@ -1257,7 +1269,7 @@ export default { str += '-' } - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(minusVal, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(minusVal, null, self.chartDot) str += '' } else if (tip.alias.indexOf('Previous ') !== -1) { str += '' @@ -1279,7 +1291,7 @@ export default { str += '-' } - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(minusVal, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(minusVal, null, self.chartDot) str += '
' } str += '
' @@ -1291,7 +1303,7 @@ export default { str += self.$t('dashboard.panel.chartTotal') str += '' str += '
' - str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(sum, null, 2) + str += chartDataFormat.getUnit(chartInfo.unit ? chartInfo.unit : 2).compute(sum, null, self.chartDot) str += '
' str += '' } diff --git a/nezha-fronted/src/components/page/dashboard/explore/explore.vue b/nezha-fronted/src/components/page/dashboard/explore/explore.vue index cd10905f9..960f03256 100644 --- a/nezha-fronted/src/components/page/dashboard/explore/explore.vue +++ b/nezha-fronted/src/components/page/dashboard/explore/explore.vue @@ -376,6 +376,12 @@ export default { if (data) { data.forEach((result, i) => { const metrics = Object.assign({}, result.metric) + if (!Array.isArray(result.value)) { + const val = result + result = { + value: ['', val] + } + } this.$set(metrics, 'value#' + index, chartDataFormat.getUnit(this.chartUnit).compute(result.value[1], null, 2)) for (const key in metrics) { const label = { diff --git a/nezha-fronted/src/components/page/dashboard/overview/chart.vue b/nezha-fronted/src/components/page/dashboard/overview/chart.vue index efaabe119..f4af3ca00 100644 --- a/nezha-fronted/src/components/page/dashboard/overview/chart.vue +++ b/nezha-fronted/src/components/page/dashboard/overview/chart.vue @@ -53,7 +53,8 @@ export default { legendAll: [], colors: chartConfig.getBgColorList(), noData: false, - dataSize: 20 + dataSize: 20, + chartDot: 2 } }, created () { @@ -436,11 +437,18 @@ export default { str += '
' str += `
${alias || item.seriesName}:
` str += '
' - str += chartDataFormat.getUnit(this.unit).compute(val, null, 2) + let dot = bus.countDecimals(val) + // console.log(dot,val) + if (dot < this.chartDot) { + dot = this.chartDot + } else if (dot > 6) { + dot = 6 + } + str += chartDataFormat.getUnit(this.unit).compute(val, null, -1, dot) const previousItem = params.find((series) => ('Previous ' + item.seriesName) === series.seriesName) if (previousItem) { str += '' - let previousval = parseFloat(Number(previousItem.data[1]).toFixed(2)) + let previousval = parseFloat(Number(previousItem.data[1]).toFixed(dot)) if (previousval === 0) { previousval = Number(item.data[1]).toExponential(2) } @@ -453,7 +461,7 @@ export default { str += '-' } - str += chartDataFormat.getUnit(this.unit).compute(minusVal, null, 2) + str += chartDataFormat.getUnit(this.unit).compute(minusVal, null, -1, dot) str += '' } else if (alias.indexOf('Previous ') !== -1) { str += '' @@ -478,16 +486,15 @@ export default { }, defaultYAxisFormatter: function (value, index) { const maxValueCopies = this.getMaxValue(this.series, { unit: this.unit }) - const maxValue = maxValueCopies.maxValue - const copies = maxValueCopies.copies let dot = maxValueCopies.dot let chartUnit = this.unit chartUnit = chartUnit || 2 const unit = chartDataFormat.getUnit(chartUnit) - const flag = JSON.stringify(value).length > JSON.stringify(chartDataFormat.Interval(maxValue, copies, unit.type)).length - if (dot === 0 || flag) { + // const flag = JSON.stringify(value).length > JSON.stringify(chartDataFormat.Interval(maxValue, copies, unit.type)).length + if (dot === 0) { dot = 1 } + this.chartDot = dot return unit.compute(value, index, -1, dot) }, setRandomColors: function (num) { // 当线条过多,默认颜色数量不够时须使用此方法,num 颜色的数量,通常传递series的length即可 @@ -530,7 +537,7 @@ export default { chartInit () { this.chart = echarts.init(document.getElementById(this.chartId)) }, - getMaxValue (dataArg, chartInfo) { + getMaxValue (dataArg, chartInfo) { // 获取最大值 最小值 精度 以及份数 let maxValue = 0 let minValue = 0 if (chartInfo.unit && dataArg.length > 0) { diff --git a/nezha-fronted/src/libs/bus.js b/nezha-fronted/src/libs/bus.js index ee9d596f7..8e55fb1cd 100644 --- a/nezha-fronted/src/libs/bus.js +++ b/nezha-fronted/src/libs/bus.js @@ -337,6 +337,17 @@ export default new Vue({ return configTime } }, + countDecimals (value) { + if (Math.floor(value) !== value) { + const arr = value.toString().split('.') + if (Math.abs(arr[0] > 0)) { + return 2 + } + const dot = arr[1].split('0').length + 1 + return dot || 2 + } + return 2 + } }, created () { this.getDefaultDate()