2015-10-30 24 views
1

這是我撥弄相關代碼:http://jsfiddle.net/hfznh45w/13/c3js如何將一個時間戳記格式化爲自紀元至正常日期以來的秒數?

所以,我使用C3.js,並想使一個時間序列圖和時遇到的日期格式的問題。

這是我使用轉換紀元以來的秒數的日期字符串的內容:

function dateToString(e) { 
    var date = new Date(e * 1000); 
    return date.toDateString().substring(4); 
} 

,這裏是我如何我打電話上述功能:

x1: { 
    type: 'timeseries', 
    show: true, 
    tick: { 
    fit: true, 
    format: function(e, d){ 
     return dateToString(e) 
    } 
    } 
}, 

但似乎像那些永遠不會被調用的函數一樣(當我把它們放入函數時,console.logs從不被調用)。

回答

2

我的建議是,將數據轉換成正確的日期格式在數據層面

regTimes = []; 
incTimes = []; 
registrationTimes.forEach(function(e){regTimes.push(new Date(e * 1000))}); 

incomeTimes.forEach(function(e){incTimes.push(new Date(e * 1000))}); 

現在喂regTimes和incTimes的數據是這樣的:

columns: [ 
     ['x1'].concat(regTimes), 
     ['x2'].concat(incTimes), 
     ['Registrations'].concat(registrations), 
     ['Income'].concat(incomes) 
     ], 

還提供您選擇的日期刻度格式如下:

tick: { 
       format: function (x) { return x.getSeconds(); } 

      } 

完整的工作代碼here

希望這會有所幫助!

+0

感謝jsfiddle。這有助於一噸!雖然我不想將秒作爲我的標籤,但正確解析日期時間是主要問題。一旦我獲得格式/標籤間距,我會盡我所能發佈我的jsfiddle。 – NullVoxPopuli

+1

這裏是:http://jsfiddle.net/hfznh45w/17/再次感謝! – NullVoxPopuli

相關問題