NVD3折线图未捕获TypeError:无法读取未定义的属性"x"

rus*_*ilg 9 d3.js nvd3.js

我正在使用NVD3库根据Rails控制器中生成的数据制作简单的折线图.我用来在Rails中生成数据的代码是:

task.task_values.each do |u|
 array.push({ :x => u.created_at.to_i * 1000, :y => u.value.to_i })
end
data_label = task.name + " ("+ task_unit +")"
taskValuesList = [{:key => data_label, :values => array}]
data = {:type => "line", :data => taskValuesList}
Run Code Online (Sandbox Code Playgroud)

然后,在我看来,我有以下JS代码:

nv.addGraph(function() {
var chart = nv.models.lineChart()
  .x(function(d) { return d.x; })
      .y(function(d) { return d.y; });

chart.xAxis
   .showMaxMin(false)
       .tickFormat(function(d){return d3.time.format("%m/%d/%y")(new Date(d));});
chart.yAxis
   .tickFormat(d3.format(',d'));

d3.select('#chart<%= i %> svg')
  .datum(data.data)
  .transition().duration(500)
  .call(chart);

nv.utils.windowResize(chart.update);
  return chart;
});
Run Code Online (Sandbox Code Playgroud)

图表正确呈现,但是当我尝试鼠标悬停数据点以显示工具提示时,我收到错误"Uncaught TypeError:无法读取属性'x'未定义"

lin*_*nqu 26

我有同样的错误,并被困了几个小时.经过一番调查,我发现,我用的是最新版本d3.js这是不是最新版本的兼容nvd3.js

确保您使用的是nvd3存储库中包含的d3.js版本:/lib/d3.v3.js

找出来是非常棘手的.特别是因为nvd3文档告诉你使用最新的d3.js版本;-(

  • 一个细节,这可以在折线图上重现,该折线图包含共享公共(x,y)点的两条线. (4认同)

sha*_*r90 -2

确保您的数据是 JSON 格式,

示例 JSON 数据应如下所示

data = [{
    key : "Line 1",
    color : "#51A351",
    values : [{
        x : 1373403179000,
        y : 40
    }, {
        x : 1373403469000,
        y : 30
    }, {
        x : 1373403567000,
        y : 20
    }]
}, {
    key : "Line 2",
    color : "#BD362F",
    values : [{
        x : 1373403179000,
        y : 60
    }, {
        x : 1373403469000,
        y : 50
    }, {
        x : 1373403567000,
        y : 70
    }]
}]
Run Code Online (Sandbox Code Playgroud)

UPDATE :这是NVD3 折线图 的工作小提琴