是的,所以我试图从JSON源中绘制一个简单的折线图;但我得到了一些我无法弄清楚的错误!
我的d3.json()请求应该返回:
[{"timestamp":1399325270,"value":-0.0029460209892230222598710528},{"timestamp":1399325271,"value":-0.0029460209892230222598710528},{"timestamp":1399325279,"value":-0.0029460209892230222598710528},....]
这是我的绘图脚本:
var margin = { top: 20, right: 20, bottom: 30, left: 50 },
width = document.getElementById("chartArea").offsetWidth - margin.left - margin.right,
height = document.getElementById("chartArea").offsetHeight - margin.top - margin.bottom;
var parseDate = d3.time.format("%X");
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function (d) { return x(d.timestamp); })
.y(function (d) { return y(d.value); });
var svg = d3.select("#chartArea").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//var series = [];
d3.json("/cryptic/performance/benchmark/all", function (error, data) {
data.forEach(function (d) {
d.timestamp = parseDate((new Date(d.timestamp*1000)));
d.value = (+d.value) * 100;
//series.push([d.timestamp, d.value]);
});
x.domain(d3.extent(data, function (d) { return d.timestamp; }));
y.domain(d3.extent(data, function (d) { return d.value; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
console.log(data[0])
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
我得到的错误看起来像这样:
Error: Problem parsing d="MNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNa....
取决于数据的大小,但你明白了!
任何帮助都非常受欢迎,因为我已经开始撕掉我的头发了。