chartjs stacked bar show total

Solutions on MaxInterview for chartjs stacked bar show total by the best coders in the world

showing results for - "chartjs stacked bar show total"
Émeric
02 May 2019
1callbacks: {
2    label: function(tooltipItem, data) {
3        var corporation = data.datasets[tooltipItem.datasetIndex].label;
4        var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
5
6        // Loop through all datasets to get the actual total of the index
7        var total = 0;
8        for (var i = 0; i < data.datasets.length; i++)
9            total += data.datasets[i].data[tooltipItem.index];
10
11        // If it is not the last dataset, you display it as you usually do
12        if (tooltipItem.datasetIndex != data.datasets.length - 1) {
13            return corporation + " : $" + valor.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
14        } else { // .. else, you display the dataset and the total, using an array
15            return [corporation + " : $" + valor.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'), "Total : $" + total];
16        }
17    }
18}
19