d3 中的 d3.time.interval.range() 不返回等间距间隔



我使用下面的函数来创建相等间隔的时间间隔:

d3.time.second.range(new Date(1444717315000), new Date(1444717615000), 38)

var _text = "";
d3.time.second.range(new Date(1444717315000), new Date(1444717615000), 38).forEach(function(d) {
  
  _text = _text + d.toString();
 _text = _text + '<br>';
  
  
})
document.getElementById("timearray").innerHTML = _text
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="timearray">

在d3 API中指定为:

#间隔。范围(start, stop[, step])

返回开始之后或等于停止之前的每个时间间隔。如果指定了step,则将根据间隔数返回每一步间隔(例如d3.time.day是当月的第几天)。例如,步骤2将返回d3.time.day中当月的第1、3、5等日期。

我希望它在38秒内返回相等间隔的结果数组,例如:

0: Tue Oct 13 2015 11:52:00 GMT+0530 (India Standard Time)
1: Tue Oct 13 2015 11:52:38 GMT+0530 (India Standard Time)
2: Tue Oct 13 2015 11:53:16 GMT+0530 (India Standard Time)
3: Tue Oct 13 2015 11:53:54 GMT+0530 (India Standard Time)
..
..

我得到的是(不等距):

0: Tue Oct 13 2015 11:52:00 GMT+0530 (India Standard Time)
1: Tue Oct 13 2015 11:52:38 GMT+0530 (India Standard Time)
2: Tue Oct 13 2015 11:53:00 GMT+0530 (India Standard Time)
3: Tue Oct 13 2015 11:53:38 GMT+0530 (India Standard Time)
4: Tue Oct 13 2015 11:54:00 GMT+0530 (India Standard Time)
5: Tue Oct 13 2015 11:54:38 GMT+0530 (India Standard Time)
6: Tue Oct 13 2015 11:55:00 GMT+0530 (India Standard Time)
7: Tue Oct 13 2015 11:55:38 GMT+0530 (India Standard Time)
8: Tue Oct 13 2015 11:56:00 GMT+0530 (India Standard Time)
9: Tue Oct 13 2015 11:56:38 GMT+0530 (India Standard Time)

这个用法有问题吗?或者API意味着别的什么?我们有不同的函数吗?

time尊重Date对象的结构,而不只是将其作为十进制数处理,我猜这是描述您正在寻找的内容的一种方式。

因此,为了达到你想要的效果,你不需要使用d3。时间,而只是生成一系列间隔38000毫秒的数字。

var _text = "";
d3.range(1444717320000, 1444717615000, 38000)
.map(function(t){return new Date(t)})
.forEach(function(d) {
  
  _text = _text + d.toString();
 _text = _text + '<br>';
  
  
})
document.getElementById("timearray").innerHTML = _text
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="timearray">

最新更新