d3.js parsing date error with csv file

二次信任 提交于 2019-12-03 21:14:56

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

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.

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