2

I have a problem with parsing dates in a csv file. Have been looking for hours online and in books and testing, without finding the solution.

Perhaps someone can help.

The code works fine for reading a file with parsing just numbers. But when parsing a file with dates I get the following error message indicating the date format is not recognised:

Problem parsing d="MNaN,268.5466377440347LNaN,117.78741865509761LNaN ...

The file looks like this:

 date,value
 11-11-13,582
 12-11-13,860
 13-11-13,940

Code: (js)

function myFunction() {
      d3.csv('data/Date.csv', draw);
}

function draw(data) {
    "use strict";
    var margin = 50,
        width = 800 - margin,
        height = 350 - margin;

    var parseDate = d3.time.format("%d-%m-%y").parse;

    var x_scale = d3.time.scale()
        .domain(d3.extent(data,function(d){return d.date}))
        .range([margin, width]);

    var y_scale = d3.scale.linear()
       .domain(d3.extent(data,function(d){return d.value}))
       .range([height, margin]);

    var x_axis = d3.svg.axis()
        .scale(x_scale)
        .orient("bottom");

    var y_axis = d3.svg.axis()
        .scale(y_scale)
        .orient("left");

    var line = d3.svg.line()
        .x(function(d){return x_scale(d.date);})
        .y(function(d){return y_scale(d.value);});

    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.value = +d.value;
    });

    d3.select("body") 
      .append("svg")
        .attr("width", width+margin)
        .attr("height", height+margin)
      .append('g')
        .attr("transform","translate(" + margin + "," + margin + ")");

    d3.select('svg')
      .append('path')
        .datum(data)
        .attr ("class","line")
        .attr('d', line);

    d3.select("svg")
      .append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(x_axis);

    d3.select("svg")
      .append("g")
      .attr("class", "y axis")
      .attr("transform", "translate(" + margin + ",0)")
      .call(y_axis);
}

<body>
<button onclick="myFunction()">Show Graph</button>
</body>
1
  • Remark: this is written in js Commented Nov 14, 2013 at 14:50

2 Answers 2

5

You should move the following code

data.forEach(function(d) {
    console.log(d.date);
    console.log(format.parse(d.date));
    d.date = format.parse(d.date);
    d.value = +d.value;
});

Just after

var format = d3.time.format("%d-%m-%y");

Your original code used the parsed date before the date is parsed:)

Sign up to request clarification or add additional context in comments.

Comments

2

You are setting the domain of your x scale before actually parsing the dates, i.e. the scale will expect strings and not dates as input. You need to move the code

data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.value = +d.value;
});

before

var x_scale = d3.time.scale()
    .domain(d3.extent(data,function(d){return d.date}))
    .range([margin, width]);

In general, you should do any processing right at the start of the handler function.

1 Comment

Thank you. This is indeed the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.