简体   繁体   English

morris.js setData导致“未捕获的TypeError:无法读取未定义的属性'x'”

[英]morris.js setData causes “Uncaught TypeError: Cannot read property 'x' of undefined”

I am trying to draw a bar graph that gets updated when a dropdown selection changes. 我正在尝试绘制一个条形图,该条形图在下拉选择更改时会更新。

The following code is included in my html doc, and the leaveStayGraph function is called onChange of dropdown. 以下代码包含在我的html文档中,并且leavesStayGraph函数称为下拉列表的onChange。 Every time after the first time leaveStayGraph is called, I get the following error: 每次第一次调用LeaveStayGraph之后,都会出现以下错误:

Uncaught TypeError: Cannot read property 'x' of undefined

Before subsequent calls (when setData is called), leaveStayData looks perfectly fine (in fact it looks just about the same as it did on the first call, which worked). 在随后的调用之前(当调用setData时),leavesStayData看起来很好(实际上,它看起来与第一次调用的效果几乎相同)。

var leaveStayChart = null;
function leaveStayGraph(display_data, date) {

    /* Do stuff to get data */

    // Draw Chart
    var leaveStayData = {
        element: 'leave_stay_pie_chart',
        data: [{
            label: "Stayed",
            value: count
        }, {
            label: "Left",
            value: total - count
        }],
        xkey: 'label',
        ykeys: ['value'],
        labels: ['Count'],
        hideHover: 'auto'
    };

    if (leaveStayChart == null) {
        // FIRST CALL
        leaveStayChart = Morris.Bar(leaveStayData);
    }
    else {
        // SUBSEQUENT CALLS
        leaveStayChart.setData(leaveStayData);
    }
}

My end goal is to update the graph upon changing the selection of the dropdown, but every time setData is called, I get this error and nothing displays. 我的最终目标是在更改下拉列表的选择时更新图形,但是每次调用setData时,都会出现此错误,并且不显示任何内容。 Is something happening that I can change. 我正在改变某些事情。

UPDATE 更新

Just to clarify, the basic idea is something like this (half-taken from the example from morris.js). 需要澄清的是,基本思想是这样的(取自morris.js的示例的一半)。 When the button is pressed, the chart updates. 按下按钮后,图表将更新。

var x = Morris.Bar({
  element: 'bar-example',
  data: [
    { y: '2006', a: 100, b: 90 },
    { y: '2007', a: 75,  b: 65 },
    { y: '2008', a: 50,  b: 40 },
    { y: '2009', a: 75,  b: 65 },
    { y: '2010', a: 50,  b: 40 },
    { y: '2011', a: 75,  b: 65 },
    { y: '2012', a: 100, b: 90 }
  ],
  xkey: 'y',
  ykeys: ['a', 'b'],
  labels: ['Series A', 'Series B']
});

document.write("<button onClick='update()'>Test</button>");

function update() {
  x.setData({
  element: 'bar-example',
  data: [
    { y: '2006', a: 100, b: 1 },
    { y: '2007', a: 75,  b: 65 },
    { y: '2008', a: 46,  b: 40 },
    { y: '2009', a: 43,  b: 41 },
    { y: '2010', a: 50,  b: 40 },
    { y: '2011', a: 3,  b: 2 },
    { y: '2012', a: 30, b: 90 }
  ],
  xkey: 'y',
  ykeys: ['a', 'b'],
  labels: ['Series A', 'Series B']
});
}

So you are on the right track, you just need to pass in the JSON data to setData instead of the entire graph object. 因此,您处在正确的轨道上,只需要将JSON数据而不是整个图对象传递给setData。 So this would be my revised version of your code: 因此,这将是我的代码修订版:

var leaveStayChart = null;
function leaveStayGraph(display_data, date) {

    /* Do stuff to get data */
    var graphData = [{
            label: "Stayed",
            value: count
        }, {
            label: "Left",
            value: total - count
        }];

    if (leaveStayChart == null) {
        // FIRST CALL
        leaveStayChart = Morris.Bar({
            element: 'leave_stay_pie_chart',
            data: graphData,
            xkey: 'label',
            ykeys: ['value'],
            labels: ['Count'],
            hideHover: 'auto'
        });
    }
    else {
        // SUBSEQUENT CALLS
        leaveStayChart.setData(graphData);
    }
}

The issue is that setData doesn't take in the same type of object that is passed in to Morris.Bar(data) . 问题是setData不会采用传递给Morris.Bar(data)的同一类型的对象。 The object passed into the setData function is the value under data in the original object. 传递给setData函数的对象是原始对象中data之下的值。

This would be the revised sample code: 这将是修改后的示例代码:

var x = Morris.Bar({
  element: 'bar-example',
  data: [
    { y: '2006', a: 100, b: 90 },
    { y: '2007', a: 75,  b: 65 },
    { y: '2008', a: 50,  b: 40 },
    { y: '2009', a: 75,  b: 65 },
    { y: '2010', a: 50,  b: 40 },
    { y: '2011', a: 75,  b: 65 },
    { y: '2012', a: 100, b: 90 }
  ],
  xkey: 'y',
  ykeys: ['a', 'b'],
  labels: ['Series A', 'Series B']
});

document.write("<button onClick='update()'>Test</button>");

function update() {
  x.setData([
    { y: '2006', a: 100, b: 1 },
    { y: '2007', a: 75,  b: 65 },
    { y: '2008', a: 46,  b: 40 },
    { y: '2009', a: 43,  b: 41 },
    { y: '2010', a: 50,  b: 40 },
    { y: '2011', a: 3,  b: 2 },
    { y: '2012', a: 30, b: 90 }
  ]);
}

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

相关问题 未捕获的TypeError:无法读取未定义的Morris.js的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined Morris.js Morris.js未捕获TypeError:无法读取未定义的属性“匹配” - Morris.js Uncaught TypeError: Cannot read property 'match' of undefined Morris.js:无法读取未定义的属性“ x” - Morris.js: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;setData&#39; - Uncaught TypeError: Cannot read property 'setData' of undefined Morris.js数据(无法读取未定义的属性“ match”) - Morris.js data (Cannot read property 'match' of undefined) 未捕获的TypeError:无法读取未定义的属性&#39;setData&#39;[Slider] - Uncaught TypeError: Cannot read property 'setData' of undefined[Slider] 未捕获的TypeError:无法读取未定义的common.js的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined common.js js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball - js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball 未捕获的TypeError:无法读取未定义的属性“ x” - Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性x - Uncaught TypeError: Cannot read property x of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM