Apache ECharts 如何在 Title Subtext 中显示饼图的 Total(sum)值?

Apache ECharts how to display Total (sum) value for pie chart in Title Subtext?

piechart

我在 Apache Echarts 上创建了一个饼图,我可以知道有没有办法在标题子文本中显示总值?

option = {
title: [{
    text: 'Sales',
    subtext: 'Total Sales: ' , //wish to show total value over here
    left: '50%',
    textAlign: 'center'
}],
dataset: {
        source: [
            ['OUTLET A', 100],
            ['OUTLET B', 52],
            ['OUTLET C', 200],
            ['OUTLET D', 334],
            ['OUTLET E', 430],
            ['OUTLET F', 330],
            ['OUTLET G', 220]
        ]
    },
series: [
    {
        name: 'Sales',
        type: 'pie',
        label: {
            show: true,
            textBorderWidth: 2,
            position: 'outside',
            color: "#a4311d",
            formatter: '{c} ({d}%)'
        }
    }]
};

您需要将数据集分配给局部变量并为其创建求和函数。

let data = [['OUTLET A', 100],
            ['OUTLET B', 52],
            ['OUTLET C', 200],
            ['OUTLET D', 334],
            ['OUTLET E', 430],
            ['OUTLET F', 330],
            ['OUTLET G', 220]]

在你的数据集中:

dataset: {
        source: data},

创建求和函数来计算总和:

const sum = data.reduce(function(prev, current) {
  return prev + current[1]
}, 0);

在标题子文本中显示总和:

subtext: 'Total Sales: '+sum