百度echarts饼状legend显示值和百分比

2025年02月21日 建站教程

功能介绍:百度echarts做一个pie饼状,想要在legend名称后面加上当前的值和百分比,并且每个文字标签宽带一致(如右图),下面web建站小编给大家简单介绍一下实现代码!

实现代码如下:

//myData图表数据

legend: {
  bottom: '0',
  left: 'center',
  icon: "circle",
  itemHeight: 6,
  itemWidth: 6,
  formatter: function(name) {
    var total = 0
    var value
    for (var i = 0; i < myData.length; i++) {
      total += myData[i].value
      if (myData[i].name == name) {
        // 获取对应值
        value = myData[i].value
      }
    }
    let percent = Math.round((tarValue / total) * 100) + '%' // 获取百分百(根据需要在下面添加)
    var arr = ['{a|' + name + '}', '{b|' + value + '}']
    return arr.join('  ')
  },
  textStyle: {
    rich: {
      a: { //设置名称宽度
        width: 32
      },
      b: { //设置值的宽度
        width: 65,
      },
      c:{
	//.....一般值都可以设置
        //fontSize: 15,
        //color: '#c1c1c1'
      }
    }
  },
}

本文链接:http://so.lmcjl.com/news/23586/

展开阅读全文
相关内容