Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format datetime for (x,y) pair data for Highcharts

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"
    }]
});

});

this code work perfectly:

$(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?

like image 582
Rafael Costa Avatar asked Oct 22 '25 06:10

Rafael Costa


1 Answers

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"
}]
like image 143
Josep Avatar answered Oct 23 '25 21:10

Josep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!