如何在 amcharts V4 导出功能中导出百分比值

How to export the percentage value in amcharts V4 export functionality

我的问题与 基本相同,但使用的是 amcharts v4 而不是 v3:我想导出 amcharts 使用 chart.exporting 功能计算的百分比值。

这就是我目前所拥有的。我计算百分比并在图表中显示它们:

const series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.categoryX = 'hoursCount';
series.dataFields.valueY = 'clientsCount';
series.name = 'Visits';
series.calculatePercent = true;
series.dataFields.valueYShow = 'percent';

然后,我正在配置导出:

chart.exporting.menu = new am4core.ExportMenu();
chart.exporting.dataFields = {
      hoursCount: 'Name of bins',
      clientsCount: 'Number of clients',
};

但是,如何将amcharts v4计算的pct添加到导出中?

chart.exporting.adapter.add("data", function(data) {
    var sum = data.data.reduce(function(accumulator, currentDataValue) {
        return accumulator + parseFloat(currentDataValue.clientsCount);
    }, 0);
    data.data.forEach(function(currentDataValue) {
        currentDataValue.percents =
            (parseFloat(currentDataValue.clientsCount) / sum * 100).toFixed(1) + "%";
    });
    return data;
});

@otmezger 已经设置好了。 series.calculatePercent = true; 这意味着百分比将被计算两次。

chart.exporting.adapter.add("data", function(data) {
    var sum = data.data.reduce(function(accumulator, currentDataValue) {
        return accumulator + parseFloat(currentDataValue.clientsCount);
    }, 0);
    data.data.forEach(function(currentDataValue) {
        currentDataValue.percents =
            (parseFloat(currentDataValue.clientsCount) / sum * 100).toFixed(1) + "%";
    });
    return data;
});

问题已通过从 chart.series 检索数据得到解决。

chart.exporting.adapter.add("data", function(data, target) {
    for (var i = 0; i < data.data.length; i++) {
        var v0 = chart.series._values[0];
        data.data[i].percent = Math.round((v0.dataItems.values[i].values.value.percent || 0) * 10) / 10;
    }
    return data;
});

在新版本中,它不会计算两次百分比。