To change the axis labels, use axis.tickFormat()
-- see e.g. this example.
Line chart in D3.js - parsing the date from JSON
-
16-07-2023 - |
Question
Right, so I'm trying to draw a simple line chart from a JSON source; but I am getting some errors which I can't figure out!
My d3.json() request should return :
[{"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.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow