Refactoring to fix stacked graphs with NaNs.
[dygraphs.git] / auto_tests / tests / stacked.js
index e365c58..b8c095e 100644 (file)
@@ -5,11 +5,17 @@
  */
 var stackedTestCase = TestCase("stacked");
 
+stackedTestCase._origGetContext = Dygraph.getContext;
+
 stackedTestCase.prototype.setUp = function() {
   document.body.innerHTML = "<div id='graph'></div>";
+  Dygraph.getContext = function(canvas) {
+    return new Proxy(stackedTestCase._origGetContext(canvas));
+  }
 };
 
 stackedTestCase.prototype.tearDown = function() {
+  Dygraph.getContext = stackedTestCase._origGetContext;
 };
 
 stackedTestCase.prototype.testCorrectColors = function() {
@@ -179,3 +185,97 @@ stackedTestCase.prototype.testMissingValueAtZero = function() {
   g.setSelection(2);
   assertEquals("2: Y2: 3", Util.getLegend());
 };
+
+stackedTestCase.prototype.testInterpolation = function() {
+  var opts = {
+    colors: ['#ff0000', '#00ff00', '#0000ff'],
+    stackedGraph: true
+  };
+
+  // The last series is all-NaN, it ought to be treated as all zero
+  // for stacking purposes.
+  var N = NaN;
+  var data = [
+    [100, 1, 2, N, N],
+    [101, 1, 2, 2, N],
+    [102, 1, N, N, N],
+    [103, 1, 2, 4, N],
+    [104, N, N, N, N],
+    [105, 1, 2, N, N],
+    [106, 1, 2, 7, N],
+    [107, 1, 2, 8, N],
+    [108, 1, 2, 9, N],
+    [109, 1, N, N, N]];
+
+  var graph = document.getElementById("graph");
+  g = new Dygraph(graph, data, opts);
+
+  var htx = g.hidden_ctx_;
+  var attrs = {};
+
+  // Check that lines are drawn at the expected positions, using
+  // interpolated values for missing data.
+  CanvasAssertions.assertLineDrawn(
+      htx, g.toDomCoords(100, 4), g.toDomCoords(101, 4), {strokeStyle: '#00ff00'});
+  CanvasAssertions.assertLineDrawn(
+      htx, g.toDomCoords(102, 6), g.toDomCoords(103, 7), {strokeStyle: '#ff0000'});
+  CanvasAssertions.assertLineDrawn(
+      htx, g.toDomCoords(107, 8), g.toDomCoords(108, 9), {strokeStyle: '#0000ff'});
+  CanvasAssertions.assertLineDrawn(
+      htx, g.toDomCoords(108, 12), g.toDomCoords(109, 12), {strokeStyle: '#ff0000'});
+
+  // Check that the expected number of line segments gets drawn
+  // for each series. Gaps don't get a line.
+  assertEquals(7, CanvasAssertions.numLinesDrawn(htx, '#ff0000'));
+  assertEquals(4, CanvasAssertions.numLinesDrawn(htx, '#00ff00'));
+  assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#0000ff'));
+
+  // Check that the selection returns the original (non-stacked)
+  // values and skips gaps.
+  g.setSelection(1);
+  assertEquals("101: Y1: 1 Y2: 2 Y3: 2", Util.getLegend());
+
+  g.setSelection(8);
+  assertEquals("108: Y1: 1 Y2: 2 Y3: 9", Util.getLegend());
+
+  g.setSelection(9);
+  assertEquals("109: Y1: 1", Util.getLegend());
+};
+
+stackedTestCase.prototype.testInterpolationOptions = function() {
+  var opts = {
+    colors: ['#ff0000', '#00ff00', '#0000ff'],
+    stackedGraph: true
+  };
+
+  var data = [
+    [100, 1, NaN, 3],
+    [101, 1, 2, 3],
+    [102, 1, NaN, 3],
+    [103, 1, 2, 3],
+    [104, 1, NaN, 3]];
+
+  var choices = ['all', 'inside', 'none'];
+  var stackedY = [
+    [6, 6, 6, 6, 6],
+    [4, 6, 6, 6, 4],
+    [4, 6, 4, 6, 4]];
+
+  for (var i = 0; i < choices.length; ++i) {
+    var graph = document.getElementById("graph");
+    opts['stackedGraphNaNFill'] = choices[i];
+    g = new Dygraph(graph, data, opts);
+
+    var htx = g.hidden_ctx_;
+    var attrs = {};
+
+    // Check top lines get drawn at the expected positions.
+    for (var j = 0; j < stackedY[i].length - 1; ++j) {
+      CanvasAssertions.assertLineDrawn(
+          htx,
+          g.toDomCoords(100 + j, stackedY[i][j]),
+          g.toDomCoords(101 + j, stackedY[i][j + 1]),
+          {strokeStyle: '#ff0000'});
+    }
+  }
+};