简体   繁体   English

Morris.js:无法读取未定义的属性“ x”

[英]Morris.js: Cannot read property 'x' of undefined

I'm trying to use Morris.js feeded by a JSON file to plot an area chart. 我正在尝试使用JSON文件提供的Morris.js来绘制面积图。

My JSON is as following: 我的JSON如下:

[{"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}]

I created a .js file with the following content: 我创建了一个具有以下内容的.js文件:

$(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
    });
});
});

I'm encountering the following error: 我遇到以下错误:

Uncaught TypeError: Cannot read property 'x' of undefined

When I replace the "data: jsonData" line with manually introduced values I received no error and successfully plot the data. 当我用手动引入的值替换“ data:jsonData”行时,我没有收到任何错误并成功绘制了数据。

$(function() { $(function(){

Morris.Area({
    element: 'morris-area-chart',
    data: [{
        period: '1',
        time: 10,
    }, {
        period: '2',
        time: 8,
    }, {
       period: '3',
        time: 11,
    }, {
       period: '4',
        time: 12,
    }, {
        period: '5',
        time: 16,
    }, {
        period: '6',
        time: 13,
    }, {
        period: '7',
        time: 7,
    }, {
        period: '8',
        time: 10,
    }, {
        period: '9',
        time: 13,
    }, {
        period: '10',
        time: 10,
    }],
    xkey: 'period',
    ykeys: ['time'],
    labels: ['Task Time'],
    pointSize: 2,
    hideHover: 'auto',
    resize: true
});

As per Pointy suggestion: 根据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
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM