2 * @fileoverview Tests relating to annotations
4 * @author danvk@google.com (Dan Vanderkam)
6 var AnnotationsTestCase
= TestCase("annotations");
8 AnnotationsTestCase
.prototype.setUp
= function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 AnnotationsTestCase
.prototype.tearDown
= function() {
15 AnnotationsTestCase
.prototype.testAnnotationsDrawn
= function() {
27 var graph
= document
.getElementById("graph");
28 var g
= new Dygraph(graph
, data
, opts
);
46 assertEquals(2, g
.annotations().length
);
47 var a1
= document
.getElementsByClassName('ann1');
48 assertEquals(1, a1
.length
);
50 assertEquals('A', a1
.textContent
);
52 var a2
= document
.getElementsByClassName('ann2');
53 assertEquals(1, a2
.length
);
55 assertEquals('B', a2
.textContent
);
58 // Some errors that should be flagged:
59 // 1. Invalid series name (e.g. 'X' or 'non-existent')
60 // 2. Passing a string as 'x' instead of a number (e.g. x: '1')
62 AnnotationsTestCase
.prototype.testAnnotationsDontDisappearOnResize
= function() {
72 var graph
= document
.getElementById("graph");
73 var g
= new Dygraph(graph
, data
, opts
);
84 // Check that it displays at all
85 assertEquals(1, g
.annotations().length
);
86 var a1
= document
.getElementsByClassName('ann1');
87 assertEquals(1, a1
.length
);
89 assertEquals('A', a1
.textContent
);
91 // ... and that resizing doesn't kill it.
93 assertEquals(1, g
.annotations().length
);
94 var a1
= document
.getElementsByClassName('ann1');
95 assertEquals(1, a1
.length
);
97 assertEquals('A', a1
.textContent
);
100 // Verify that annotations outside of the visible x-range are not shown.
101 AnnotationsTestCase
.prototype.testAnnotationsOutOfRangeX
= function() {
111 var graph
= document
.getElementById("graph");
112 var g
= new Dygraph(graph
, data
, opts
);
123 // Check that it displays at all
124 assertEquals(1, g
.annotations().length
);
125 var a1
= document
.getElementsByClassName('ann1');
126 assertEquals(1, a1
.length
);
128 assertEquals('A', a1
.textContent
);
130 // ... and that panning right removes the annotation.
131 g
.updateOptions({dateWindow
: [2, 6]});
132 assertEquals(1, g
.annotations().length
);
133 a1
= document
.getElementsByClassName('ann1');
134 assertEquals(0, a1
.length
);
136 // ... and that panning left brings it back.
137 g
.updateOptions({dateWindow
: [0, 4]});
138 assertEquals(1, g
.annotations().length
);
139 a1
= document
.getElementsByClassName('ann1');
140 assertEquals(1, a1
.length
);
143 // Verify that annotations outside of the visible y-range are not shown.
144 AnnotationsTestCase
.prototype.testAnnotationsOutOfRangeY
= function() {
154 var graph
= document
.getElementById("graph");
155 var g
= new Dygraph(graph
, data
, opts
);
166 // ... check that panning up removes the annotation.
167 g
.updateOptions({valueRange
: [0.5, 2.5]});
168 assertEquals(1, g
.annotations().length
);
169 a1
= document
.getElementsByClassName('ann1');
170 assertEquals(0, a1
.length
);
172 // ... and that panning down brings it back.
173 g
.updateOptions({valueRange
: [-1, 1]});
174 assertEquals(1, g
.annotations().length
);
175 a1
= document
.getElementsByClassName('ann1');
176 assertEquals(1, a1
.length
);
179 AnnotationsTestCase
.prototype.testAnnotationsDrawnInDrawCallback
= function() {
185 var graph
= document
.getElementById("graph");
188 var g
= new Dygraph(graph
, data
, {
191 drawCallback
: function(g
, initial
) {
206 assertEquals([true, false], calls
);
210 // Test that annotations on the same point are stacked.
211 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=256
212 AnnotationsTestCase
.prototype.testAnnotationsStacked
= function() {
213 var data
= 'X,Y1,Y2\n' +
216 var graph
= document
.getElementById("graph");
231 var g
= new Dygraph(graph
, data
, {
235 g
.setAnnotations(annotations
);
237 var annEls
= document
.getElementsByClassName('dygraphDefaultAnnotation');
238 assertEquals(2, annEls
.length
);
240 assertEquals(annEls
[0].offsetLeft
, annEls
[1].offsetLeft
);
241 assert(annEls
[1].offsetTop
< annEls
[0].offsetTop
- 10);
245 // Test the .ready() method, which is most often used with setAnnotations().
246 AnnotationsTestCase
.prototype.testReady
= function() {
247 var data
= 'X,Y1,Y2\n' +
250 var mockXhr
= Util
.overrideXMLHttpRequest(data
);
252 var graph
= document
.getElementById("graph");
253 var g
= new Dygraph(graph
, "data.csv", {
259 g
.ready(function() { ready_calls
++; });
261 assertEquals(0, ready_calls
);
263 assertEquals(1, ready_calls
);
265 // Make sure that ready isn't called on redraws.
267 assertEquals(1, ready_calls
);
270 g
.updateOptions({file
: data
});
271 assertEquals(1, ready_calls
);