Fix connectSeparatedPoints, add test
[dygraphs.git] / auto_tests / tests / callback.js
index aad0dad..9551eb0 100644 (file)
@@ -167,7 +167,7 @@ var runClosestTest = function(isStacked, widthNormal, widthHighlighted) {
         strokeWidth: widthNormal,
         strokeBorderWidth: 2,
         highlightCircleSize: widthNormal * 2,
-        highlightSeriesBackgroundFade: 0.7,
+        highlightSeriesBackgroundAlpha: 0.3,
 
         highlightSeriesOpts: {
           strokeWidth: widthHighlighted,
@@ -244,6 +244,7 @@ CallbackTestCase.prototype.testClosestPointCallbackCss1 = function() {
     "div.dygraph-legend > span.highlight { border: 1px solid grey; }\n";
   this.styleSheet.innerHTML = css;
   runClosestTest(false, 2, 4);
+  this.styleSheet.innerHTML = '';
 }
 
 /**
@@ -254,5 +255,107 @@ CallbackTestCase.prototype.testClosestPointCallbackCss2 = function() {
     "div.dygraph-legend > span.highlight { display: inline; }\n";
   this.styleSheet.innerHTML = css;
   runClosestTest(false, 10, 15);
+  this.styleSheet.innerHTML = '';
   // TODO(klausw): verify that the highlighted line is drawn on top?
 }
+
+/**
+ * This tests that closest point searches work for data containing NaNs.
+ *
+ * It's intended to catch a regression where a NaN Y value confuses the
+ * closest-point algorithm, treating it as closer as any previous point.
+ */
+CallbackTestCase.prototype.testNaNData = function() {
+  var dataNaN = [
+    [9, -1, NaN, NaN],
+    [10, -1, 1, 2],
+    [11, 0, 3, 1],
+    [12, 1, 4, NaN],
+    [13, 0, 2, 3],
+    [14, -1, 1, 4]];
+
+  var h_row;
+  var h_pts;
+
+  var highlightCallback  =  function(e, x, pts, row) {
+    h_row = row;
+    h_pts = pts;
+  };
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, dataNaN,
+      {
+        width: 600,
+        height: 400,
+        labels: ['x', 'a', 'b', 'c'],
+        visibility: [false, true, true],
+        highlightCallback: highlightCallback
+      });
+
+  DygraphOps.dispatchMouseMove(g, 10.1, 0.9);
+  //check correct row is returned
+  assertEquals(1, h_row);
+
+  // Explicitly test closest point algorithms
+  var dom = g.toDomCoords(10.1, 0.9);
+  assertEquals(1, g.findClosestRow(dom[0]));
+
+  var res = g.findClosestPoint(dom[0], dom[1]);
+  assertEquals(1, res.row);
+  assertEquals('b', res.seriesName);
+
+  res = g.findStackedPoint(dom[0], dom[1]);
+  assertEquals(1, res.row);
+  assertEquals('c', res.seriesName);
+};
+
+CallbackTestCase.prototype.testGapHighlight = function() {
+var dataGap = [
+    [1, null, 3],
+    [2, 2, null],
+    [3, null, 5],
+    [4, 4, null],
+    [5, null, 7],
+    [6, NaN, null],
+    [8, 8, null],
+    [10, 10, null]];
+
+  var h_row;
+  var h_pts;
+
+  var highlightCallback  =  function(e, x, pts, row) {
+    h_row = row;
+    h_pts = pts;
+  };
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, dataGap, {
+     width: 400,
+     height: 300,
+     //stackedGraph: true,
+     connectSeparatedPoints: true,
+     drawPoints: true,
+     labels: ['x', 'A', 'B'],
+     highlightCallback : highlightCallback
+  });
+
+  DygraphOps.dispatchMouseMove(g, 1.1, 10);
+  //point from series B
+  assertEquals(0, h_row);
+  assertEquals(1, h_pts.length);
+  assertEquals(3, h_pts[0].yval);
+  assertEquals('B', h_pts[0].name);
+
+  DygraphOps.dispatchMouseMove(g, 6.1, 10);
+  // A is NaN at x=6
+  assertEquals(1, h_pts.length);
+  assert(isNaN(h_pts[0].yval));
+  assertEquals('A', h_pts[0].name);
+
+  DygraphOps.dispatchMouseMove(g, 8.1, 10);
+  //point from series A
+  assertEquals(6, h_row);
+  assertEquals(1, h_pts.length);
+  assertEquals(8, h_pts[0].yval);
+  assertEquals('A', h_pts[0].name);
+};