有时候我们需要在饼图的legend上显示百分比
var option6 = {
color: ['#00a65a', '#00c0ef', '#f39c12', '#f56954', '#0d84df', '#ca04d6', '#e2ce27', '#a3d252', '#13cabf', '#42c78a'],
title: {
text: '今年各行业用气占比',
x: '270',
y: 'top',
textStyle: {
fontSize: 16,
}
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b}: {c} ({d}%)"
},
legend: {
orient: 'vertical',
x: '0',
top: '80',
itemGap: 15,
formatter: function(name) {
var data = option6.series[0].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(2);
return name + ' ' + ' ' + '(' + p + '%)';
},
data: ['行业1', '行业2', '行业3', '行业4', '行业5', '行业6', '行业7', '行业8', '行业9', '行业10'],
},
series: [{
name: '今年各行业用气占比',
type: 'pie',
center: ['70%', '50%'],
radius: ['30%', '65%'],
label: {
},
labelLine: {
},
data: [
{ value: 335, name: '行业1', },
{ value: 310, name: '行业2', },
{ value: 234, name: '行业3', },
{ value: 335, name: '行业4', },
{ value: 310, name: '行业5', },
{ value: 234, name: '行业6', },
{ value: 234, name: '行业7', },
{ value: 335, name: '行业8', },
{ value: 310, name: '行业9', },
{ value: 234, name: '行业10', },
]
}]
};
var myarea1 = echarts.init(document.getElementById('chart-area1'), pietheme);
myarea1.setOption(option6);
(adsbygoogle = window.adsbygoogle || []).push({});