Reputation: 369
I am trying to render a graph from a csv file based on the date using the library d3.
I can easily display the graph for the whole csv file but I would like (using the example below) to display only the data for dates from June for instance.
The csv file is as followed:
date, close
15/06/15,200
15/06/15,250
10/06/15,300
05/06/15,500
25/05/15,200
20/05/15,100
OK so parsing the dates works and I am comparing dates with matching format now. Still, something does not make sense.
d3.csv("txt.csv", function(error, data) {
data2 = data.filter(function(d) { return d.date <= timeFormat(parseDate("10/06/15")) });
data2.forEach(function(d) {
d.date = timeFormat(parseDate(d.date));
d.close = +d.close;
});
var timeFormat = d3.time.format("%d/%m/%y");
var parseDate = d3.time.format("%d/%m/%Y").parse;
When I console.log(data2); I do get some array so that means it is finding some of the lines, the problem is that it finds completely random lines from the csv that do not correspond at all to my <= condition.
P.S.: For clarity I simplified the csv. My real condition should pick up only 2 lines at the end of the csv but it gets hundreds of lines from the rest of the csv (but not all of the lines!!!!).
Any idea what's happening?
Upvotes: 1
Views: 1152
Reputation: 884
There is a caveat when using d3.time.format(). In javascript, when a new Date(yyyy, mm, dd) is created, the numbers of the months can be from 0 to 11. They are zero based. In d3.time.format(), the months are not zero based (going from 1 to 12).
interpreting what should be 15/06/15 as 2015-06-14T23:00:00.000Z...
That is a bit weird though. The d3 format (like you defined it) returns for me 15/07/15 when I do the following:
var someDate = new Date(2015,06,15);
var var timeFormat = d3.time.format("%d/%m/%y");
console.log(timeFormat(someDate));
So I actually get what I expect to get. Its not printing 2015-06-15T00:00:00 or something like that...
And then there is one more thing about dates in javascript. You can read more about it here. However, it is a bit of age and it does not explain why the d3.time.format would interpret something totally different. Could you perhaps post the entire code? Where did you notice the error? Or how did you notice?
Upvotes: 1