Search code examples
javascriptjqueryajaxjsondygraphs

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 ?


Solution

  • 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)