Error when generating line Chart, Uncaught TypeError: Cannot read property 'each' of undefined

Error when generating line Chart, Uncaught TypeError: Cannot read property 'each' of undefined

这里是问题复现。当 运行 它并打开控制台时,您可以看到错误。 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.) 你的数据格式错误,应该是对象数组,而不是单个对象:

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