Remove hours from time series

让人想犯罪 __ 提交于 2019-12-11 19:39:36

问题


Dygraphs allows easy display of time series...

However, if my data contains only two data points, it automatically fills the gaps in X axis with hours. Is it possible to disable this functionality?
I searched and tried many options but not found anything useful.

Example might be the 'Time Series Drawing Demo' from the gallery - if executed on only few datapoints, it fills the 'gaps' with hours.

This is a good example:

g = new Dygraph(document.getElementById('plot'),"a,b\n2008-12-01,0.9\n2008-12-02,0.3\n2008-12-03,0.7\n")

UPDATE- this seems to be working:

ticker: function(a, b, pixels, opts, dygraph, vals) {
  var chosen = Dygraph.pickDateTickGranularity(a, b, pixels, opts);
  if(chosen==12) chosen=13;
  if (chosen >= 0) {
    return Dygraph.getDateAxis(a, b, chosen, opts, dygraph);
  } else {
    // this can happen if self.width_ is zero.
    return [];
  }
};

回答1:


Your issue is not that you have two points, but that your points cover a certain amount of time. Dygraphs tries to calculate the best granularity for the x axis tick marks in a given set of data.

One way to modify the default calculation is by using the pixelsPerLabel option.

Example: http://jsfiddle.net/kaliatech/P8ehg/

var data = "a,b\n2008-12-01,0.9\n2008-12-02,0.3\n2008-12-03,0.7\n";
g = new Dygraph(document.getElementById("plot"), data, {
  axes: {
    x: {
        pixelsPerLabel: 100
      }
    }
});

This requires hard coding a pixel width though, and it is still ultimately dependent on the data set that you are graphing. A more flexible approach might be to use the ticker option, allowing you to supply your own function for calculating label granularity. See the documentation and built-in functions of dygraph-tickers.js.

See also:

  • How to set specific y-axis label points in dygraphs?

EDIT: Example using ticker. This requires that you are familiar with the data and the data range is somewhat constant, otherwise you could end up with unreadable x-axis labels.

var g = new Dygraph(document.getElementById("demodiv3"), data(), {
  title: 'Example for changing x-axis label granularity 3',
  axes: {
    x: {
        ticker: function(a, b, pixels, opts, dygraph, vals) {
          var chosen = Dygraph.pickDateTickGranularity(a, b, pixels, opts);
          //Force to DAILY if built-in calculation returned SIX_HOURLY
            //if(chosen==Dygraph.SIX_HOURLY)
            //    chosen=Dygraph.DAILY;
          //or

          //Force to DAILY always
          chosen = Dygraph.DAILY;

          if (chosen >= 0) {
            return Dygraph.getDateAxis(a, b, chosen, opts, dygraph);
          } else {
            return [];
          }
        }        
      }

    }
});


来源:https://stackoverflow.com/questions/18338881/remove-hours-from-time-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!