简体   繁体   English

Flot Chart工具提示错误 - 工具提示日期是X轴日期的一天

[英]Flot Chart Tooltip Error - Tooltip Date is One Day Behind X-Axis Date

I haven't been able to understand why the tooltip date is off by a single day. 我无法理解为什么工具提示日期已经过了一天。 I believe the x-axis is correct, and I've been playing around with it, but this is killing me. 我相信x轴是正确的,我一直在玩它,但这是在扼杀我。

How can I fix this? 我怎样才能解决这个问题?

I'm passing in a JSON from a URL endpoint into variable jsonDataUrl. 我将一个JSON从一个URL端点传递给变量jsonDataUrl。 Here is an example datapoint: [{date: "2013-01-01", value: 50}] 以下是一个示例数据点: [{date: "2013-01-01", value: 50}]

And cssSelector is simply the placeholder. 而cssSelector只是占位符。

Here's my code: 这是我的代码:

  $.getJSON(jsonDataUrl, function(res) {
   var data = [];
    $.each(res, function(i, entry){
      data.push( [new Date(entry["date"]), entry["value"]] );
    });

    var opts = { yaxis: { min: 0},
                 xaxis: { mode: "time", timeformat: "%m-%d"},

                 series: { lines: { show: true }, points: { show: true } },

                 grid: {hoverable: true, clickable: true}
    };

    $.plot($(cssSelector), [data], opts);

    $(cssSelector).bind("plotclick", function(event, pos, item) {
        if (item) {

            var x = parseInt(item.datapoint[0]),
                y = item.datapoint[1];
            var date = (new Date(x));
            var day = date.getDate();
            var month = date.getMonth() + 1;
            var formattedDate = month + "-" + day;

            $("#tooltip").remove();
            var label = "date: " + formattedDate + "<br/> count: " + y;
            showTooltip(item.pageX, item.pageY, label);
        }
    });
  });

I hope this is OK, but I would like to answer my own question. 我希望这没关系,但我想回答我自己的问题。 I'm happy that I discovered this silly mistake! 我很高兴我发现了这个愚蠢的错误! I'm using Ruby on Rails to gather data and pass it to the Flot Chart via JSON url. 我正在使用Ruby on Rails收集数据并通过JSON url将其传递给Flot Chart。 Since the server records the data at UTC midnight, it was passing in the data to the tooltip at local timezone completely knocking it out of sync. 由于服务器在UTC午夜记录数据,因此它将数据传递到本地时区的工具提示,完全使其不同步。

So, I basically changed the date.getDate() and date.getMonth() to date.getUTCDate() and date.getUTCMonth(). 所以,我基本上将date.getDate()和date.getMonth()更改为date.getUTCDate()和date.getUTCMonth()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM