[英]Line chart in D3.js - parsing the date from JSON
Right, so I'm trying to draw a simple line chart from a JSON source; 对,所以我试图从JSON源绘制一个简单的折线图。 but I am getting some errors which I can't figure out! 但是我遇到了一些我无法弄清的错误!
My d3.json() request should return : 我的d3.json()请求应返回:
[{"timestamp":1399325270,"value":-0.0029460209892230222598710528},{"timestamp":1399325271,"value":-0.0029460209892230222598710528},{"timestamp":1399325279,"value":-0.0029460209892230222598710528},....]
Here is my plotting script: 这是我的绘图脚本:
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);
});
The error that I'm getting looks something like this: 我收到的错误看起来像这样:
Error: Problem parsing d="MNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNaN,451.6363636363636LNa....
depending on the size of the data but you get the point! 取决于数据的大小,但是您明白了!
Any help is much appreciated as I have started to tear my hair out. 我已经开始梳理头发,对您的帮助非常感谢。
要更改轴标签,请使用axis.tickFormat()
-参见例如本示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.