How get sum of total values in stackedBar ChartJs

前端 未结 6 1651
执笔经年
执笔经年 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 14:53

    @Haider this is what you were looking for, I had the same problem. I have reused your code and built upon it @tektiv

    I have made one small change where instead of building into the label I have made use of the afterbody. This removes the key color

    afterBody code:

                    afterBody: function (tooltipItem, data) {
                      var corporation = data.datasets[tooltipItem[0].datasetIndex].label;
                    var valor = data.datasets[tooltipItem[0].datasetIndex].data[tooltipItem[0].index];
                    var total = 0;
                    for (var i = 0; i < data.datasets.length; i++)
                        total += data.datasets[i].data[tooltipItem[0].index];  
                    return "Total : $" + total;
                }
    

    Full code here at JSFiddle

    Picture demonstration of the finished tooltip

提交回复
热议问题