【发布时间】:2016-04-22 05:32:06
【问题描述】:
我在脚本标签中有以下 d3 代码:
d3.csv("data.csv", function(error, data) {
data.forEach(function(d) {
console.log(data[0]);
d.date = parseDate(d.date);
d.close = +d.close;
});
console.log("hello2")
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);
svg.append("path") // Add the valueline path.
.attr("class", "line")
.attr("d", valueline(data));
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
});
当我在浏览器中检查控制台时,console.log 的输出是 Object {<!DOCTYPE html>: "</head>"}。我预计输出为{"date": "1-May-12", close: "58.13"}。
为什么回调函数使用我的 html 作为数据参数而不是我的 CSV 数据?请注意,我还在运行一个简单的节点服务器,因此我可以读取 CSV。
【问题讨论】:
-
同时粘贴 data.csv 中的内容
-
github.com/mbostock/d3/wiki/CSV#csv > 如果发生错误,回调函数将改为以 null 调用。函数(错误,数据)-> 函数(数据)请。
-
date,close 1-May-12,58.13 30-Apr-12,53.98 27-Apr-12,67.00 26-Apr-12,89.70 25-Apr-12,99.00这就是我的 CSV @Cyril 中的内容