From c9faeafdb66e6781576d4fb9ecc53f5c26cec759 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 17 Aug 2011 15:55:49 -0400 Subject: [PATCH] fix disappearing annotations bug --- auto_tests/misc/local.html | 1 + auto_tests/tests/annotations.js | 98 +++++++++++++++++++++++++++++++++++++++++ dygraph.js | 4 ++ 3 files changed, 103 insertions(+) create mode 100644 auto_tests/tests/annotations.js diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index cd75c28..0a73f16 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -30,6 +30,7 @@ + diff --git a/auto_tests/tests/annotations.js b/auto_tests/tests/annotations.js new file mode 100644 index 0000000..7433fd3 --- /dev/null +++ b/auto_tests/tests/annotations.js @@ -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 = "
"; +}; + +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); +}; diff --git a/dygraph.js b/dygraph.js index c04bccc..9359bb7 100644 --- a/dygraph.js +++ b/dygraph.js @@ -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_(); } -- 2.7.4