How to use d3.time.scale() to generate an array of evenly spaced dates?

前端 未结 2 1890
囚心锁ツ
囚心锁ツ 2021-01-15 08:13

This seems like it should be trivial. I want to use d3.time.scale() to get an array of evenly spaced dates that covers a certain range of time. For example, yea

2条回答
  •  孤城傲影
    2021-01-15 08:52

    Using the Date() constructor doesn't work reliably -- it basically depends on the browser. It's much safer to parse explicitly. The constructor doesn't parse as such.

    Instead of

    var dates = getDates().map(function(d) { return new Date(d) });
    

    use this:

    var dates = getDates().map(function(d) {
        return d3.time.format("%Y-%m-%d").parse(d);
    });
    

    Modified jsfiddle here.

提交回复
热议问题