Morris.js:看不懂财产' x'未定义的

时间:2015-05-25 15:20:28

标签: javascript jquery json morris.js

我试图使用由JSON文件提供的Morris.js来绘制面积图。

我的JSON如下:

Scanner in=new Scanner(System.in);
while(true){
    int X = in.nextInt();
    System.out.println(X);
}

我创建了一个包含以下内容的.js文件:

[{"period": 0, "time": 121.0}, {"period": 1, "time": 102.0}, {"period": 2, "time": 104.0}, {"period": 3, "time": 91.0}, {"period": 4, "time": 99.0}, {"period": 5, "time": 94.0}, {"period": 6, "time": 95.0}, {"period": 7, "time": 95.0}, {"period": 8, "time": 91.0}, {"period": 9, "time": 108.0}]

我遇到以下错误:

$(function() {

var jsonData = $.getJSON("data.json", function (json) {
    console.log(json); 

    Morris.Area({
        element: 'morris-area-chart',
        data: jsonData,
        xkey: 'period',
        ykeys: ['time'],
        labels: ['Time'],
        pointSize: 2,
        hideHover: 'auto',
        resize: true
    });
});
});

当我替换"数据:jsonData"手动引入值的行我没有收到错误并成功绘制数据。

$(function(){

Uncaught TypeError: Cannot read property 'x' of undefined

1 个答案:

答案 0 :(得分:1)

根据Pointy的建议:

$(function() {

  var jsonData = $.getJSON("data.json", function (jsonData) {
  console.log(jsonData); 

  Morris.Area({
      element: 'morris-area-chart',
      data: jsonData,
      xkey: 'period',
      ykeys: ['time'],
      labels: ['Time'],
      pointSize: 2,
      hideHover: 'auto',
      resize: true
    });
  });
});