Commit | Line | Data |
---|---|---|
c9faeafd DV |
1 | /** |
2 | * @fileoverview Tests relating to annotations | |
3 | * | |
4 | * @author danvk@google.com (Dan Vanderkam) | |
5 | */ | |
89fdcedb | 6 | describe("annotations", function() { |
c9faeafd | 7 | |
89fdcedb | 8 | beforeEach(function() { |
c9faeafd | 9 | document.body.innerHTML = "<div id='graph'></div>"; |
89fdcedb | 10 | }); |
c9faeafd | 11 | |
89fdcedb DV |
12 | afterEach(function() { |
13 | }); | |
c9faeafd | 14 | |
89fdcedb | 15 | it('testAnnotationsDrawn', function() { |
c9faeafd DV |
16 | var opts = { |
17 | width: 480, | |
18 | height: 320 | |
19 | }; | |
20 | var data = "X,Y\n" + | |
21 | "0,-1\n" + | |
22 | "1,0\n" + | |
23 | "2,1\n" + | |
24 | "3,0\n" | |
25 | ; | |
26 | ||
27 | var graph = document.getElementById("graph"); | |
28 | var g = new Dygraph(graph, data, opts); | |
29 | g.setAnnotations([ | |
30 | { | |
31 | series: 'Y', | |
32 | x: 1, | |
33 | shortText: 'A', | |
34 | text: 'Long A', | |
35 | cssClass: 'ann1' | |
36 | }, | |
37 | { | |
38 | series: 'Y', | |
39 | x: 2, | |
40 | shortText: 'B', | |
41 | text: 'Long B', | |
42 | cssClass: 'ann2' | |
43 | } | |
44 | ]); | |
45 | ||
89fdcedb | 46 | assert.equal(2, g.annotations().length); |
c9faeafd | 47 | var a1 = document.getElementsByClassName('ann1'); |
89fdcedb | 48 | assert.equal(1, a1.length); |
c9faeafd | 49 | a1 = a1[0]; |
89fdcedb | 50 | assert.equal('A', a1.textContent); |
c9faeafd DV |
51 | |
52 | var a2 = document.getElementsByClassName('ann2'); | |
89fdcedb | 53 | assert.equal(1, a2.length); |
c9faeafd | 54 | a2 = a2[0]; |
89fdcedb DV |
55 | assert.equal('B', a2.textContent); |
56 | }); | |
c9faeafd DV |
57 | |
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') | |
61 | ||
89fdcedb | 62 | it('testAnnotationsDontDisappearOnResize', function() { |
c9faeafd DV |
63 | var opts = { |
64 | }; | |
65 | var data = "X,Y\n" + | |
66 | "0,-1\n" + | |
67 | "1,0\n" + | |
68 | "2,1\n" + | |
69 | "3,0\n" | |
70 | ; | |
71 | ||
72 | var graph = document.getElementById("graph"); | |
73 | var g = new Dygraph(graph, data, opts); | |
74 | g.setAnnotations([ | |
75 | { | |
76 | series: 'Y', | |
77 | x: 1, | |
78 | shortText: 'A', | |
79 | text: 'Long A', | |
80 | cssClass: 'ann1' | |
81 | } | |
82 | ]); | |
83 | ||
84 | // Check that it displays at all | |
89fdcedb | 85 | assert.equal(1, g.annotations().length); |
c9faeafd | 86 | var a1 = document.getElementsByClassName('ann1'); |
89fdcedb | 87 | assert.equal(1, a1.length); |
c9faeafd | 88 | a1 = a1[0]; |
89fdcedb | 89 | assert.equal('A', a1.textContent); |
c9faeafd DV |
90 | |
91 | // ... and that resizing doesn't kill it. | |
92 | g.resize(400, 300); | |
89fdcedb | 93 | assert.equal(1, g.annotations().length); |
c9faeafd | 94 | var a1 = document.getElementsByClassName('ann1'); |
89fdcedb | 95 | assert.equal(1, a1.length); |
c9faeafd | 96 | a1 = a1[0]; |
89fdcedb DV |
97 | assert.equal('A', a1.textContent); |
98 | }); | |
4c919f55 DV |
99 | |
100 | // Verify that annotations outside of the visible x-range are not shown. | |
89fdcedb | 101 | it('testAnnotationsOutOfRangeX', function() { |
4c919f55 DV |
102 | var opts = { |
103 | }; | |
104 | var data = "X,Y\n" + | |
105 | "0,-1\n" + | |
106 | "1,0\n" + | |
107 | "2,1\n" + | |
108 | "3,0\n" | |
109 | ; | |
110 | ||
111 | var graph = document.getElementById("graph"); | |
112 | var g = new Dygraph(graph, data, opts); | |
113 | g.setAnnotations([ | |
114 | { | |
115 | series: 'Y', | |
116 | x: 1, | |
117 | shortText: 'A', | |
118 | text: 'Long A', | |
119 | cssClass: 'ann1' | |
120 | } | |
121 | ]); | |
122 | ||
123 | // Check that it displays at all | |
89fdcedb | 124 | assert.equal(1, g.annotations().length); |
4c919f55 | 125 | var a1 = document.getElementsByClassName('ann1'); |
89fdcedb | 126 | assert.equal(1, a1.length); |
4c919f55 | 127 | a1 = a1[0]; |
89fdcedb | 128 | assert.equal('A', a1.textContent); |
4c919f55 DV |
129 | |
130 | // ... and that panning right removes the annotation. | |
131 | g.updateOptions({dateWindow: [2, 6]}); | |
89fdcedb | 132 | assert.equal(1, g.annotations().length); |
4c919f55 | 133 | a1 = document.getElementsByClassName('ann1'); |
89fdcedb | 134 | assert.equal(0, a1.length); |
4c919f55 DV |
135 | |
136 | // ... and that panning left brings it back. | |
137 | g.updateOptions({dateWindow: [0, 4]}); | |
89fdcedb | 138 | assert.equal(1, g.annotations().length); |
4c919f55 | 139 | a1 = document.getElementsByClassName('ann1'); |
89fdcedb DV |
140 | assert.equal(1, a1.length); |
141 | }); | |
4c919f55 DV |
142 | |
143 | // Verify that annotations outside of the visible y-range are not shown. | |
89fdcedb | 144 | it('testAnnotationsOutOfRangeY', function() { |
4c919f55 DV |
145 | var opts = { |
146 | }; | |
147 | var data = "X,Y\n" + | |
148 | "0,-1\n" + | |
149 | "1,0\n" + | |
150 | "2,1\n" + | |
151 | "3,0\n" | |
152 | ; | |
153 | ||
154 | var graph = document.getElementById("graph"); | |
155 | var g = new Dygraph(graph, data, opts); | |
156 | g.setAnnotations([ | |
157 | { | |
158 | series: 'Y', | |
159 | x: 1, | |
160 | shortText: 'A', | |
161 | text: 'Long A', | |
162 | cssClass: 'ann1' | |
163 | } | |
164 | ]); | |
165 | ||
166 | // ... check that panning up removes the annotation. | |
167 | g.updateOptions({valueRange: [0.5, 2.5]}); | |
89fdcedb DV |
168 | assert.equal(1, g.annotations().length); |
169 | var a1 = document.getElementsByClassName('ann1'); | |
170 | assert.equal(0, a1.length); | |
4c919f55 DV |
171 | |
172 | // ... and that panning down brings it back. | |
173 | g.updateOptions({valueRange: [-1, 1]}); | |
89fdcedb | 174 | assert.equal(1, g.annotations().length); |
4c919f55 | 175 | a1 = document.getElementsByClassName('ann1'); |
89fdcedb DV |
176 | assert.equal(1, a1.length); |
177 | }); | |
492ea455 | 178 | |
89fdcedb | 179 | it('testAnnotationsDrawnInDrawCallback', function() { |
492ea455 RK |
180 | var data = "X,Y\n" + |
181 | "0,-1\n" + | |
182 | "1,0\n" + | |
183 | "2,1\n"; | |
184 | ||
185 | var graph = document.getElementById("graph"); | |
186 | ||
187 | var calls = []; | |
188 | var g = new Dygraph(graph, data, { | |
189 | width: 480, | |
190 | height: 320, | |
191 | drawCallback: function(g, initial) { | |
192 | calls.push(initial); | |
193 | if (initial) { | |
194 | g.setAnnotations([ | |
195 | { | |
196 | series: 'Y', | |
197 | x: 1, | |
198 | shortText: 'A', | |
199 | text: 'Long A', | |
200 | }, | |
201 | ]); | |
202 | } | |
203 | } | |
204 | }); | |
205 | ||
89fdcedb DV |
206 | assert.deepEqual([true, false], calls); |
207 | }); | |
b5481aea DV |
208 | |
209 | ||
210 | // Test that annotations on the same point are stacked. | |
211 | // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=256 | |
89fdcedb | 212 | it('testAnnotationsStacked', function() { |
b5481aea DV |
213 | var data = 'X,Y1,Y2\n' + |
214 | '0,1,2\n' + | |
215 | '1,2,3\n'; | |
216 | var graph = document.getElementById("graph"); | |
217 | var annotations = [ | |
218 | { | |
219 | series: 'Y1', | |
220 | x: 0, | |
221 | shortText: '1', | |
222 | attachAtBottom: true | |
223 | }, | |
224 | { | |
225 | series: 'Y2', | |
226 | x: 0, | |
227 | shortText: '2', | |
228 | attachAtBottom: true | |
229 | } | |
230 | ]; | |
231 | var g = new Dygraph(graph, data, { | |
232 | width: 480, | |
233 | height: 320 | |
234 | }); | |
235 | g.setAnnotations(annotations); | |
236 | ||
237 | var annEls = document.getElementsByClassName('dygraphDefaultAnnotation'); | |
89fdcedb | 238 | assert.equal(2, annEls.length); |
b5481aea | 239 | |
89fdcedb | 240 | assert.equal(annEls[0].offsetLeft, annEls[1].offsetLeft); |
b5481aea | 241 | assert(annEls[1].offsetTop < annEls[0].offsetTop - 10); |
89fdcedb | 242 | }); |
5bcc58b4 DV |
243 | |
244 | ||
245 | // Test the .ready() method, which is most often used with setAnnotations(). | |
89fdcedb | 246 | it('testReady', function() { |
5bcc58b4 DV |
247 | var data = 'X,Y1,Y2\n' + |
248 | '0,1,2\n' + | |
249 | '1,2,3\n'; | |
250 | var mockXhr = Util.overrideXMLHttpRequest(data); | |
251 | ||
252 | var graph = document.getElementById("graph"); | |
253 | var g = new Dygraph(graph, "data.csv", { | |
254 | width: 480, | |
255 | height: 320 | |
256 | }); | |
257 | ||
258 | var ready_calls = 0; | |
259 | g.ready(function() { ready_calls++; }); | |
260 | ||
89fdcedb | 261 | assert.equal(0, ready_calls); |
5bcc58b4 | 262 | mockXhr.respond(); |
89fdcedb | 263 | assert.equal(1, ready_calls); |
5bcc58b4 DV |
264 | |
265 | // Make sure that ready isn't called on redraws. | |
266 | g.updateOptions({}); | |
89fdcedb | 267 | assert.equal(1, ready_calls); |
5bcc58b4 DV |
268 | |
269 | // Or data changes. | |
270 | g.updateOptions({file: data}); | |
89fdcedb DV |
271 | assert.equal(1, ready_calls); |
272 | }); | |
273 | ||
274 | }); |