2 * @fileoverview Tests relating to annotations
4 * @author danvk@google.com (Dan Vanderkam)
7 import Dygraph from
'../../src/dygraph';
8 import Util from
'./Util';
10 describe("annotations", function() {
14 it('testAnnotationsDrawn', function() {
26 var graph
= document
.getElementById("graph");
27 var g
= new Dygraph(graph
, data
, opts
);
45 assert
.equal(2, g
.annotations().length
);
46 var a1
= document
.getElementsByClassName('ann1');
47 assert
.equal(1, a1
.length
);
49 assert
.equal('A', a1
.textContent
);
51 var a2
= document
.getElementsByClassName('ann2');
52 assert
.equal(1, a2
.length
);
54 assert
.equal('B', a2
.textContent
);
57 // Some errors that should be flagged:
58 // 1. Invalid series name (e.g. 'X' or 'non-existent')
59 // 2. Passing a string as 'x' instead of a number (e.g. x: '1')
61 it('testAnnotationsDontDisappearOnResize', function() {
71 var graph
= document
.getElementById("graph");
72 var g
= new Dygraph(graph
, data
, opts
);
83 // Check that it displays at all
84 assert
.equal(1, g
.annotations().length
);
85 var a1
= document
.getElementsByClassName('ann1');
86 assert
.equal(1, a1
.length
);
88 assert
.equal('A', a1
.textContent
);
90 // ... and that resizing doesn't kill it.
92 assert
.equal(1, g
.annotations().length
);
93 var a1
= document
.getElementsByClassName('ann1');
94 assert
.equal(1, a1
.length
);
96 assert
.equal('A', a1
.textContent
);
99 // Verify that annotations outside of the visible x-range are not shown.
100 it('testAnnotationsOutOfRangeX', function() {
110 var graph
= document
.getElementById("graph");
111 var g
= new Dygraph(graph
, data
, opts
);
122 // Check that it displays at all
123 assert
.equal(1, g
.annotations().length
);
124 var a1
= document
.getElementsByClassName('ann1');
125 assert
.equal(1, a1
.length
);
127 assert
.equal('A', a1
.textContent
);
129 // ... and that panning right removes the annotation.
130 g
.updateOptions({dateWindow
: [2, 6]});
131 assert
.equal(1, g
.annotations().length
);
132 a1
= document
.getElementsByClassName('ann1');
133 assert
.equal(0, a1
.length
);
135 // ... and that panning left brings it back.
136 g
.updateOptions({dateWindow
: [0, 4]});
137 assert
.equal(1, g
.annotations().length
);
138 a1
= document
.getElementsByClassName('ann1');
139 assert
.equal(1, a1
.length
);
142 // Verify that annotations outside of the visible y-range are not shown.
143 it('testAnnotationsOutOfRangeY', function() {
153 var graph
= document
.getElementById("graph");
154 var g
= new Dygraph(graph
, data
, opts
);
165 // ... check that panning up removes the annotation.
166 g
.updateOptions({valueRange
: [0.5, 2.5]});
167 assert
.equal(1, g
.annotations().length
);
168 var a1
= document
.getElementsByClassName('ann1');
169 assert
.equal(0, a1
.length
);
171 // ... and that panning down brings it back.
172 g
.updateOptions({valueRange
: [-1, 1]});
173 assert
.equal(1, g
.annotations().length
);
174 a1
= document
.getElementsByClassName('ann1');
175 assert
.equal(1, a1
.length
);
178 it('testAnnotationsDrawnInDrawCallback', function() {
184 var graph
= document
.getElementById("graph");
187 var g
= new Dygraph(graph
, data
, {
190 drawCallback
: function(g
, initial
) {
205 assert
.deepEqual([true, false], calls
);
209 // Test that annotations on the same point are stacked.
210 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=256
211 it('testAnnotationsStacked', function() {
212 var data
= 'X,Y1,Y2\n' +
215 var graph
= document
.getElementById("graph");
230 var g
= new Dygraph(graph
, data
, {
234 g
.setAnnotations(annotations
);
236 var annEls
= document
.getElementsByClassName('dygraphDefaultAnnotation');
237 assert
.equal(2, annEls
.length
);
239 assert
.equal(annEls
[0].offsetLeft
, annEls
[1].offsetLeft
);
240 assert(annEls
[1].offsetTop
< annEls
[0].offsetTop
- 10);
244 // Test the .ready() method, which is most often used with setAnnotations().
245 it('testReady', function() {
246 var data
= 'X,Y1,Y2\n' +
249 var mockXhr
= Util
.overrideXMLHttpRequest(data
);
251 var graph
= document
.getElementById("graph");
252 var g
= new Dygraph(graph
, "data.csv", {
258 g
.ready(function() { ready_calls
++; });
260 assert
.equal(0, ready_calls
);
262 assert
.equal(1, ready_calls
);
264 // Make sure that ready isn't called on redraws.
266 assert
.equal(1, ready_calls
);
269 g
.updateOptions({file
: data
});
270 assert
.equal(1, ready_calls
);