display total count at stacked bar in cahrtjs

Solutions on MaxInterview for display total count at stacked bar in cahrtjs by the best coders in the world

showing results for - "display total count at stacked bar in cahrtjs"
Abraham
20 Oct 2017
1animation: {
2                onComplete: function() {
3                  var chartInstance = this.chart;
4                  var ctx = chartInstance.ctx;
5                  ctx.textAlign = "left";
6                  ctx.font = "bold 15px Open Sans ";
7                  ctx.textBaseline = 'bottom';
8              
9                  //display total count
10                  this.data.datasets[0].data.forEach(function(data, index) {
11                    var total = data + this.data.datasets[1].data[index] +this.data.datasets[2].data[index]+this.data.datasets[3].data[index]+this.data.datasets[4].data[index];
12                    //getDatasetMeta(4) include your dataset length
13                    var meta = chartInstance.controller.getDatasetMeta(4);
14                    var posX = meta.data[index]._model.x;
15                    var posY = meta.data[index]._model.y;
16                    //if length is 0 dont display
17                    if(total > 0){
18                        ctx.fillText(total, posX + 6, posY + 8);
19                        ctx.fillStyle = "black";
20                    }
21                  }, this);
22                }
23              },