vue的一张echarts环形图,当数据全部为0的时候,算出来的百分比不是0%而是NaN%,怎么样让它变成0%?
async ShorepowermaintainStatistics() {
let res = await shorepowermaintainStatistics(this.listQuery);
if (res.code === "0") {
const total = res.data.neiHe + res.data.yanJiang + res.data.yanHai;
var option1 = {
title: {
text: `设备总数:${total}台`,
textStyle: {
color: "#00FFFF",
fontSize: "14px",
},
right: 0,
top: 15,
},
legend: {
right: 0,
top: "center",
orient: "",
icon: "circle",
textStyle: {
color: "white",
},
formatter: function (name) {
var data = option1.series.data;
var total = 0;
var tarValue;
for (var i = 0, l = data.length; i < l; i++) {
total += data[i].value;
if (data[i].name == name) {
tarValue = data[i].value;
}
}
var p = ((tarValue / total) * 100).toFixed(1);
return name + " " + " | " + " " + " " + p + "% " + tarValue;
},
},
tooltip: {},
series: {
name: "",
type: "pie",
radius: ["40%", "90%"],
center: ["30%", "55%"],
//roseType: "area",
label: {
show: false,
},
color: ["#0EBB5B", "#F19149", "#A0A0A0"],
data: [
{ value: res.data.neiHe, name: "内河" },
{ value: res.data.yanJiang, name: "沿江" },
{ value: res.data.yanHai, name: "沿海" },
],
},
};