如何格式化Highcharts的(x,y)对数据的日期时间 [英] How to format datetime for (x,y) pair data for Highcharts

查看:150
本文介绍了如何格式化Highcharts的(x,y)对数据的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的序列化方法产生如下日期时间字符串:2014-07-09T12:30:41Z

My serialization method results a datetime string like this: "2014-07-09T12:30:41Z"

为什么以下代码不起作用?

Why the following code does not work?

$(function () {
$('#container').highcharts({
    xAxis: {
        type: 'datetime'
    },

    series: [{
        data: [
            {x:"2014-07-09T12:30:41Z",y: 29.9},
            {x:"2014-09-09T12:30:41Z", y:71.5}
        ],
        name: "Teste"
    }]
});

});

此代码完美运行:

$(function () {
$('#container').highcharts({
    xAxis: {
        type: 'datetime'
    },

    series: [{
        data: [
            {x:Date.UTC(2014, 0, 1),y: 50},
            {x:Date.UTC(2014, 2, 1), y:20}
        ],
        name: "Teste2"
    }]
});

}) ;

如何转换日期时间格式或配置高级图表以使用我的数据?

How to convert datetime format or configure highcharts to work with my data?

推荐答案

显然,Highcharts必须期望日期为毫秒数自1970年1月1日00:00:00世界时,那就是 Date.UTC()检索,所以你可以完成同样的事情:

Apparently Highcharts must be expecting the date as the number of milliseconds since "January 1, 1970, 00:00:00" universal time, that's what Date.UTC() retrieves, so you can accomplish the same thing doing this:

series: [{
    data: [
        {x:(new Date("2014-07-09T12:30:41Z")).getTime(),y: 29.9},
        {x:(new Date("2014-09-09T12:30:41Z")).getTime(), y:71.5}
    ],
    name: "Teste"
}]

这篇关于如何格式化Highcharts的(x,y)对数据的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆