Display tooltip with correct Time zone using Flot jQuery plugin
Asked Answered
C

3

17

I have a little problem with the Flot plugin while displaying the xaxis labels in the graph. They are 'mode: "time"'. Currently I use Flot with the tooltip function and the tooltip contains a date and time. I supply JSON to the plugin which contains timestamps. Afterwards I convert the timestamp and I display it in the tooltip. The problem is that while displaying the data in the graph, the times from the tooltips don't correspond to the xaxis labels generated by the plugin due to a difference between the timezones. My JSON timestamps are +2 GMT, but the xaxis labels in Flot are +0 GMT. So I wonder if there is a possibility to set an offset to the time zone or something similar.

My JSON (generated by AJAX)

 [1300087800000,29],
 [1300088700000,39],
 [1300089600000,46],
 [1300090500000,53],
 [1300091400000,68],
 [1300092300000,95],
 ...

My tooltip function

$(placeholder).bind("plothover", function (event, pos, item) {
     $("#tooltip").remove();

     var x = item.datapoint[0].toFixed(2);
     var y = item.datapoint[1].toFixed(2);

     var currDate   = new Date(Math.floor(x));
     var hour       = currDate.getHours();
     var minute     = String("") + currDate.getMinutes();

     var tooltip = hour + ":" +
                   ((minute.length < 2) ? "0" + minute : minute) + " " +
                   (Math.round(y * 100)/100) + "Wh"
     showTooltip(item.pageX, item.pageY, tooltip);
 });  

The Flot options

 var plotOptions = {  
     lines:  { show: true, lineWidth: 1 },  
     points: { show: false, symbol: "cross" },  
     xaxis:  {  
         mode:   "time",  
         tickLength: 5,  
         timeZoneOffset: (new Date()).getTimezoneOffset()  
     },  
     selection: { mode: "x", color: "#BCBCBC" },
     grid:      { hoverable: true, clickable: false }
};

but unfortunately timeZoneOffset doesn't work and I have still differences between the xaxis and the tooltips.

Do you have any ideas how I should resolve my problem?

Convulse answered 14/4, 2011 at 13:15 Comment(5)
what is the timezone offset provided by (new Date()).getTimezoneOffset()?Caseose
@justki "-120", it's expressed in minutesConvulse
Nothing matter what I put, the difference between the xaxis and the tooltip times is always 2 hours...Convulse
@vlad tried putting in minutes and does not have an effectUfo
I too faced this, it was because I was missing this file jquery.flot.time.jsAcree
O
23

You can try to use timezone instead of timeZoneOffset. your options look like:

var plotOptions = {  
     lines:  { show: true, lineWidth: 1 },  
     points: { show: false, symbol: "cross" },  
     xaxis:  {  
          mode:   "time",  
          tickLength: 5,  
          timezone: "browser" // "browser" for local to the client or timezone for timezone-js  
          },  
    selection: { mode: "x", color: "#BCBCBC" },
    grid:      { hoverable: true, clickable: false }
    };

My flot version is 0.7

Outrage answered 16/10, 2012 at 0:37 Comment(1)
I expected this would work, but nothing changed, unfortunately. I'm also on .7Outdoor
C
4

If you look at the flot issue database, issue 141 addresses timezones. Issue 484 suggesting the syntax you use was merged into this issue.

The documentation says:

Normally you want the timestamps to be displayed according to a certain time zone, usually the time zone in which the data has been produced. However, Flot always displays timestamps according to UTC. It has to as the only alternative with core Javascript is to interpret the timestamps according to the time zone that the visitor is in, which means that the ticks will shift unpredictably with the time zone and daylight savings of each visitor.

So given that there's no good support for custom time zones in Javascript, you'll have to take care of this server-side.

So the correct solution is to make your data look like UTC server-side (even if it isn't). If you cannot alter your data source you may want to consider proxying it. Server-side languages should allow timezone manipulation.

Alternatively follow issue 141 and watch for patches or plugins.

Caseose answered 14/4, 2011 at 14:25 Comment(0)
H
0

for UTC timeStamps, use UTC time functions:

var hour = currDate.getUTCHours();  // instead of getHours()
var minute = String("") + currDate.getUTCMinutes(); // instead of getMinutes()

and remove xaxis timezone.

Hustle answered 15/3, 2016 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.