Dataset._meta[0].dataset is null in ChartJS

廉价感情. 提交于 2021-01-29 06:13:16

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/52162058/dataset-meta0-dataset-is-null-in-chartjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!