【问题标题】:D3: ordinal scale returning undefined in part of its rangeD3:序数比例在其部分范围内返回未定义
【发布时间】:2015-11-11 15:32:02
【问题描述】:

我有一个序数比例,它在其域的第二部分返回 undefined

// Return an array of all dates between date1 and date2 inclusive.
function days_in_range(date1, date2) {
    if (date1 > date2)
        return [];

    var date  = new Date(date1.getTime());
    var dates = [];
    while (date <= date2) {
        dates.push(new Date(date));
        date.setDate( date.getDate() + 1 );
    }
    return dates;
}

min_date = new Date("2015-10-12");
max_date = new Date('2015-11-10');
the_dates = days_in_range(min_date, max_date);
xx_scale = d3.scale.ordinal().domain(the_dates).rangeRoundBands([0, 790])

xx_scale(new Date('2015-10-12')); // Returns 4.
xx_scale(new Date('2015-10-24')); // Returns 328.
xx_scale(new Date('2015-10-25')); // Expect 355, but returns undefined.

从 2015 年 10 月 25 日到范围结束,我不确定。从域名开始到 2015 年 10 月 24 日,我得到了合理的数字。

days_in_range() 函数似乎返回正确。我在这里复制它只是为了完整。

max_date 设置为 2015-10-26 会产生相同的中断(在 24 和 25 之间)。

任何关于我做错了什么的指针?

【问题讨论】:

标签: javascript d3.js


【解决方案1】:

问题是双重的(如果我们把我算作杜弗斯的话,问题是三重的):

  1. 域值是字符串,所以在计算 the_days 后,我最好将其转换回年-月-日值。
  2. 夏令时更改发生在 2015 年 10 月 24 日和 2015 年 10 月 25 日之间。因此,Date 值从日期2015-10-24T00:00:00.000Z 更改为Date 2015-10-25T01:00:00.000Z,由days_in_range() 返回,但new Date('2015-10-25') 返回Date 2015-10-25T00:00:00.000Z。注意一小时的轮班。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多