簡體   English   中英

將數字格式化為時間d3js

[英]Format number to time d3js

我有一個看起來像這樣的數據集

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

但是,接下來有很多條目。

日期的格式當前為201506131450,即2015年6月13日14:50,其中14:50是軍事時間,因此(2:50 pm)

我需要保持這種格式,以便在python中進行排序。

無論如何。 現在,標簽輸出了巨大的數字。 雖然我只想要軍事時間。

所以201506131450應該標為14:50

當前代碼:

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; }));

這將在x軸上產生標簽,例如:201,506,131,600

我如何將其更改為16:00

提前致謝!

在您的軸上,設置tickFormat

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

演示:

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

PS:請記住,對於小數位數,這不是時間,這只是數字的線性刻度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM