score:0

you could add an additional expression to rounded variable if its rendering specific item. this is not the best approach.

var rounded = this._datasetindex  === lastvisible || this._index === 2;

alternatively you could compare values in each dataset

var cond = false;

if(this._datasetindex === 0) {
  var datasets = this._chart.data.datasets;
  var dataset = datasets[this._datasetindex];
  var currentdata = dataset.data[this._index];
  var nextdata = datasets[this._datasetindex + 1].data[this._index]
  cond = currentdata !== 0 && nextdata === 0;
}

var rounded = this._datasetindex  === lastvisible || cond;

score:1

i have updated your fiddle only line number 118: https://jsfiddle.net/r7hvn2ox/

from:

if (rounded){

to:

if (rounded || this._chart.data.datasets[1].data[this._index] == 0) {

i know its fix for 2 datasets: [{}, {}] bars, but it will work perfectly in any of the values of datasets and also for this example.

score:1

thanks everyone for answers.i tried below and worked perfectly for any amount of datasets.

var lastvisible;
  var datasetslength = this._chart.data.datasets.length;
  this._chart.data.datasets.map((e,index)=>{
    lastvisible=datasetslength-1;
    //to find the depth of datasets and get non-zero value
    for(var i=lastvisible;i>0;i--){
    if(!this._chart.getdatasetmeta(i).hidden){
      if(this._chart.data.datasets[i].data[this._index] != 0){
        lastvisible = i;
        break;
      }
    }
    } 
  })

Related Query

More Query from same tag