生成折线图时出错,Uncaught TypeError:无法读取属性'每个'未定义的

时间:2015-11-08 23:03:55

标签: javascript d3.js nvd3.js

这是转载的问题。运行它并打开控制台时,您可以看到错误。 http://plnkr.co/edit/WC5TLIMENHtLcPF7T8uK?p=preview

我得到了

Uncaught TypeError: Cannot read property 'each' of undefined

在第6125行定义的函数的nvd3.js中。Link the the source。以下功能很少:

function chart(selection) {
    renderWatch.reset();
    renderWatch.models(lines);
    if (showXAxis) renderWatch.models(xAxis);
    if (showYAxis) renderWatch.models(yAxis);
    console.log(selection);
    selection.each(function(data) {
    ...

这看起来是定义图表的结构。不过我还是这个图书馆的初学者,所以我非常感谢你帮忙解决这个问题!

1 个答案:

答案 0 :(得分:2)

两个问题:

1。)您的数据格式错误,应该是一个对象数组,而不仅仅是一个对象:

var data = [{
  "key": "test",
  "values": [{
    "val": 56.00,
    "timestamp": "2015-11-08T18:10:36"
  }, {
    "val": 88.45,
    "timestamp": "2015-11-08T18:11:36"
  }, {
    "val": 120.71,
    "timestamp": "2015-11-08T18:12:36"
  }, {
    "val": 30.04,
    "timestamp": "2015-11-08T18:13:36"
  }, {
    "val": 55.11,
    "timestamp": "2015-11-08T18:14:36"
  }, {
    "val": 88.31,
    "timestamp": "2015-11-08T18:15:36"
  }, {
    "val": 90.09,
    "timestamp": "2015-11-08T18:16:36"
  }]
}];

2。)nv.addGraph将函数作为参数,但是您正在调用该函数。应该是:

nv.addGraph(generateLineChart);

更新plunker here