Chart.JS无法读取未定义的'_meta'属性。

6

我试图构建一个基本的条形图,但是我遇到了标题中的错误。我使用alert()来验证我想用于填充图表的数组是否包含数据,但是语法仍然存在问题。能否有人检查并告诉我如何做得更好,以使图表产生?

    var ctx = document.getElementById('cvtree').getContext('2d');
var chart = new Chart(ctx, {
    type: 'bar',
    data: { 
        labels: yoylabels,
        datasets: [{
                label: 'Pay By Person',
                backgroundColor: 'rgba(0, 129, 214, 0.8)',
                data: numericdollarvals
            }]
    },
    options: {
        },
        legend: {
            display: false,
            position: 'top',
        },
        scales: {
            yAxes: [{
                    ticks: {
                        beginAtZero: true,
                        callback: function (value, index, values) {
                            if (parseInt(value) >= 1000) {
                                return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                            } else {
                                return '$' + value;
                            }
                        }
                    }
                }]
        }
});
1个回答

5

在设置所有选项之前,您意外地关闭了选项属性。

以下是正确的语法:

var ctx = document.getElementById('cvtree').getContext('2d');
var chart = new Chart(ctx, {
   type: 'bar',
   data: {
      labels: yoylabels,
      datasets: [{
         label: 'Pay By Person',
         backgroundColor: 'rgba(0, 129, 214, 0.8)',
         data: numericdollarvals
      }]
   },
   options: {
      legend: {
         display: false,
         position: 'top',
      },
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               callback: function(value, index, values) {
                  if (parseInt(value) >= 1000) {
                     return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                  } else {
                     return '$' + value;
                  }
               }
            }
         }]
      }
   }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接