[英]Javascript - Convert timestamp into milliseconds for Highcharts Y axis
[英]Highcharts Y axis time in milliseconds
我正在嘗試創建一個Highcharts圖表,其中yAxis數據是以毫秒為單位的時間。 我希望yAxis標簽顯示適當的時間,即秒,分鍾等。 我嘗試設置yAxis.type ='datatime',但這將轉換為實際日期。 沒有它,使用下面的示例,由於明顯的原因,我的yAxis從0
變為100 M
。
[[1522187288000, 79692000], [1523314079000, 60000]]
這是我的配置,其中plot
是上面的數據:
{
chart: {
type: 'line'
},
credits: {
enabled: false
},
title: {
text: null
},
yAxis: {
title: {
text: null
}
},
xAxis: {
type: 'datetime'
},
legend: {
enabled: false
},
series: [{
'data': plot
}],
plotOptions: {
series: {
animation: {
duration: 0
}
}
},
}
編輯:我還需要點標簽來匹配yAxis。
通過結合以下問題的答案: 定制的Y軸格式化程序 將時間間隔轉換為人類可讀的形式,我們得到:
function millisecondsToStr (milliseconds) {
// TIP: to find current time in milliseconds, use:
// var current_time_milliseconds = new Date().getTime();
function numberEnding (number) {
return (number > 1) ? 's' : '';
}
var temp = Math.floor(milliseconds / 1000);
var years = Math.floor(temp / 31536000);
if (years) {
return years + ' year' + numberEnding(years);
}
//TODO: Months! Maybe weeks?
var days = Math.floor((temp %= 31536000) / 86400);
if (days) {
return days + ' day' + numberEnding(days);
}
var hours = Math.floor((temp %= 86400) / 3600);
if (hours) {
return hours + ' hour' + numberEnding(hours);
}
var minutes = Math.floor((temp %= 3600) / 60);
if (minutes) {
return minutes + ' minute' + numberEnding(minutes);
}
var seconds = temp % 60;
if (seconds) {
return seconds + ' second' + numberEnding(seconds);
}
return 'less than a second'; //'just now' //or other string you like;
}
...
yAxis: {
title: {
text: 'Time interval'
},
labels: {
formatter: function () {
return millisecondsToStr(this.value);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.