我正在尝试使用d3创建折线图,但是使用javascript对象而不是像bl.ocks示例那样的tsv。但是,无论我如何尝试解析时间数据(写作年份,例如" 2003"),我都会收到错误:
Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…"
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 50},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseTime = d3.timeParse("%Y");
var x = d3.scaleTime()
.rangeRound([0, width]);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var line = d3.line()
.x(function(d) { return x( parseTime(d.year)); })
.y(function(d) { return y(d.locations); });
x.domain(d3.extent(myData, function(d) { return parseTime(d.year); }));
y.domain(d3.extent(myData, function(d) { return d.locations; }));
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.select(".domain")
.remove();
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Price ($)");
g.append("path")
.datum(myData)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
数据来自电子表格,然后转换为javascript对象。
myData=[{"year":"2000","locations":1286.0,"}]
我已尝试将%Y
更改为%y
,并且我已尝试格式化电子表格中的年份列,以便生成2000.0
和01/01/2000
而不是字符串。
myData=[{"year":2000.0,"locations":1286.0,"}]
我还使用了new Date()
代替parseTime
,但我不知道错误消息试图显示的内容