简体   繁体   English

Morris.js数据(无法读取未定义的属性“ match”)

[英]Morris.js data (Cannot read property 'match' of undefined)

I have been pounding at this problem for a couple of days. 我已经为这个问题困扰了几天。 I am trying to use Morris Chart in one of my pages to display statistics collected from a SQL database. 我试图在我的页面之一中使用Morris Chart显示从SQL数据库收集的统计信息。 But I cannot get it to work. 但是我无法使其正常工作。 It all comes down to this: 归结为:

If I manually enter the data in the "data" field everything is fine. 如果我在“数据”字段中手动输入数据,一切都很好。 But if I put the data in a variable and refer to the variable in the "data" field I get: 但是,如果我将数据放在变量中并在“数据”字段中引用变量,则会得到:

Cannot read property 'match' of undefined. 无法读取未定义的属性“匹配”。

What is the difference? 有什么区别?

This works: 这有效:

 var chart = Morris.Line({ element: 'line-example', data: [{Timestamp:'2015-05-20 10:00:00',Diastolic:110,Systolic:90,Pulse:65}], xkey: 'Timestamp', ykeys: ['Diastolic', 'Systolic', 'Pulse'], labels: ['Diastolic', 'Systolic', 'Pulse'] }); 

This does not work: 这不起作用:

 var mydata = "[{Timestamp:'2015-05-20 10:00:00',Diastolic:110,Systolic:90,Pulse:65}]"; var chart = Morris.Line({ element: 'line-example', data: mydata, xkey: 'Timestamp', ykeys: ['Diastolic', 'Systolic', 'Pulse'], labels: ['Diastolic', 'Systolic', 'Pulse'] }); 

The first one is an array. 第一个是数组。 The second one, you're defining mydata as a string. 第二个,您将mydata定义为字符串。

Try: 尝试:

data: JSON.parse(mydata)

I was experiencing a similar issue which was driving me crazy but fortunately I came to a solution. 我遇到了类似的问题,这使我发疯,但是幸运的是我找到了解决方案。

I was passing an array of javascript objects as the holder of data and used the function to turn it into a JSON string. 我传递了一个javascript对象数组作为数据持有者,并使用该函数将其转换为JSON字符串。 It wasn't working at all... but then I tried to parse the string with JSON.parse and it worked smoothly. 它根本没有用...但是后来我尝试用JSON.parse解析字符串,并且运行顺利。 Apparently there is a bug in Morris JS code while parsing a JSON string. 解析JSON字符串时,显然Morris JS代码中存在错误。

If you are using an array of objects like me, try this: 如果您正在使用像我这样的对象数组,请尝试以下操作:

var area = new Morris.Area({
        element: 'revenue-chart',
        resize: true,
        data: JSON.parse(JSON.stringify(data)), //data is an array of objects
        xkey: 'date',
        ykeys: ['npubs_t', 'npubs_f'],
        labels: ['T', 'F'],
        lineColors: ['#a0d0e0', '#3c8dbc'],
        hideHover: 'auto'
      });

It worked great this way. 这样效果很好。 Hope this helps you or someone else. 希望这对您或其他人有帮助。

I hit this same problem. 我遇到了同样的问题。 My issue was that I had declared the data variable as array in my angular(1.5) controller: 我的问题是我已在我的angular(1.5)控制器中将数据变量声明为数组:

  data = [];

And then I was getting values from the function call. 然后我从函数调用中获取值。 This was working except for line charts in morris.js (Angular-morris was the wrapper). 除morris.js中的折线图(角度morris是包装器)外,此方法均有效。 I changed data to 我将数据更改为

  data = '';

It worked perfectly. 效果很好。

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

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