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

标签 amcharts amcharts4

我的问题与How to export the percentage value in amchart V3 export functionality基本相同,但使用 amcharts v4 而不是 v3:我想使用 chart.exporting 功能导出 amcharts 计算的百分比值。

这就是我到目前为止所拥有的。我计算百分比并将其显示在图表中:

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 添加到导出中?

最佳答案

@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;
});

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

关于amcharts - 如何在 amcharts V4 导出功能中导出百分比值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68858263/

相关文章:

javascript - Amcharts "TypeError: this.chart.zoomToDates is not a function"

amcharts - 创建饼图 amCharts

javascript - 防止浏览器中的日期转换?

javascript - 更新 am4charts.XYChart 数据

javascript - 无法向 amchart 股票图表添加超过 2 个图表

javascript - 如何点击Amcharts中的 map 气泡并调用服务器请求

javascript - 如何以编程方式更改 amStockChart 的缩放级别?

javascript - 如何解析 amcharts 股票日期

javascript - amCharts map ,纬度对数坐标上带有气泡