echarts显示小数点后两位
2024-11-04 15:16:08 # 技术笔记
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html style="height: 100%">

<head>
<meta charset="utf-8">
</head>

<body style="height: 100%; margin: 0; background-color: transparent">
<div id="container" style="height: 100%; background-color: transparent"></div>

<script type="text/javascript" src="echarts.min.js"></script>
<script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
var option;
option = {
backgroundColor: 'transparent',

series: [
{
type: 'gauge',
axisLine: {
lineStyle: {
width: 15,
color: [
[0.3, '#67e0e3'],
[0.7, '#37a2da'],
[1, '#fd666d']
]
}
},
min: 0,
max: 3,
pointer: {
itemStyle: {
color: 'auto'
}
},
axisTick: {//轴刻度
distance: -15,
length: 8,
lineStyle: {
color: '#fff',
width: 2
}
},
splitLine: {
distance: -15,
length: 15,
lineStyle: {
color: '#fff',
width: 2
}
},
axisLabel: {
color: 'auto',
distance: 20,
fontSize: 12
},
detail: {//pue显示
valueAnimation: true,
formatter: 'PUE= {value}',
color: 'green',
fontSize: 20
},
data: [
{
value: 0.00,
min: 0.00,
max: 3.00
}
]
}
]
};

oaJsApi.dpConnect('System1:PUE5.PUE:_online.._value', true,
{
success: function (data) {
var value = data.value[0];
//var pueShow = newF.toFixed(2);
//var pueShow = formatFloat(newF, n = 2,'floor');
//console.log(newF);
//console.log(pueShow);

myChart.setOption({
series: [
{
detail:{
formatter: function (value) {
return "PUE="+value.toFixed(2);
}
},
data: [
{
value:value,
}
]

}
]
});

}
});


if (option && typeof option === 'object') {
myChart.setOption(option);
}
//自建方法 ceil 向上取 floor 向下区
function formatFloat(num, n = 1, flag) {
var f = parseFloat(num);
if (isNaN(f)) {
return false;
}
if (flag === 'ceil') {
f = Math.ceil(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
} else if (flag === 'floor') {
f = Math.floor(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
} else {
f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
}
var s = f.toString();
var rs = s.indexOf('.');
//判定如果是整数,增加小数点再补0
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + n) {
s += '0';
}
return s;
}

</script>
<script>

</script>

</body>

</html>

先处理传入的value值,

1
2
3
4
5
data: [
{
value:value,
}
]

然后利用函数给formatter发送数据,return数据可以是string类型

1
2
3
4
5
detail:{
formatter: function (value) {
return "PUE="+value.toFixed(2);
}
},

前端针对pue数据进行显示 formatter: '{value}'

1
2
3
4
5
6
detail: {//pue显示
valueAnimation: true,
formatter: '{value}',
color: 'green',
fontSize: 20
},