Javascript 使用 highcharts 处理 unix 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10160822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:07:23  来源:igfitidea点击:

Handling unix timestamp with highcharts

javascriptjqueryhighcharts

提问by Hedde van der Heide

jsfiddle:http://jsfiddle.net/RjPRd/

jsfiddle:http : //jsfiddle.net/RjPRd/

Times & Labels are displayed incorrectly.

时间和标签显示不正确。

I think the timestamp should be multiplied by 1000 for Javascript Time but what's the best approach? Also I believe the setup is still incorrect because the labels seem opposite to where the cursor is.

我认为 JavaScript 时间的时间戳应该乘以 1000,但最好的方法是什么?另外我认为设置仍然不正确,因为标签似乎与光标所在的位置相反。

回答by Claude Vedovini

You are right, timestamps in Javascript are milliseconds so you should multiply everything by 1000.

您是对的,Javascript 中的时间戳是毫秒,因此您应该将所有内容乘以 1000。

For the other problem it comes from the fact that your data is ordered backwards. Apparently HighCharts is messing up when the series are not properly ordered.

对于另一个问题,它来自您的数据向后排序的事实。显然,当系列没有正确排序时,HighCharts 就搞砸了。

Here's the correction for your code: http://jsfiddle.net/cvedovini/RjPRd/2/

这是您的代码的更正:http: //jsfiddle.net/cvedovini/RjPRd/2/

回答by Fellipe Sanches

An easy way to work with timestamp (milliseconds) in Highcharts is use the formatter. So first receive your time values as unix timestampand then set one of the features below in the chart:

在 Highcharts 中使用时间戳(毫秒)的一种简单方法是使用formatter。因此,首先将您的时间值作为unix 时间戳接收,然后在图表中设置以下功能之一:

Using in xAxis labels:

在 xAxis 标签中使用:

xAxis:[{
  labels:{
     formatter:function(){
         return Highcharts.dateFormat('%Y %M %d',this.value);
     }
  }
}]

Using in tooltip:

在工具提示中使用:

tooltip: {
    readerFormat: {
        formatter: function(){
         return Highcharts.dateFormat('%Y %M %d',this.value);
     }
  },
    pointFormat: '{point.y} ms',
    shared: true
},

An exemple of code with tooltip

带有工具提示的代码示例

A reference about formatter

关于格式化程序的参考