score:0

i would use a library like lodash (to make utility things quicker) to pre process the data into a flat list of all timestamps and then for each dataset a matching list of values recording null if that data set does not have a timestamp from the merged flat list of all timestamps.

i have also added an option to my fork of chart js which would be useful here which would be to then populate sparse data to the lines still connect and you are not left with floating points that can be hard to see.

var datasets = {
  "default": {
    "values": [
      999,
      799,
      999
    ],
    "timestamps": [
      "2015-03-01t03:31:16+00:00",
      "2015-03-01t07:21:43+00:00",
      "2015-03-01t14:02:22+00:00"
    ]
  },
  "current": {
    "values": [
      399,
      849
    ],
    "timestamps": [
      "2015-03-01t01:15:22+00:00",
      "2015-03-01t21:30:43+00:00"
    ]
  },
  "currentpremium": {
    "values": [
      500,
      345,
      200,
      500
    ],
    "timestamps": [
      "2015-02-01t14:24:00+00:00",
      "2015-03-01t00:13:28+00:00",
      "2015-03-01t09:56:43+00:00",
      "2015-03-01t12:00:04+00:00"
    ]
  }
};

//merge and sort all timestamps in to one array (used lodash here just to make things easy
var timestamps = _.chain(datasets).pluck("timestamps").reduce(function(previous, current, index) {
  return previous.concat(current)
}).unique().sortby(function(timestamp) {
  return new date(timestamp)
}).value();


//set up base chart data with colours
var chartdatasets = [{

  fillcolor: "rgba(220,120,120,0.2)",
  strokecolor: "rgba(220,120,120,1)",
  pointcolor: "rgba(120,120,120,1)",
  pointstrokecolor: "#fff",
  pointhighlightfill: "#fff",
  pointhighlightstroke: "rgba(220,220,220,1)",

}, {

  fillcolor: "rgba(20,120,120,0.2)",
  strokecolor: "rgba(20,120,120,1)",
  pointcolor: "rgba(20,120,120,1)",
  pointstrokecolor: "#fff",
  pointhighlightfill: "#fff",
  pointhighlightstroke: "rgba(220,220,220,1)",

}, {

  fillcolor: "rgba(120,120,120,0.2)",
  strokecolor: "rgba(120,120,120,1)",
  pointcolor: "rgba(120,120,120,1)",
  pointstrokecolor: "#fff",
  pointhighlightfill: "#fff",
  pointhighlightstroke: "rgba(220,220,220,1)",

}]



//go through each dataset from server,
//for each dataset go through it's time stamps,
//go through the merged timestamps and if the timestamp is prresent in the dataset record the value other wise record null so we
//end up with a flat list of data that matches the flat time stamps
var datasetsindex = 0;
_.foreach(datasets, function(dataset, key) {
  var data = [];
  _.foreach(timestamps, function(timestamp) {
    var datatopush = null;
    _.foreach(dataset.timestamps, function(datasettimestamp, datasettimestampindex) {
      if (datasettimestamp === timestamp) {
        datatopush = dataset.values[datasettimestampindex];
      }
    });
    data.push(datatopush);

  });

  chartdatasets[datasetsindex].label = key;
  chartdatasets[datasetsindex].data = data;
  datasetsindex++;
});

var chartdata = {
  labels: timestamps,
  datasets: chartdatasets
};


var chart = new chart(document.getelementbyid("chart").getcontext("2d")).line(chartdata, {
  populatesparsedata: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.3.1/lodash.js"></script>
<script src="http://quincewebdesign.com/cdn/chart.js"></script>
<canvas id="chart" width="400" height="400"></canvas>


Related Query