0

I'm trying to show the value of each bar using chart JS. I saw this thread and followed, but I got an error.

Link: How to show data values or index labels in ChartJs (Latest Version)

var options = {
    events: false,
    showTooltips: false,
    animation: {
          duration: 0,
          onComplete: function(){
                var ctx = this.chart.ctx;
                ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontFamily, 'normal', Chart.defaults.global.defaultFontFamily);
                ctx.textAlign = 'center';
                ctx.textBaseLine = 'bottom';

                this.data.datasets.forEach(function(dataset){
                console.log(dataset);
                for(var i = 0; i < dataset.data.length; i++){
                    var model = dataset._meta[0].dataset._children[i]._model;
                    ctx.fillText(dataset.data[i], model.x, model.y - 5);
                }
            });
        }
    }
};

Error

TypeError: dataset._meta[0].dataset is null

Blues Clues
  • 1,694
  • 3
  • 31
  • 72

1 Answers1

2

As you can see on the linked post (you linked that post), the answer contains code like this:

for (var i = 0; i < dataset.data.length; i++) {
    for(var key in dataset._meta)
    {
        var model = dataset._meta[key].data[i]._model;
        ctx.fillText(dataset.data[i], model.x, model.y - 5);
    }
}

This code is the accepted. Try this. The other, you try to use has the same null error problem.

DiabloSteve
  • 431
  • 2
  • 13