Zulu
Zulu

Reputation: 9265

Using JSON with Dygraph and datetime

I'm using Dygraph and jQuery as below:

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

The response datas are as below:

[[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]]

My trouble is dygraph doesn't seem to take timestamps as date. It shows number for legend. I tried to use ValueFormatter, ValueParser, and AxisLabelFormatter but with no success (maybe I did wrong?)

Does someone know how to enable datetime without walk in all data and convert timestamps manually ?

Upvotes: 1

Views: 8758

Answers (2)

MarcelH
MarcelH

Reputation: 43

Do this:

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

where data_from_json are in format [ [ timestamp, y-value ], [ ts, t-val ], ...]

then use data_for_dygraph as a source of data form dygraph:

Upvotes: 0

jtromans
jtromans

Reputation: 4263

Have you tried:

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

Take a look at http://dygraphs.com/data.html and scroll to Array (native format)

Upvotes: 1

Related Questions