nvd3 时间 H:M:S 在轴上显示不正确

nvd3 time H:M:S displaying incorrectly in axis

我试图让 y2 轴(在右侧)以小时、分钟和秒显示,但返回的值不正确。

我在 GMT 中有以下 unix 时间戳:

        [1396303200000, 1481501473], //should display on y2axis as 00:11:13
        [1398895200000, 1467497437], //should display on y2axis as 00:10:37
        [1401573600000, 1481501521], //should display on y2axis as 00:12:01
        [1404165600000, 1481501493], //should display on y2axis as 00:11:33
        [1406844000000, 1481501630], //should display on y2axis as 00:13:50
        [1409522400000, 1481501668], //should display on y2axis as 00:14:28
        [1412114400000, 1481501615], //should display on y2axis as 00:13:35
        [1414796400000, 1481501605], //should display on y2axis as 00:13:25
        [1417388400000, 1481501557], //should display on y2axis as 00:12:37
        [1420066800000, 1481501567], //should display on y2axis as 00:12:47
        [1422745200000, 1481501501]  //should display on y2axis as 00:11:41

但是,nvd3 图表中显示的值根本不是我所期望的。如果我使用纪元转换器,我确实看到了我所期望的值。

已尝试以下代码,但值仍然显示不正确:

      chart.y2Axis
          .tickFormat(function(d) {
              return d3.time.format('%H:%M:%S')(new Date(d))
           });

也尝试过:

return d3.time.format('%X')(new Date(d))

谁能帮忙解释一下可能是什么问题?

完整工作 fiddle 和代码在这里 http://jsfiddle.net/wrcjp79o/2/

首先是JavaScriptnew Date(value)构造函数expects:

value - Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch; but consider that most Unix time stamp functions count in seconds).

你给了它秒,所以乘以 1000。


其次,如果您始终需要 UTC,则您正在使用 d3.time.format, which converts to local time. You want to be using d3.time.format.utc


  chart.y2Axis
      .tickFormat(function(d) {
          return d3.time.format.utc('%H:%M:%S')(new Date(d * 1000))              
       });

已更新 fiddle