将JSON与Dygraph和datetime一起使用

Using JSON with Dygraph and datetime

本文关键字:一起 datetime JSON Dygraph      更新时间:2023-09-26

我正在使用Dygraph和jQuery,如下所示:

$.getJSON('/myurl/for/jsondata', function(data) {
  g = new Dygraph(document.getElementById("demodiv"), data, {});
});

响应数据如下:

[[1372951380.0, 82.31065525957484], [1372951440.0, 85.25771431214908], [1372951500.0, 81.12563963030715], [1372951560.0, 80.87068966263206], [1372951620.0, 80.18137775897438], [1372951680.0, 81.46331467662664], [1372951740.0, 77.4216130457947]]

我的问题是dygraph似乎不把时间戳当作日期。它显示图例的编号。我尝试使用ValueFormatter、ValueParser和AxisLabelFormatter,但没有成功(也许我做错了?)

有人知道如何在不输入所有数据的情况下启用日期时间并手动转换时间戳吗?

您尝试过吗:

var utcSeconds = 1372951380;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);

看看http://dygraphs.com/data.html并滚动到Array(原生格式)

这样做:

data_for_dygraph = $.map(data_from_json, function(n) {
              return [ [ new Date(n[0]), n[1] ] ];
          });

其中data_from_json的格式为[[timestamp,y-value],[ts,t-val],…]

然后使用data_for_dygraph作为dygraph的数据源: