我正在使用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'未定义"