Echarts 图例后面增加内容 报错option is not defind

效果

 需添加以下代码即可,与 series 平级

formatter: function(name) {
  let data = option.series[0].data;
  let total = 0;
  let tarValue;
  for (let i = 0; i < data.length; i++) {
    total += data[i].value;
    if (data[i].name === name) {
      tarValue = data[i].value;
    }
  }
  // 数量
  let v = tarValue;
  // 百分比
  let p = Math.round(((tarValue / total) * 100));
  // return `${name}  ${v}次(${p}%)`;
  return `${name}:${v}次`;
},

解决问题

formatter: name=> {
    let data = this.option.series[0].data;
    let total = 0;
    let tarValue;
    for (let i = 0; i < data.length; i++) {
        total += data[i].value;
        if (data[i].name === name) {
        tarValue = data[i].value;
        }
    }
        // 数量
    let v = tarValue;
        // 百分比
    let p = Math.round(((tarValue / total) * 100));
    // return `${name}  ${v}次(${p}%)`;
    return `${name}:${v}次`;
        },
},

 

posted @ 2024-05-21 09:51  焦廉琨  阅读(23)  评论(0编辑  收藏  举报