show total on stacked bar chartjs code example

Example: display total count at stacked bar in cahrtjs

animation: {
                onComplete: function() {
                  var chartInstance = this.chart;
                  var ctx = chartInstance.ctx;
                  ctx.textAlign = "left";
                  ctx.font = "bold 15px Open Sans ";
                  ctx.textBaseline = 'bottom';
              
                  //display total count
                  this.data.datasets[0].data.forEach(function(data, index) {
                    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];
                    //getDatasetMeta(4) include your dataset length
                    var meta = chartInstance.controller.getDatasetMeta(4);
                    var posX = meta.data[index]._model.x;
                    var posY = meta.data[index]._model.y;
                    //if length is 0 dont display
                    if(total > 0){
                        ctx.fillText(total, posX + 6, posY + 8);
                        ctx.fillStyle = "black";
                    }
                  }, this);
                }
              },