I'm importing some data from Quandl's API to make a chart of brent oil prices over the years. I'm serving the data from a HTTP-request made with Angular. Somehow the data being served is not being read as numbers, since i get the following error:
Error: attribute d: Expected number, "MNaN,NaNLNaN,NaNL…".
app.service('oilService', function($http) {
delete $http.defaults.headersmon['X-Requested-With'];
this.getData = function() {
var URL = ".json";
return $http({
method: 'GET',
url: URL
})
}
})
.directive('oilGraph', function() {
return {
scope: {},
controller: function(oilService) {
var width = 200;
var height = 100;
var parseTime = d3.timeParse("%y-%m-%d");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.settle); });
var svg = d3.select("#oilgraph").append("svg")
.attr("width", width)
.attr("height", height);
oilService.getData()
.then(function(data) {
var mapped_data = data.data.data.map(function(d) {
return {
date: parseTime(d[0]),
settle: d[4]
};
})
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.settle; })]);
svg.append("path")
.data([mapped_data])
.attr("class", "line")
.attr("d", valueline);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.call(d3.axisLeft(y));
})
})
}
}
})
I'm importing some data from Quandl's API to make a chart of brent oil prices over the years. I'm serving the data from a HTTP-request made with Angular. Somehow the data being served is not being read as numbers, since i get the following error:
Error: attribute d: Expected number, "MNaN,NaNLNaN,NaNL…".
app.service('oilService', function($http) {
delete $http.defaults.headers.mon['X-Requested-With'];
this.getData = function() {
var URL = "https://www.quandl./api/v1/datasets/CHRIS/ICE_B1.json";
return $http({
method: 'GET',
url: URL
})
}
})
.directive('oilGraph', function() {
return {
scope: {},
controller: function(oilService) {
var width = 200;
var height = 100;
var parseTime = d3.timeParse("%y-%m-%d");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.settle); });
var svg = d3.select("#oilgraph").append("svg")
.attr("width", width)
.attr("height", height);
oilService.getData()
.then(function(data) {
var mapped_data = data.data.data.map(function(d) {
return {
date: parseTime(d[0]),
settle: d[4]
};
})
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.settle; })]);
svg.append("path")
.data([mapped_data])
.attr("class", "line")
.attr("d", valueline);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.call(d3.axisLeft(y));
})
})
}
}
})
Share
Improve this question
asked Jun 30, 2017 at 12:07
SudokuNinjaSudokuNinja
4391 gold badge6 silver badges16 bronze badges
7
- 1 Share your JSON (just a few objects) – Gerardo Furtado Commented Jun 30, 2017 at 12:09
-
@GerardoFurtado
{ "date": null, "settle": 47.42 }
– SudokuNinja Commented Jun 30, 2017 at 12:19 - @GerardoFurtado Well, the date should've been there. Not sure why it's not parsing correctly. It's the same for all the other objects, "settle"-data is retrieved correctly, but all the date-values are null. – SudokuNinja Commented Jun 30, 2017 at 12:20
-
1
null
doesn't help us to see the date objects at all. The best idea is creating a plunker/codepen/fiddle/whatever with your json, even if it doesn't work properly, so we can investigate what's happening. – Gerardo Furtado Commented Jun 30, 2017 at 12:33 - 1 The raw json data. – Gerardo Furtado Commented Jun 30, 2017 at 12:53
2 Answers
Reset to default 3Your problem is just a wrong specifier. Since your dates have the century...
"2017-06-16"
... you should use %Y
instead of %y
. According to the API:
%Y - year with century as a decimal number.
Thus, this should be your specifier:
var parseTime = d3.timeParse("%Y-%m-%d");
Besides that, you have to use mapped_data
in the scales, not data
.
Here is your code with that changes:
var width = 500;
var height = 200;
var parseTime = d3.timeParse("%Y-%m-%d");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var valueline = d3.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.settle);
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("https://www.quandl./api/v1/datasets/CHRIS/ICE_B1.json", function(data) {
var mapped_data = data.data.map(function(d) {
return {
date: parseTime(d[0]),
settle: d[4]
};
});
x.domain(d3.extent(mapped_data, function(d) {
return d.date;
}));
y.domain([0, d3.max(mapped_data, function(d) {
return d.settle;
})]);
svg.append("path")
.data([mapped_data])
.attr("class", "line")
.attr("d", valueline);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.call(d3.axisLeft(y));
})
path {
fill: none;
stroke: black;
}
<script src="https://d3js/d3.v4.min.js"></script>
As far as i can see, in your code at
svg.append("path")
.data([mapped_data])
.attr("class", "line")
.attr("d", valueline);`
.attr("d", valueline);, "valueline is giving you nan", either you want to use property of value line. i.e x and y property of it you setting it in above of your code.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744730726a4590451.html
评论列表(0条)