2

为什么以下范围功能...

d3.time.minute.range(
    new Date('Sat Aug 17 2013 00:00:00 GMT-0400'),
    new Date('Sat Aug 17 2013 06:00:00 GMT-0400'),
    22);

...返回一个这样的数组...

[
Sat Aug 17 2013 00:00:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 00:22:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 00:44:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 01:00:00 GMT-0400 (Eastern Daylight Time), // << normalized to 1am
Sat Aug 17 2013 01:22:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 01:44:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 02:00:00 GMT-0400 (Eastern Daylight Time), // << normalized to 2am
...
]

...而不是像这样?

[
Sat Aug 17 2013 00:00:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 00:22:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 00:44:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 01:06:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 01:28:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 01:50:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 17 2013 02:12:00 GMT-0400 (Eastern Daylight Time),
...
]

我正在尝试渲染时间线,然后用于d3.time.scale关联像素和时间。rect在这个例子中,我渲染的每个代表 22 分钟。当我绑定.data()到这个范围函数的结果时,我的text元素在所有情况下都不能清楚地表示实际时间。

d3.time.minute.range如果我自己避免使用并简单地添加 22 分钟 ( ) ,我会遇到什么样的问题.getTime() + 22 * 60 * 1000

4

1 回答 1

2

根据 mbostock 在这里所说的,我认为你需要自己动手:http: //grokbase.com/t/gg/d3-js/1344en8cz8/time-scale-always-forcibly-display-first-date-of-一个月

所以:

d3.time.minute.range(
    new Date('Sat Aug 17 2013 00:00:00 GMT-0400'),
    new Date('Sat Aug 17 2013 06:00:00 GMT-0400'))
.filter(function(d) {
    return (d - new Date('Sat Aug 17 2013 00:00:00 GMT-0400')) % (22 * 60 * 1000) == 0;
});

在这里小提琴:http: //jsfiddle.net/JHTQf/

至于问题,我认为两者都可以,因为浏览器没有闰秒。我会考虑 DST,但我认为这是通过 UTC 计算解决的。

于 2013-08-30T15:37:05.580 回答