fix disappearing annotations bug
authorDan Vanderkam <danvk@google.com>
Wed, 17 Aug 2011 19:55:49 +0000 (15:55 -0400)
committerDan Vanderkam <danvk@google.com>
Wed, 17 Aug 2011 19:55:49 +0000 (15:55 -0400)
auto_tests/misc/local.html
auto_tests/tests/annotations.js [new file with mode: 0644]
dygraph.js

index cd75c28..0a73f16 100644 (file)
@@ -30,6 +30,7 @@
   <script type="text/javascript" src="../tests/selection.js"></script>
   <script type="text/javascript" src="../tests/rolling_average.js"></script>
   <script type="text/javascript" src="../tests/error_bars.js"></script>
+  <script type="text/javascript" src="../tests/annotations.js"></script>
   <script type="text/javascript" src="../tests/update_options.js"></script>
   <script type="text/javascript" src="../tests/utils_test.js"></script>
   <script type="text/javascript" src="../tests/multiple_axes.js"></script>
diff --git a/auto_tests/tests/annotations.js b/auto_tests/tests/annotations.js
new file mode 100644 (file)
index 0000000..7433fd3
--- /dev/null
@@ -0,0 +1,98 @@
+/**
+ * @fileoverview Tests relating to annotations
+ *
+ * @author danvk@google.com (Dan Vanderkam)
+ */
+var annotationsTestCase = TestCase("annotations");
+
+annotationsTestCase.prototype.setUp = function() {
+  document.body.innerHTML = "<div id='graph'></div>";
+};
+
+annotationsTestCase.prototype.tearDown = function() {
+};
+
+annotationsTestCase.prototype.testAnnotationsDrawn = function() {
+  var opts = {
+    width: 480,
+    height: 320
+  };
+  var data = "X,Y\n" +
+      "0,-1\n" +
+      "1,0\n" +
+      "2,1\n" +
+      "3,0\n"
+  ;
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data, opts);
+  g.setAnnotations([
+    {
+      series: 'Y',
+      x: 1,
+      shortText: 'A',
+      text: 'Long A',
+      cssClass: 'ann1'
+    },
+    {
+      series: 'Y',
+      x: 2,
+      shortText: 'B',
+      text: 'Long B',
+      cssClass: 'ann2'
+    }
+  ]);
+
+  assertEquals(2, g.annotations().length);
+  var a1 = document.getElementsByClassName('ann1');
+  assertEquals(1, a1.length);
+  a1 = a1[0];
+  assertEquals('A', a1.textContent);
+
+  var a2 = document.getElementsByClassName('ann2');
+  assertEquals(1, a2.length);
+  a2 = a2[0];
+  assertEquals('B', a2.textContent);
+};
+
+// Some errors that should be flagged:
+// 1. Invalid series name (e.g. 'X' or 'non-existent')
+// 2. Passing a string as 'x' instead of a number (e.g. x: '1')
+
+annotationsTestCase.prototype.testAnnotationsDontDisappearOnResize = function() {
+  var opts = {
+  };
+  var data = "X,Y\n" +
+      "0,-1\n" +
+      "1,0\n" +
+      "2,1\n" +
+      "3,0\n"
+  ;
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data, opts);
+  g.setAnnotations([
+    {
+      series: 'Y',
+      x: 1,
+      shortText: 'A',
+      text: 'Long A',
+      cssClass: 'ann1'
+    }
+  ]);
+
+  // Check that it displays at all
+  assertEquals(1, g.annotations().length);
+  var a1 = document.getElementsByClassName('ann1');
+  assertEquals(1, a1.length);
+  a1 = a1[0];
+  assertEquals('A', a1.textContent);
+
+  // ... and that resizing doesn't kill it.
+  g.resize(400, 300);
+  assertEquals(1, g.annotations().length);
+  var a1 = document.getElementsByClassName('ann1');
+  assertEquals(1, a1.length);
+  a1 = a1[0];
+  assertEquals('A', a1.textContent);
+};
index c04bccc..9359bb7 100644 (file)
@@ -2831,6 +2831,10 @@ Dygraph.prototype.resize = function(width, height) {
     this.roller_ = null;
     this.attrs_.labelsDiv = null;
     this.createInterface_();
+    if (this.annotations_.length) {
+      // createInterface_ reset the layout, so we need to do this.
+      this.layout_.setAnnotations(this.annotations_);
+    }
     this.predraw_();
   }