d3.js parsing date error with csv file

余生颓废 提交于 2019-12-05 04:50:41

问题


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:


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:)




回答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.



来源:https://stackoverflow.com/questions/19980581/d3-js-parsing-date-error-with-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!