【问题标题】:Highchart timedate formatHighchart 时间日期格式
【发布时间】:2016-08-05 18:21:43
【问题描述】:

我的 highchart 几乎完成了,但我无法让日期时间/时间戳看起来正确。我不确定它是 xAxis 的格式,还是系列的格式。

我的数据: [{"x":"2016-04-08 12:11:02","y":32},{"x":"2016-04-08 14:22:07","y":2},{"x":"2016-04-11 10:10:06","y":4},{"x":"2016-04-11 11:56:35","y":2},{"x":"2016-04-11 12:16:20","y":2},{"x":"2016-04-11 14:09:27","y":2},{"x":"2016-04-11 15:03:31","y":1},{"x":"2016-04-11 20:18:41","y":1172},{"x":"2016-04-11 21:00:06","y":1014}]

$('#container').highcharts('StockChart', {
        rangeSelector : {
            selected : 1
        },
        xAxis: {
            type: 'datetime'
        },
        title : {
            text : 'test'
        },
        series : [{
            name : 'signups',
            data : data,
            turboThreshold : 0
        }]
    });

我确定这是我错过的一件小事。谢谢

【问题讨论】:

  • 它不理解您的时间戳格式,因此它显示的是从 unix 时间(1970 年 1 月 1 日)开始的毫秒数。为每个点创建新日期(您的时间戳)或以纪元毫秒为单位传递时间。
  • 如果你能分享小提琴会很有帮助。
  • 所以这可能是 wh..,它需要 unixtime 中的时间,但我在 yyyy-mm-dd hh-mm-ss 中给出它。
  • 该点的格式应为:[timestamp, value] 或 {x:timestmap, value},其中 value 为数字,timestamp 为以毫秒为单位的时间(即由 Date.UTC() 返回); .因此,您需要在后端或预处理期间将日期转换为正确的格式。

标签: javascript json datetime highcharts timestamp


【解决方案1】:

对于您的每个日期,将其转换为 Date.UTC

var date = new Date(yourData[i].x);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
console.log(new Date(Date.UTC(year, month, day, hours, minutes, seconds)));

然后将其插入您的系列 可能会很长,但它会起作用。 看看这个FIDDLE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多