Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcharts - time off by 1 hour

I cant seem to work out why the time on the x axis is always an hour behind.

I know its got something to do with this line but I cant work out what to change it to.

 date = Date.parse(line[0] + ' UTC');

My current timezone is London.

I've got this file: index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Using Highcharts with PHP and MySQL</title>

<script type="text/javascript" src="js/jquery-1.7.1.min.js" ></script>
<script type="text/javascript" src="js/highcharts.js" ></script>
<script type="text/javascript" src="js/themes/gray.js"></script>

<script type="text/javascript">
    var chart;
                    $(document).ready(function() {
                            var options = {
                                    chart: {
                                            renderTo: 'container',
                                            defaultSeriesType: 'line',
                                            marginRight: 130,
                                            marginBottom: 25
                                    },
                                    title: {
                                  text: 'Temperature for the last hour',
                                            x: -20 //center
                                    },
                                    subtitle: {
                                            text: '',
                                            x: -20
                                    },
                                    xAxis: {
                                            type: 'datetime',
                                            tickInterval: 3600 * 1000, // one hour
                                            tickWidth: 0,
                                            gridLineWidth: 1,
                                            labels: {
                                                    align: 'center',
                                                    x: -3,
                                                    y: 20,
                                                    formatter: function() {
                                                            return Highcharts.dateFormat('%H:%M', this.value);
                                                    }
                                            }
                                    },
                                    yAxis: {
                                            title: {
                                                    text: 'Temperature'
                                            },
                                            plotLines: [{
                                                    value: 0,

                                                   width: 1,
                                                    color: '#808080'
                                            }]
                                    },
                                    tooltip: {
                                            formatter: function() {
                                            return Highcharts.dateFormat('%H:%M', this.x-(1000*3600)) +': <b>'+ this.y + '</b>';
                                            }
                                    },
                                    legend: {
                                            layout: 'vertical',
                                            align: 'right',
                                            verticalAlign: 'top',
                                            x: -10,
                                            y: 100,
                                            borderWidth: 0
                                    },
                                    series: [{
                                            name: 'Temperature'
                                    }]
                            }
                            // Load data asynchronously using jQuery. On success, add the data
                            // to the options and initiate the chart.
                            // This data is obtained by exporting a GA custom report to TSV.
                            // http://api.jquery.com/jQuery.get/
                            jQuery.get('data.php', null, function(tsv) {

                              var lines = [];
                                    traffic = [];
                                    try {
                                            // split the data return into lines and parse them
                                            tsv = tsv.split(/\n/g);
                                            jQuery.each(tsv, function(i, line) {
                                                    line = line.split(/\t/);
                                                    date = Date.parse(line[0]);
                                                    traffic.push([
                                                            date,
                                                            parseFloat(line[1].replace(',', ''), 10)
                                                    ]);
                                            });
                                    } catch (e) {  }
                                    options.series[0].data = traffic;
                                    chart = new Highcharts.Chart(options);
                            });
                    });
</script>
</head>
<body>

<div id="container" style="width: 100%; height: 400px; margin: 0 auto"></div>

</body>
</html>

I've tried fiddling around with settings like

date = Date.parse(line[0] + ' Europe/London');

date = Date.parse(line[0] + ' London');                    

I have also tried putting this at the top of the php file:

<?php date_default_timezone_set('Europe/London'); ?>  

data.php spits out:

2013-09-15 11:49:42 18.3 2013-09-15 11:52:42    18.4 2013-09-15 11:55:42    18.4 2013-09-15 11:58:42    18.4 2013-09-15 12:01:42    18.3 2013-09-15 12:04:42    18.5 2013-09-15 12:07:42    18.6 2013-09-15 12:10:43 18.6 2013-09-15 12:13:43   18.8 2013-09-15 12:16:43    19 2013-09-15 12:19:43  19.3 2013-09-15 12:22:43    19.4 2013-09-15 12:25:43    19.5 2013-09-15 12:28:43    19.6 2013-09-15 12:31:43    19.8 2013-09-15 12:34:45 20.1 2013-09-15 12:37:43   20.1 2013-09-15 12:40:43    20.2 2013-09-15 12:43:43    20.3 2013-09-15 12:46:43    20.3

where the times are correct.

I've now added in:

    Highcharts.setOptions({
            global: { useUTC: false }
                    });

The tickmarks are correct but the times displayed on the tooltip are still an hour behind.

Update: Found the problem - I need to add the useUTC: false option as suggested, and then change the following line as well:

 return Highcharts.dateFormat('%H:%M', this.x-(1000*3600)) +': <b>'+ this.y + '</b>';

to

 return Highcharts.dateFormat('%H:%M', this) +': <b>'+ this.y + '</b>';
like image 859
Greg Avatar asked Oct 02 '22 23:10

Greg


2 Answers

London uses BST during the summer and GMT during the winter. See: here

BST is the equivalent to UTC +1. That's important information to know for this reason: the result you are getting back is UTC, not BST. If you wish to retrieve the local time, then it is necessary to set the global option for useUTC: false. See: here.

This page gives an example of how to properly set your global options: link

Essentially, what you need to add is the following:

global: {
  useUTC: false
}
like image 198
thtsigma Avatar answered Oct 07 '22 19:10

thtsigma


For me the useUTC: false was enough. Maybe you can try also timezoneOffset. But for me it works with useUTC.

My full code looks like this:

Highcharts.setOptions({
    global: {
        // timezoneOffset: +1,
        useUTC: false
    }
});

More info will be in documentation - http://api.highcharts.com/highcharts#global

like image 23
Jan Jůna Avatar answered Oct 07 '22 19:10

Jan Jůna