gpt4 book ai didi

javascript - nvd3时间H :M:S displaying incorrectly in axis

转载 作者:行者123 更新时间:2023-11-27 22:49:34 26 4
gpt4 key购买 nike

我试图让 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/

最佳答案

首先,JavaScript new 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。

<小时/>

其次,您正在使用 d3.time.format ,转换为本地时间。您想使用d3.time.format.utc ,如果您始终需要 UTC。

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

已更新fiddle .

关于javascript - nvd3时间H :M:S displaying incorrectly in axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com