2 * @fileoverview Tests relating to annotations
4 * @author danvk@google.com (Dan Vanderkam)
6 describe("annotations", function() {
8 beforeEach(function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 afterEach(function() {
15 it('testAnnotationsDrawn', function() {
27 var graph
= document
.getElementById("graph");
28 var g
= new Dygraph(graph
, data
, opts
);
46 assert
.equal(2, g
.annotations().length
);
47 var a1
= document
.getElementsByClassName('ann1');
48 assert
.equal(1, a1
.length
);
50 assert
.equal('A', a1
.textContent
);
52 var a2
= document
.getElementsByClassName('ann2');
53 assert
.equal(1, a2
.length
);
55 assert
.equal('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 it('testAnnotationsDontDisappearOnResize', function() {
72 var graph
= document
.getElementById("graph");
73 var g
= new Dygraph(graph
, data
, opts
);
84 // Check that it displays at all
85 assert
.equal(1, g
.annotations().length
);
86 var a1
= document
.getElementsByClassName('ann1');
87 assert
.equal(1, a1
.length
);
89 assert
.equal('A', a1
.textContent
);
91 // ... and that resizing doesn't kill it.
93 assert
.equal(1, g
.annotations().length
);
94 var a1
= document
.getElementsByClassName('ann1');
95 assert
.equal(1, a1
.length
);
97 assert
.equal('A', a1
.textContent
);
100 // Verify that annotations outside of the visible x-range are not shown.
101 it('testAnnotationsOutOfRangeX', function() {
111 var graph
= document
.getElementById("graph");
112 var g
= new Dygraph(graph
, data
, opts
);
123 // Check that it displays at all
124 assert
.equal(1, g
.annotations().length
);
125 var a1
= document
.getElementsByClassName('ann1');
126 assert
.equal(1, a1
.length
);
128 assert
.equal('A', a1
.textContent
);
130 // ... and that panning right removes the annotation.
131 g
.updateOptions({dateWindow
: [2, 6]});
132 assert
.equal(1, g
.annotations().length
);
133 a1
= document
.getElementsByClassName('ann1');
134 assert
.equal(0, a1
.length
);
136 // ... and that panning left brings it back.
137 g
.updateOptions({dateWindow
: [0, 4]});
138 assert
.equal(1, g
.annotations().length
);
139 a1
= document
.getElementsByClassName('ann1');
140 assert
.equal(1, a1
.length
);
143 // Verify that annotations outside of the visible y-range are not shown.
144 it('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 assert
.equal(1, g
.annotations().length
);
169 var a1
= document
.getElementsByClassName('ann1');
170 assert
.equal(0, a1
.length
);
172 // ... and that panning down brings it back.
173 g
.updateOptions({valueRange
: [-1, 1]});
174 assert
.equal(1, g
.annotations().length
);
175 a1
= document
.getElementsByClassName('ann1');
176 assert
.equal(1, a1
.length
);
179 it('testAnnotationsDrawnInDrawCallback', function() {
185 var graph
= document
.getElementById("graph");
188 var g
= new Dygraph(graph
, data
, {
191 drawCallback
: function(g
, initial
) {
206 assert
.deepEqual([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 it('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 assert
.equal(2, annEls
.length
);
240 assert
.equal(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 it('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 assert
.equal(0, ready_calls
);
263 assert
.equal(1, ready_calls
);
265 // Make sure that ready isn't called on redraws.
267 assert
.equal(1, ready_calls
);
270 g
.updateOptions({file
: data
});
271 assert
.equal(1, ready_calls
);