How get sum of total values in stackedBar ChartJs

前端 未结 6 1527
执笔经年
执笔经年 2021-02-05 14:06

I\'m trying to get the sum of all values of a stackedBar and include this total in tooltip.

Note: my datasets aren\'t static, this is an example

6条回答
  •  滥情空心
    2021-02-05 15:05

    In the other answers you replace the last dataset, with this you don't need to

    tooltips: {
        callbacks: {
            title: function(tooltipItems, data) {
                return _this.chart.data.labels[tooltipItems[0].index];
            },
            footer: function(tooltipItems, data) {
                let total = 0;
                for (let i = 0; i < tooltipItems.length; i++) {
                    total += parseInt(tooltipItems[i].yLabel, 10);
                }
                return 'Total: ' + total;
            }
        }
    }
    

    Ps: It's typescript lang.

提交回复
热议问题