解決echarts餅圖不顯示資料為0的資料
如圖所示 餅圖資料為0但是還是會顯示lableline和lable
解決方法
var echartData = [{
value: data_arry[0]==0?null:data_arry[0],
name: '常駐人口'
}]
傳資料時判斷資料是否為空,如果為空的話 就填入null
餅圖程式碼
var scale = 1;
var echartData = [{
value: 54,
name: '常駐人口'
}, {
value: null,
name: 'VIP客戶'
}, {
value: null,
name : '工作人員'
}, {
value: 546,
name: '黑名單'
},
{
value: 245,
name: '空巢老人'
},{
value: 545,
name: '未登記人口'
}, ];
var rich = {total: { color: "#333333", fontSize: 45 * scale, align: 'center' }, chart_text:{ fontSize:14*scale, }
}
option = {
backgroundColor: '#fff',
title: {
text: '人流量',
left: 'center',
top: '50%',
padding: [24, 0],
textStyle: {
color: '#333333',
fontSize: 20 * scale,
align: 'center'
}
},
legend: {
selectedMode: false,
formatter: function(name) {
var total = 0; //各科正確率總和
var averagePercent; //綜合正確率
echartData.forEach(function(value, index, array) {
total += value.value;
});
return '{total|' + total + '}';
},
data: [echartData[0].name],
left: 'center',
top: 'center',
icon: 'none',
align: 'center',
textStyle: {
color: "#fff",
fontSize: 16 * scale,
rich: rich
},
},
series: [{
name: '人流量',
type: 'pie',
radius: ['42%', '50%'],
hoverAnimation: true,
color: ['#2DB7F5', '#7DC856','#999999', '#F3857B', '#8996E6', '#F9BF00' ],
label: {
normal: {
formatter: function(params, ticket, callback) {
var total = 0; //
var percent = 0; //
echartData.forEach(function(value, index, array) {
total += value.value;
});
percent = ((params.value / total) * 100).toFixed(1);
if(params.value==0)
return null;
else
return '{chart_text|' + params.name + '}\n{chart_text|' + params.value + '人}\n{chart_text|' + percent + '%}';
},
rich:rich,
},
},
data: echartData
}]
};