使用d3嵌套分组时按时区偏移日期

时间:2013-12-27 19:10:01

标签: javascript d3.js coffeescript

我有一个d3函数(如下所示)来分组如下所示的数据:

[{"count":17,"created_at":"2012-08-20T04:10:08Z","id":1,"notes":"","occurred_at":"2012-08-19T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T04:10:08Z"},{"count":25,"created_at":"2012-08-20T04:11:06Z","id":4,"notes":"","occurred_at":"2012-08-18T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T04:11:06Z"},{"count":40,"created_at":"2012-08-20T21:05:47Z","id":94,"notes":null,"occurred_at":"2012-07-16T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T21:05:47Z"},{"count":10,"created_at":"2012-08-20T04:13:38Z","id":8,"notes":"","occurred_at":"2012-08-17T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T04:13:38Z"},{"count":9,"created_at":"2012-08-20T20:35:19Z","id":11,"notes":null,"occurred_at":"2012-08-13T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T20:35:19Z"}]

这似乎产生了正确的结果,但是我想用我的时区(当前EST)来偏移已解析的日期,以便分组是针对当前时区而不是UTC。此外,如果有人知道更好的方法来分组我的数据,我很乐意看到它。

  @group_data: (inputData) ->
    format = d3.time.format.iso
    gformat = d3.time.format("%Y-%m-%d")
    array = d3.nest()
      .key((d) ->
        gformat(format.parse(d.occurred_at))
      )
      .entries(inputData)
      .map((d) ->
        group = d.key
        total = 0
        d.values.forEach((day) ->
          total = total + day.count
        )
        return {'group': group, total:total }
      )
    obj = {}
    array.forEach((d) -> obj[d.group] = d.total)
    obj

1 个答案:

答案 0 :(得分:0)

您可以使用timezone-js在时区之间转换日期。