如何在ECharts中的甜甜圈图中心显示文本?

问题描述 投票:0回答:1

我想在不使用x和y轴的情况下在甜甜圈图的中心显示所有数据值的总和。对于甜甜圈图,我将其用作参考https://echarts.apache.org/examples/zh/editor.html?c=pie-doughnut

如下面的代码所示,chartvalueDatanameoflabels都是动态出现的。

this.chartData = {
          tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
          },
          legend: {
            orient: 'vertical',
            x: 'left',
            data: nameoflabels,
            bottom: 20
          }
          series: [
            {
              name: 'Title',
              type: 'pie',
              radius: ['50%', '70%'],
              avoidLabelOverlap: false,
              label: {
                normal: {
                  textStyle: {
                    fontWeight: 'bold'
                  }
                },
                emphasis: {
                  textStyle: {
                    fontWeight: 'bold'
                  }
                }
              },
              labelLine: {
                normal: {
                  show: true
                }
              },
              data: chartvalueData,
              color: ['lightblue', 'orange','lightcoral','plum']
            }
          ]
};
angular echarts
1个回答
0
投票

使用formatter选项在图表中显示自定义值,并使用position: center居中对齐标签:

const data = [
  { value: 335, name: 'A' },
  { value: 310, name: 'B' },
  { value: 234, name: 'C' },
  { value: 135, name: 'D' },
  { value: 1548, name: 'E' },
];

// find the sum of all data values
const sum = data.reduce((prev, current) => prev + current.value, 0);

option = {
  series: [
    {
      name: 'Series 1',
      type: 'pie',
      radius: ['50%', '70%'],
      avoidLabelOverlap: true,
      label: {
        color: '#000',
        fontSize: '80',
        position: 'center',
        formatter: () => {
          return sum; // Use sum variable here
        },
      },
      labelLine: {
        show: false,
      },
      data: data,
    },
  ],
};
© www.soinside.com 2019 - 2024. All rights reserved.