在上周测试了 D3js 之后,我是 nvd3 的新手。我想用 nvd3 从 .csv 文件加载数据,我在 D3 上做过,但我不能用 nvd3 来做......有人知道怎么做吗?使用 D3,我编写下一行,并从我的 wather.csv 中获取数据:
d3.csv("weather.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.Hour);
d.T = +d.T;
})
});
考虑到我的 weather.csv 文件,例如:
"Hour";"T";
"25.04.2013 12:00";"18.7";
"25.04.2013 11:00";"18.5";
"25.04.2013 10:00";"18.4";
"25.04.2013 09:00";"18.9";
...
在 Nvd3 示例中,我只在代码中找到了变量,没有加载任何 .csv 或 .json 文件。我想从文件中加载数据并显示一个简单的条形图。
更新
在这里,我有我实现的代码,但它不起作用。我使用 D3 中的 .csv 函数,但导航器说数据不存在,我不知道为什么。
var parseDate = d3.time.format("%d.%m.%Y %H:%M").parse;
d3.csv("weather.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.Hour);
d.T = +d.T;
})
nv.addGraph(function() {
//console.log(data);
var chart = nv.models.discreteBarChart()
.x(function(d) { return d.date })
.y(function(d) { return d.T })
.staggerLabels(true)
//.staggerLabels(historicalBarChart[0].values.length > 8)
.tooltips(false)
.showValues(true);
d3.select('#chart1 svg')
//.datum(historicalBarChart)
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
});
谢谢。