This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nezha-fronted/nezha-fronted/src/components/charts/chartDataFormat.js

796 lines
22 KiB
JavaScript
Raw Normal View History

/*
* value:yAxis 默认第一个参数 传递的数值
* index:yAxis 默认第二个参数 y轴的位置
* type:自定义参数用于区分是y轴调用还是tooltip调用,以设置不同精度 type =1 y轴调用 type=2 tooltip调用
* */
2021-03-19 18:52:19 +08:00
import bus from '../../libs/bus'
2021-03-19 18:52:19 +08:00
function none (value, index) {
return keepDoubleNumber(value)
}
2021-03-19 18:52:19 +08:00
function short (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, [' ', 'K', 'Mil', 'Bil', 'Til', 'Quadrillion', 'Quintillion'], 0)
} else if (type == -1) {
return asciiCompute(value, 1000, [' ', 'K', 'Mil', 'Bil', 'Til', 'Quadrillion', 'Quintillion'], dot)
} else {
return asciiCompute(value, 1000, [' ', 'K', 'Mil', 'Bil', 'Til', 'Quadrillion', 'Quintillion'], 2)
}
}
2021-03-19 18:52:19 +08:00
function percent01 (value, index) {
value = parseFloat((Number(value)).toFixed(2))
2021-03-19 18:52:19 +08:00
return `${value} %`
}
2021-03-19 18:52:19 +08:00
function percent02 (value, index) {
value = parseFloat((Number(value) * 100).toFixed(2))
2021-03-19 18:52:19 +08:00
return `${value} %`
}
2021-03-19 18:52:19 +08:00
function localFormat (value, index) {
let num = (value || 0).toString()
let result = ''
while (num.length > 3) {
2021-03-19 18:52:19 +08:00
result = ',' + num.slice(-3) + result
num = num.slice(0, num.length - 3)
}
if (num) { result = num + result }
return result
}
function bits (value, index, type = 1, dot = 0) {
if (type == 1) {
return asciiCompute(value, 1024, ['b', 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['b', 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['b', 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 2)
}
}
function bytes (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1024, ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 2)
}
}
function kilobytes (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1024, ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 2)
}
}
function megabytes (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1024, ['MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 2)
}
}
function gigabytes (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1024, ['GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 2)
}
}
function terabytes (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1024, ['TB', 'PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['TB', 'PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['TB', 'PB', 'EB', 'ZB', 'YB'], 2)
}
}
function petabytes (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1024, ['PB', 'EB', 'ZB', 'YB'], 0)
} else if (type == -1) {
return asciiCompute(value, 1024, ['PB', 'EB', 'ZB', 'YB'], dot)
} else {
return asciiCompute(value, 1024, ['PB', 'EB', 'ZB', 'YB'], 2)
}
}
function packetsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['pps', 'Kpps', 'Mpps', 'Gpps', 'Tpps', 'Ppps', 'Epps', 'Zpps', 'Ypps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['pps', 'Kpps', 'Mpps', 'Gpps', 'Tpps', 'Ppps', 'Epps', 'Zpps', 'Ypps'], dot)
} else {
return asciiCompute(value, 1000, ['pps', 'Kpps', 'Mpps', 'Gpps', 'Tpps', 'Ppps', 'Epps', 'Zpps', 'Ypps'], 2)
}
}
function bitsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Epps', 'Zpps', 'Ypps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Epps', 'Zpps', 'Ypps'], dot)
} else {
return asciiCompute(value, 1000, ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Epps', 'Zpps', 'Ypps'], 2)
}
}
function bytesSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Bs', 'KBs', 'MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Bs', 'KBs', 'MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], dot)
} else {
return asciiCompute(value, 1000, ['Bs', 'KBs', 'MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 2)
}
}
function kilobytesSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['KBs', 'MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['KBs', 'MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], dot)
} else {
return asciiCompute(value, 1000, ['KBs', 'MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 1)
}
}
function kilobitsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], dot)
} else {
return asciiCompute(value, 1000, ['Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 2)
}
}
function megabytesSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], dot)
} else {
return asciiCompute(value, 1000, ['MBs', 'GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 2)
}
}
function megabitsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], dot)
} else {
return asciiCompute(value, 1000, ['Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 2)
}
}
function gigabytesSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], dot)
} else {
return asciiCompute(value, 1000, ['GBs', 'TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 2)
}
}
function gigabitsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], dot)
} else {
return asciiCompute(value, 1000, ['Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 2)
}
}
function terabytesSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], dot)
} else {
return asciiCompute(value, 1000, ['TBs', 'PBs', 'EBs', 'ZBs', 'YBs'], 2)
}
}
function terabitsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], dot)
} else {
return asciiCompute(value, 1000, ['Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'], 2)
}
}
function petabytesSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['PBs', 'EBs', 'ZBs', 'YBs'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['PBs', 'EBs', 'ZBs', 'YBs'], dot)
} else {
return asciiCompute(value, 1000, ['PBs', 'EBs', 'ZBs', 'YBs'], 2)
}
}
function petabitsSec (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Pbps', 'Ebps', 'Zbps', 'Ybps'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Pbps', 'Ebps', 'Zbps', 'Ybps'], dot)
} else {
return asciiCompute(value, 1000, ['Pbps', 'Ebps', 'Zbps', 'Ybps'], 2)
}
}
function Hertz (value, index, type = 1, dot) {
if (type == 1) {
return asciiCompute(value, 1000, ['Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz', 'EHz', 'ZHz', 'YHz'], 1)
} else if (type == -1) {
return asciiCompute(value, 1000, ['Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz', 'EHz', 'ZHz', 'YHz'], dot)
} else {
return asciiCompute(value, 1000, ['Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz', 'EHz', 'ZHz', 'YHz'], 2)
}
}
function nanoseconds (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 'ns')
} else if (type == -1) {
return timeCompute(value, 'ns', dot)
} else {
return timeCompute(value, 'ns', 2)
}
}
function microseconds (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 'us')
} else if (type == -1) {
return timeCompute(value, 'us', dot)
} else {
return timeCompute(value, 'us', 2)
}
}
function milliseconds (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 'ms')
} else if (type == -1) {
return timeCompute(value, 'ms', dot)
} else {
return timeCompute(value, 'ms', 2)
}
}
function seconds (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 's')
} else if (type == -1) {
return timeCompute(value, 's', dot)
} else {
return timeCompute(value, 's', 2)
}
}
function minutes (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 'm')
} else if (type == -1) {
return timeCompute(value, 'm', dot)
} else {
return timeCompute(value, 'm', 2)
}
}
function hours (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 'h')
} else if (type == -1) {
return timeCompute(value, 'h', dot)
} else {
return timeCompute(value, 'h', 2)
}
}
function days (value, index, type = 1, dot) {
if (type == 1) {
return timeCompute(value, 'day')
} else if (type == -1) {
return timeCompute(value, 'day', dot)
} else {
return timeCompute(value, 'day', 3)
}
}
/*
* 一般数值格式化方法
* num: 需要格式化的值
* ascii:进制比如数据为1024
* units单位列表
* dot保留的小数位
* */
2021-03-19 18:52:19 +08:00
function asciiCompute (num, ascii, units, dot = 2) {
if (!num && num !== 0 && num !== '0') {
2020-09-29 14:05:18 +08:00
return ''
}
2021-03-19 18:52:19 +08:00
num = Number(num)
let carry = 0
if (num > 1) {
const log = Math.log(num) / Math.log(ascii)
carry = parseInt(log)
2021-03-19 18:52:19 +08:00
num = num / Math.pow(ascii, carry)
}
if (Number.isInteger(num)) {
return num + ' ' + units[carry]
} else {
return num.toFixed(dot) + ' ' + units[carry]
2021-03-19 18:52:19 +08:00
}
}
// eslint-disable-next-line no-unused-vars
function asciiCompute2 (num, ascii, units, dot = 2, unitIndex = 0) {
num = Number(num)
const quotient = num / ascii
if (unitIndex <= units.length - 1) {
if (quotient < 1) { // 不足以进位
const toFixed = parseFloat(num.toFixed(dot))
if (toFixed == 0) {
return `${num} ${units[unitIndex]}`
2021-03-19 18:52:19 +08:00
} else {
return `${num.toFixed(dot)} ${units[unitIndex]}`
}
2021-03-19 18:52:19 +08:00
} else if (quotient >= 1 && quotient < 10) { // 可以进位,但是又不足以更进一位
if (unitIndex >= units.length - 1) {
unitIndex++
return asciiCompute(num, ascii, units, dot, unitIndex)
} else {
unitIndex++
return `${quotient.toFixed(dot)} ${units[unitIndex]}`
}
2021-03-19 18:52:19 +08:00
} else { // 可以更进一位
if (unitIndex >= units.length - 1) {
unitIndex++
return asciiCompute(num, ascii, units, dot, unitIndex)
} else {
unitIndex++
num = quotient
return asciiCompute(num, ascii, units, dot, unitIndex)
}
}
2021-03-19 18:52:19 +08:00
} else {
return `${num.toFixed(2)} ${units[units.length - 1]}`
}
}
/*
* 时间格式化方法
* value:需要格式化的数值
* unit:设置的单位
* */
2021-03-19 18:52:19 +08:00
function timeCompute (value, unit, dot = 0) {
if (unit == 'year') {
return `${value.toFixed(dot)} ${unit}`
}
const units = [
{ unit: 'ns', ascii: 1 },
{ unit: 'us', ascii: 1000 },
{ unit: 'ms', ascii: 1000 },
{ unit: 's', ascii: 60 },
{ unit: 'm', ascii: 60 },
{ unit: 'h', ascii: 24 },
{ unit: 'day', ascii: 7 },
{ unit: 'week', ascii: 52 },
{ unit: 'year', ascii: '' }
]
2021-03-19 18:52:19 +08:00
for (let i in units) {
const u = units[i]
if (u.unit == unit) { // 找到最小单位
const result = (time, minUnit) => {
if (minUnit.unit == 'year') {
if (Number.isInteger(time)) {
dot = 0
2020-10-16 17:02:16 +08:00
}
return `${time.toFixed(dot)} ${minUnit.unit}`
}
2021-03-19 18:52:19 +08:00
const quotient = time / minUnit.ascii
if (quotient < 1) {
if (Number.isInteger(time)) {
dot = 0
2020-10-16 17:02:16 +08:00
}
2021-03-19 18:52:19 +08:00
return `${Number(time).toFixed(dot)} ${minUnit.unit}`
} else {
minUnit = units[++i]
return result(quotient, minUnit)
}
}
2021-03-19 18:52:19 +08:00
return result(value, u)
}
}
}
// 保留两位有效数字
function keepDoubleNumber (num) {
const decimal = num.toString()
let returnNum = ''
const arr = decimal.split('.')
if (arr.length > 1) {
returnNum = arr[0] + '.'
let validNum = 0
arr[1].split('').forEach(item => {
if (validNum < 1 && item > 0) {
validNum++
returnNum += item
} else if (validNum === 0) {
returnNum += item
} else if (validNum === 1) {
validNum++
returnNum += item
}
})
if (validNum === 1) {
returnNum += '0'
}
return returnNum
} else {
return decimal
}
}
2021-03-19 18:52:19 +08:00
function timeFormat34 (value) {
2021-12-31 16:41:30 +08:00
return bus.timeFormate(parseInt(value), 'YYYY-MM-DD HH:mm:ss')
}
2021-03-19 18:52:19 +08:00
function timeFormat35 (value) {
return bus.timeFormate(parseInt(value), 'MM/DD/yyyy h:mm:ss a')
}
2021-03-19 18:52:19 +08:00
// unit转化配置信息
/*
* value:传递给数据库的值
* label下拉选显示的名字
* compute用于格式化计算的方法返回一个string类型的串
* */
2021-03-19 18:52:19 +08:00
const unitOptions = [
{ // Misc start
value: 'Misc',
label: 'Misc',
children: [
{
value: 1,
compute: none,
label: 'none'
},
{
2021-03-19 18:52:19 +08:00
value: 2,
compute: short,
label: 'short'
},
{
2021-03-19 18:52:19 +08:00
value: 3,
compute: percent01,
label: 'percent(0-100)'
},
{
2021-03-19 18:52:19 +08:00
value: 4,
compute: percent02,
label: 'percent(0.0-1.0)'
},
{
2021-03-19 18:52:19 +08:00
value: 5,
compute: localFormat,
label: 'local format'
}
]
2021-03-19 18:52:19 +08:00
}, // Misc end
{ // Data start
value: 'Data',
label: 'Data',
children: [
{
2021-03-19 18:52:19 +08:00
value: 6,
compute: bits,
label: 'bits'
},
{
2021-03-19 18:52:19 +08:00
value: 7,
compute: bytes,
label: 'bytes'
},
{
2021-03-19 18:52:19 +08:00
value: 8,
compute: kilobytes,
label: 'kilobytes'
},
{
2021-03-19 18:52:19 +08:00
value: 9,
compute: megabytes,
label: 'megabytes'
},
{
2021-03-19 18:52:19 +08:00
value: 10,
compute: gigabytes,
label: 'gigabytes'
},
{
2021-03-19 18:52:19 +08:00
value: 11,
compute: terabytes,
label: 'terabytes'
},
{
2021-03-19 18:52:19 +08:00
value: 12,
compute: petabytes,
label: 'petabytes'
}
]
2021-03-19 18:52:19 +08:00
}, // Data end
{ // DataRate start
value: 'DataRate',
label: 'DataRate',
children: [
{
2021-03-19 18:52:19 +08:00
value: 13,
compute: packetsSec,
label: 'packets/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 14,
compute: bitsSec,
label: 'bits/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 15,
compute: bytesSec,
label: 'bytes/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 16,
compute: kilobytesSec,
label: 'kilobytes/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 17,
compute: kilobitsSec,
label: 'kilobits/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 18,
compute: megabytesSec,
label: 'megabytes/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 19,
compute: megabitsSec,
label: 'megabits/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 20,
compute: gigabytesSec,
label: 'gigabytes/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 21,
compute: gigabitsSec,
label: 'gigabits/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 22,
compute: terabytesSec,
label: 'terabytes/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 23,
compute: terabitsSec,
label: 'terabits/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 24,
compute: petabytesSec,
label: 'petabytes/sec'
},
{
2021-03-19 18:52:19 +08:00
value: 25,
compute: petabitsSec,
label: 'petabits/sec'
}
]
2021-03-19 18:52:19 +08:00
}, // DataRate end
{ // Time start
value: 'Time',
label: 'Time',
children: [
{
2021-03-19 18:52:19 +08:00
value: 26,
compute: Hertz,
label: 'Hertz (1/s)',
ascii: 1
},
{
2021-03-19 18:52:19 +08:00
value: 27,
compute: nanoseconds,
label: 'nanoseconds (ns)',
ascii: 1000
},
{
2021-03-19 18:52:19 +08:00
value: 28,
compute: microseconds,
label: 'microseconds (us)',
ascii: 1000
},
{
2021-03-19 18:52:19 +08:00
value: 29,
compute: milliseconds,
label: 'milliseconds (ms)',
ascii: 60
},
{
2021-03-19 18:52:19 +08:00
value: 30,
compute: seconds,
label: 'seconds (s)',
ascii: 60
},
{
2021-03-19 18:52:19 +08:00
value: 31,
compute: minutes,
label: 'minutes (m)',
ascii: 60
},
{
2021-03-19 18:52:19 +08:00
value: 32,
compute: hours,
label: 'hours (h)',
ascii: 24
},
{
2021-03-19 18:52:19 +08:00
value: 33,
compute: days,
label: 'days (d)',
ascii: 7
}
]
2021-03-19 18:52:19 +08:00
}, // Time end
{
2021-03-19 18:52:19 +08:00
value: 'Date&Time',
label: 'Date & Time',
children: [
{
2021-03-19 18:52:19 +08:00
value: 34,
compute: timeFormat34,
2021-03-19 18:52:19 +08:00
label: 'YYYY-MM-DD HH:mm:ss'
},
{
2021-03-19 18:52:19 +08:00
value: 35,
compute: timeFormat35,
2021-03-19 18:52:19 +08:00
label: 'MM/DD/YYYY h:mm:ss a'
}
]
}
]
2021-03-19 18:52:19 +08:00
const units = []
window.onload = function () {
if (units.length < 1) {
unitOptions.forEach((item, index) => {
item.children.forEach((n, i) => {
units.push({ ...n, type: item.label })
})
})
}
}
export default {
2021-03-19 18:52:19 +08:00
unitOptions: function () {
return unitOptions
},
2021-03-19 18:52:19 +08:00
getUnit: function (index) {
if (units.length < 1) {
unitOptions.forEach((item, index) => {
item.children.forEach((n, i) => {
units.push({ ...n, type: item.label })
})
})
}
2021-03-19 18:52:19 +08:00
return units[index - 1]
},
2021-03-19 18:52:19 +08:00
formatData: function (value, unit) {
return this.getUnit(unit).compute(value, null, 2)
2020-09-22 09:41:17 +08:00
},
2021-03-19 18:52:19 +08:00
formatDatas: function (value, type, flow = 'ceil', ascii) {
let pow = 0
if (value < 1 && value != 0) {
while (value < 1) {
pow++
value = value * 10
2020-09-23 10:03:00 +08:00
}
2021-03-19 18:52:19 +08:00
return Math.ceil(value + 1) / Math.pow(10, pow)
2020-09-23 10:03:00 +08:00
}
2021-03-19 18:52:19 +08:00
if (type === 'Time') {
return value
2020-09-22 09:41:17 +08:00
}
2021-03-19 18:52:19 +08:00
if (type === 'Data') {
if (value > 1) {
while (value > 1024) {
pow++
value = value / 1024
}
2021-03-19 18:52:19 +08:00
if (flow === 'ceil') {
const length = JSON.stringify(Math.ceil(value)).length
value = value / Math.pow(10, length - 1)
return Math.ceil(value) * Math.pow(1024, pow) * Math.pow(10, length - 1)
} else if (flow === 'floor') {
const length = JSON.stringify(Math.floor(value)).length
value = value / Math.pow(10, length - 1)
return Math.floor(value) * Math.pow(1024, pow) * Math.pow(10, length - 1)
}
} else {
return value
}
2020-09-22 09:41:17 +08:00
}
2021-03-19 18:52:19 +08:00
if (type === 'Misc' || type === 'DataRate') {
if (value > 1) {
while (value > 1000) {
pow++
value = value / 1000
2020-09-22 09:41:17 +08:00
}
2021-03-19 18:52:19 +08:00
if (flow === 'ceil') {
const length = JSON.stringify(Math.ceil(value)).length
value = value / Math.pow(10, length - 1)
return Math.ceil(value) * Math.pow(1000, pow) * Math.pow(10, length - 1)
} else if (flow === 'floor') {
const length = JSON.stringify(Math.floor(value)).length
value = value / Math.pow(10, length - 1)
return Math.floor(value) * Math.pow(1000, pow) * Math.pow(10, length - 1)
2020-09-22 09:41:17 +08:00
}
2021-03-19 18:52:19 +08:00
} else {
2020-09-22 09:41:17 +08:00
return value
}
}
2021-03-19 18:52:19 +08:00
return value
2020-09-22 09:41:17 +08:00
},
2021-03-19 18:52:19 +08:00
copies: function (value) {
switch (parseInt(value)) {
case 1: return 5
case 2: return 4
case 3: return 3
case 4: return 4
case 5: return 5
case 6: return 3
case 7: return 7
case 8: return 4
case 9: return 3
case 10: return 5
2020-09-22 09:41:17 +08:00
}
},
2021-03-19 18:52:19 +08:00
Interval: function (value, copies, type, interValType) {
if (interValType === 'max' && value < 1) {
if (value < 1) {
value = 1
}
2021-03-19 18:52:19 +08:00
return value || 1
}
2021-03-19 18:52:19 +08:00
if (value < 1024 && type === 'Data') {
let interVal = value / copies
interVal = !isNaN(interVal) ? interVal : 1
2020-09-22 16:14:37 +08:00
return interVal
}
2021-03-19 18:52:19 +08:00
if (value < 1000 && (type === 'DataRate' || type === 'Misc')) {
let interVal = value / copies
interVal = !isNaN(interVal) ? interVal : 1
2020-09-22 16:14:37 +08:00
return interVal
}
2021-03-19 18:52:19 +08:00
if (type === 'Data') {
let interVal = value / copies
let pow = 0
if (interVal) {
while (interVal > 1024) {
pow++
interVal = interVal / 1024
2020-09-22 16:14:37 +08:00
}
2021-03-19 18:52:19 +08:00
interVal = Math.ceil(interVal) * Math.pow(1024, pow)
2020-09-22 16:14:37 +08:00
}
interVal = !isNaN(interVal) ? interVal : 1
2020-09-22 16:14:37 +08:00
return interVal
}
2021-03-19 18:52:19 +08:00
if (type === 'DataRate' || type === 'Misc') {
let interVal = value / copies
let pow = 0
if (interVal) {
while (interVal > 1000) {
pow++
interVal = interVal / 1000
2020-09-22 16:14:37 +08:00
}
2021-03-19 18:52:19 +08:00
interVal = Math.ceil(interVal) * Math.pow(1000, pow)
2020-09-22 16:14:37 +08:00
}
interVal = !isNaN(interVal) ? interVal : 1
return interVal
}
2021-03-19 18:52:19 +08:00
return 1
// let interVal=value/copies;
// interVal = interVal || 1;
// return interVal
}
}