Javascript 生成折线图时出错,未捕获类型错误:无法读取属性';每个';未定义的

Javascript 生成折线图时出错,未捕获类型错误:无法读取属性';每个';未定义的,javascript,d3.js,nvd3.js,Javascript,D3.js,Nvd3.js,这就是问题所在。您可以在运行它并打开控制台时看到错误。 我得到一份工作 Uncaught TypeError: Cannot read property 'each' of undefined 在第6125行中定义的函数的nvd3.js中。以下函数的几行: function chart(selection) { renderWatch.reset(); renderWatch.models(lines); if (showXAxis) renderWatch.mode

这就是问题所在。您可以在运行它并打开控制台时看到错误。

我得到一份工作

Uncaught TypeError: Cannot read property 'each' of undefined
在第6125行中定义的函数的nvd3.js中。以下函数的几行:

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。

非常感谢!后续问题:如果我想将参数传递到我的
generateLineChart
函数中(如轴标签、格式等),如果
nv.addGraph
接受一个函数,我该怎么做呢?我检查了addGraph的源代码,但它似乎没有提供这样做的方法。@M.g.只需创建一个返回函数的函数即可。查看更新