nvd3 - 在图表中将毫秒格式设置为 h:m:s(时间段,而不是日期)
nvd3 - Format Milliseconds to h:m:s in Chart (Period of Time, not Date)
我已经设法在 d3/nvd3 图表中显示日期,我喜欢这样传递毫秒值:
chart.xAxis
.tickFormat(function(d) {
return d3.time.format('%e, %H:%M')(new Date(d))
});
我想在 y 轴上将毫秒显示为时间段,例如“6 小时:23 分钟:5 秒”。
这可能吗?
提前致谢!
手筒
试试这个
d3.time.format('%H h : %M min : %S sec')(new Date())
它将像这样格式化日期
05 小时:34 分钟:26 秒
或者,如果您不喜欢数小时内的 0 填充,可以
d3.time.format('%-H h : %M min : %S sec')(new Date())
它将像这样格式化日期
5 小时:37 分钟:54 秒
编辑
在这种情况下,您需要放置一个 if 块来进行检查,并且 return 所需的格式如下所示:
.tickFormat(function(d) {
var date = new Date(d);
if (date.getHours() == 0){
return d3.time.format('%M min : %S sec')(d)
} else {
return d3.time.format('%-H h : %M min : %S sec')(d)
}
}
我已经设法在 d3/nvd3 图表中显示日期,我喜欢这样传递毫秒值:
chart.xAxis
.tickFormat(function(d) {
return d3.time.format('%e, %H:%M')(new Date(d))
});
我想在 y 轴上将毫秒显示为时间段,例如“6 小时:23 分钟:5 秒”。
这可能吗?
提前致谢!
手筒
试试这个
d3.time.format('%H h : %M min : %S sec')(new Date())
它将像这样格式化日期
05 小时:34 分钟:26 秒
或者,如果您不喜欢数小时内的 0 填充,可以
d3.time.format('%-H h : %M min : %S sec')(new Date())
它将像这样格式化日期
5 小时:37 分钟:54 秒
编辑
在这种情况下,您需要放置一个 if 块来进行检查,并且 return 所需的格式如下所示:
.tickFormat(function(d) {
var date = new Date(d);
if (date.getHours() == 0){
return d3.time.format('%M min : %S sec')(d)
} else {
return d3.time.format('%-H h : %M min : %S sec')(d)
}
}