简体   繁体   English

将数字格式化为时间d3js

[英]Format number to time d3js

I have a dataset that looks like this 我有一个看起来像这样的数据集

date,lenght,sort
201506131449,2,1
201506131449,7,0
201506131450,35,1
201506131450,7,1

But then a lot more entries. 但是,接下来有很多条目。

De date is currently formated like 201506131450 which is 2015 - 06 - 13 14:50 where 14:50 is military time so (2:50pm) 日期的格式当前为201506131450,即2015年6月13日14:50,其中14:50是军事时间,因此(2:50 pm)

I need to keep the format like this for sorting purposes in python. 我需要保持这种格式,以便在python中进行排序。

Anyway. 无论如何。 Right now The labels output the huge number. 现在,标签输出了巨大的数字。 While I only want the military time part. 虽然我只想要军事时间。

So 201506131450 should be labeld 14:50 所以201506131450应该标为14:50

Current code: 当前代码:

var x = d3.scale.linear()
.range([0, width]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(5);

x.domain(d3.extent(data, function(d) { return d.date; }));

This produce the labels on the x axis like: 201,506,131,600 这将在x轴上产生标签,例如:201,506,131,600

How can I change that to 16:00 我如何将其更改为16:00

Thanks in advance! 提前致谢!

In your axis, set the tickFormat : 在您的轴上,设置tickFormat

.tickFormat(function(d){
    return d.toString().slice(8,10) + ":" + d.toString().slice(10);
});

Demo: 演示:

 var dates = [201506131449, 201506131449, 201506131450,201506131450]; dates.forEach(function(d){ console.log(d.toString().slice(8,10) + ":" + d.toString().slice(10)) }); 

PS: keep in mind that, for the scale, this is not a time, this is just a linear scale of numbers. PS:请记住,对于小数位数,这不是时间,这只是数字的线性刻度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM