Commit | Line | Data |
---|---|---|
5469113b | 1 | /** |
1c6b239c | 2 | * @fileoverview Test cases for the callbacks. |
3 | * | |
4 | * @author uemit.seren@gmail.com (Ümit Seren) | |
5 | */ | |
6 | ||
89fdcedb | 7 | describe("callback", function() { |
1c6b239c | 8 | |
319d0361 DV |
9 | var xhr, styleSheet; |
10 | ||
89fdcedb | 11 | beforeEach(function() { |
857a6931 | 12 | document.body.innerHTML = "<div id='graph'></div><div id='selection'></div>"; |
319d0361 DV |
13 | xhr = XMLHttpRequest; |
14 | styleSheet = document.createElement("style"); | |
15 | styleSheet.type = "text/css"; | |
16 | document.getElementsByTagName("head")[0].appendChild(styleSheet); | |
89fdcedb | 17 | }); |
1c6b239c | 18 | |
89fdcedb DV |
19 | afterEach(function() { |
20 | window.XMLHttpRequest = xhr; | |
21 | }); | |
475f7420 | 22 | |
89fdcedb | 23 | var data = "X,a,b,c\n" + |
1c6b239c | 24 | "10,-1,1,2\n" + |
25 | "11,0,3,1\n" + | |
26 | "12,1,4,2\n" + | |
27 | "13,0,2,3\n"; | |
857a6931 | 28 | |
475f7420 KW |
29 | |
30 | /** | |
31 | * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback | |
32 | * is properly called when the first series is hidden (setVisibility = false) | |
33 | * | |
34 | */ | |
89fdcedb | 35 | it('testHighlightCallbackIsCalled', function() { |
475f7420 KW |
36 | var h_row; |
37 | var h_pts; | |
38 | ||
4ee251cb | 39 | var highlightCallback = function(e, x, pts, row) { |
89fdcedb | 40 | assert.equal(g, this); |
475f7420 KW |
41 | h_row = row; |
42 | h_pts = pts; | |
43 | }; | |
44 | ||
45 | var graph = document.getElementById("graph"); | |
46 | var g = new Dygraph(graph, data, | |
47 | { | |
48 | width: 100, | |
49 | height: 100, | |
50 | visibility: [false, true, true], | |
51 | highlightCallback: highlightCallback | |
52 | }); | |
53 | ||
54 | DygraphOps.dispatchMouseMove(g, 13, 10); | |
55 | ||
56 | //check correct row is returned | |
89fdcedb | 57 | assert.equal(3, h_row); |
475f7420 | 58 | //check there are only two points (because first series is hidden) |
89fdcedb DV |
59 | assert.equal(2, h_pts.length); |
60 | }); | |
475f7420 | 61 | |
a8332379 RK |
62 | |
63 | /** | |
64 | * Test that drawPointCallback isn't called when drawPoints is false | |
65 | */ | |
89fdcedb | 66 | it('testDrawPointCallback_disabled', function() { |
a8332379 RK |
67 | var called = false; |
68 | ||
72c12eda | 69 | var callback = function() { |
89fdcedb | 70 | assert.equal(g, this); |
a8332379 | 71 | called = true; |
5469113b | 72 | }; |
a8332379 RK |
73 | |
74 | var graph = document.getElementById("graph"); | |
75 | var g = new Dygraph(graph, data, { | |
4ee251cb | 76 | drawPointCallback: callback, |
a8332379 RK |
77 | }); |
78 | ||
89fdcedb DV |
79 | assert.isFalse(called); |
80 | }); | |
a8332379 RK |
81 | |
82 | /** | |
83 | * Test that drawPointCallback is called when drawPoints is true | |
84 | */ | |
89fdcedb | 85 | it('testDrawPointCallback_enabled', function() { |
a8332379 | 86 | var called = false; |
4ee251cb | 87 | var callbackThis = null; |
a8332379 | 88 | |
72c12eda | 89 | var callback = function() { |
4ee251cb | 90 | callbackThis = this; |
a8332379 | 91 | called = true; |
5469113b | 92 | }; |
a8332379 RK |
93 | |
94 | var graph = document.getElementById("graph"); | |
95 | var g = new Dygraph(graph, data, { | |
4ee251cb DV |
96 | drawPoints: true, |
97 | drawPointCallback: callback | |
a8332379 RK |
98 | }); |
99 | ||
89fdcedb DV |
100 | assert.isTrue(called); |
101 | assert.equal(g, callbackThis); | |
102 | }); | |
72c12eda RK |
103 | |
104 | /** | |
105 | * Test that drawPointCallback is called when drawPoints is true | |
106 | */ | |
89fdcedb | 107 | it('testDrawPointCallback_pointSize', function() { |
72c12eda RK |
108 | var pointSize = 0; |
109 | var count = 0; | |
110 | ||
111 | var callback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam) { | |
89fdcedb | 112 | assert.equal(g, this); |
72c12eda RK |
113 | pointSize = pointSizeParam; |
114 | count++; | |
5469113b | 115 | }; |
72c12eda RK |
116 | |
117 | var graph = document.getElementById("graph"); | |
118 | var g = new Dygraph(graph, data, { | |
4ee251cb DV |
119 | drawPoints: true, |
120 | drawPointCallback: callback | |
72c12eda RK |
121 | }); |
122 | ||
89fdcedb DV |
123 | assert.equal(1.5, pointSize); |
124 | assert.equal(12, count); // one call per data point. | |
72c12eda RK |
125 | |
126 | var g = new Dygraph(graph, data, { | |
4ee251cb DV |
127 | drawPoints: true, |
128 | drawPointCallback: callback, | |
129 | pointSize: 8 | |
72c12eda RK |
130 | }); |
131 | ||
89fdcedb DV |
132 | assert.equal(8, pointSize); |
133 | }); | |
72c12eda RK |
134 | |
135 | /** | |
41273327 KW |
136 | * Test that drawPointCallback is called for isolated points when |
137 | * drawPoints is false, and also for gap points if that's enabled. | |
138 | */ | |
89fdcedb | 139 | it('testDrawPointCallback_isolated', function() { |
41273327 KW |
140 | var xvalues = []; |
141 | ||
142 | var g; | |
143 | var callback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam) { | |
89fdcedb | 144 | assert.equal(g, this); |
41273327 KW |
145 | var dx = g.toDataXCoord(cx); |
146 | xvalues.push(dx); | |
147 | Dygraph.Circles.DEFAULT.apply(this, arguments); | |
148 | }; | |
149 | ||
150 | var graph = document.getElementById("graph"); | |
151 | var testdata = [[10, 2], [11, 3], [12, NaN], [13, 2], [14, NaN], [15, 3]]; | |
152 | var graphOpts = { | |
153 | labels: ['X', 'Y'], | |
154 | valueRange: [0, 4], | |
155 | drawPoints : false, | |
156 | drawPointCallback : callback, | |
157 | pointSize : 8 | |
158 | }; | |
159 | ||
160 | // Test that isolated points get drawn | |
161 | g = new Dygraph(graph, testdata, graphOpts); | |
89fdcedb DV |
162 | assert.equal(2, xvalues.length); |
163 | assert.equal(13, xvalues[0]); | |
164 | assert.equal(15, xvalues[1]); | |
41273327 | 165 | |
a5a50727 KW |
166 | // Test that isolated points + gap points get drawn when |
167 | // drawGapEdgePoints is set. This should add one point at the right | |
168 | // edge of the segment at x=11, but not at the graph edge at x=10. | |
41273327 | 169 | xvalues = []; // Reset for new test |
a5a50727 | 170 | graphOpts.drawGapEdgePoints = true; |
41273327 | 171 | g = new Dygraph(graph, testdata, graphOpts); |
89fdcedb DV |
172 | assert.equal(3, xvalues.length); |
173 | assert.equal(11, xvalues[0]); | |
174 | assert.equal(13, xvalues[1]); | |
175 | assert.equal(15, xvalues[2]); | |
176 | }); | |
41273327 KW |
177 | |
178 | /** | |
72c12eda | 179 | * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback |
5469113b KW |
180 | * is properly called when the first series is hidden (setVisibility = false) |
181 | * | |
72c12eda | 182 | */ |
89fdcedb | 183 | it('testDrawHighlightPointCallbackIsCalled', function() { |
72c12eda RK |
184 | var called = false; |
185 | ||
4ee251cb | 186 | var drawHighlightPointCallback = function() { |
89fdcedb | 187 | assert.equal(g, this); |
72c12eda | 188 | called = true; |
5469113b | 189 | }; |
72c12eda RK |
190 | |
191 | var graph = document.getElementById("graph"); | |
192 | var g = new Dygraph(graph, data, | |
193 | { | |
194 | width: 100, | |
4ee251cb DV |
195 | height: 100, |
196 | drawHighlightPointCallback: drawHighlightPointCallback | |
72c12eda RK |
197 | }); |
198 | ||
89fdcedb | 199 | assert.isFalse(called); |
72c12eda | 200 | DygraphOps.dispatchMouseMove(g, 13, 10); |
89fdcedb DV |
201 | assert.isTrue(called); |
202 | }); | |
5469113b | 203 | |
475f7420 KW |
204 | /** |
205 | * Test the closest-series highlighting methods for normal and stacked modes. | |
206 | * Also pass in line widths for plain and highlighted lines for easier visual | |
207 | * confirmation that the highlighted line is drawn on top of the others. | |
208 | */ | |
857a6931 KW |
209 | var runClosestTest = function(isStacked, widthNormal, widthHighlighted) { |
210 | var h_row; | |
211 | var h_pts; | |
212 | var h_series; | |
213 | ||
214 | var graph = document.getElementById("graph"); | |
215 | var g = new Dygraph(graph, data, | |
216 | { | |
217 | width: 600, | |
475f7420 | 218 | height: 400, |
857a6931 KW |
219 | visibility: [false, true, true], |
220 | stackedGraph: isStacked, | |
221 | strokeWidth: widthNormal, | |
222 | strokeBorderWidth: 2, | |
223 | highlightCircleSize: widthNormal * 2, | |
afdb20d8 | 224 | highlightSeriesBackgroundAlpha: 0.3, |
857a6931 KW |
225 | |
226 | highlightSeriesOpts: { | |
227 | strokeWidth: widthHighlighted, | |
228 | highlightCircleSize: widthHighlighted * 2 | |
229 | } | |
230 | }); | |
231 | ||
4ee251cb | 232 | var highlightCallback = function(e, x, pts, row, set) { |
89fdcedb | 233 | assert.equal(g, this); |
857a6931 KW |
234 | h_row = row; |
235 | h_pts = pts; | |
236 | h_series = set; | |
237 | document.getElementById('selection').innerHTML='row=' + row + ', set=' + set; | |
238 | }; | |
239 | ||
240 | g.updateOptions({highlightCallback: highlightCallback}, true); | |
241 | ||
242 | if (isStacked) { | |
243 | DygraphOps.dispatchMouseMove(g, 11.45, 1.4); | |
89fdcedb DV |
244 | assert.equal(1, h_row); |
245 | assert.equal('c', h_series); | |
857a6931 KW |
246 | |
247 | //now move up in the same row | |
248 | DygraphOps.dispatchMouseMove(g, 11.45, 1.5); | |
89fdcedb DV |
249 | assert.equal(1, h_row); |
250 | assert.equal('b', h_series); | |
857a6931 KW |
251 | |
252 | //and a bit to the right | |
253 | DygraphOps.dispatchMouseMove(g, 11.55, 1.5); | |
89fdcedb DV |
254 | assert.equal(2, h_row); |
255 | assert.equal('c', h_series); | |
857a6931 KW |
256 | } else { |
257 | DygraphOps.dispatchMouseMove(g, 11, 1.5); | |
89fdcedb DV |
258 | assert.equal(1, h_row); |
259 | assert.equal('c', h_series); | |
857a6931 KW |
260 | |
261 | //now move up in the same row | |
262 | DygraphOps.dispatchMouseMove(g, 11, 2.5); | |
89fdcedb DV |
263 | assert.equal(1, h_row); |
264 | assert.equal('b', h_series); | |
857a6931 KW |
265 | } |
266 | ||
267 | return g; | |
268 | }; | |
269 | ||
270 | /** | |
271 | * Test basic closest-point highlighting. | |
272 | */ | |
89fdcedb | 273 | it('testClosestPointCallback', function() { |
857a6931 | 274 | runClosestTest(false, 1, 3); |
89fdcedb | 275 | }); |
857a6931 KW |
276 | |
277 | /** | |
278 | * Test setSelection() with series name | |
279 | */ | |
89fdcedb | 280 | it('testSetSelection', function() { |
857a6931 | 281 | var g = runClosestTest(false, 1, 3); |
89fdcedb | 282 | assert.equal(1, g.attr_('strokeWidth', 'c')); |
857a6931 | 283 | g.setSelection(false, 'c'); |
89fdcedb DV |
284 | assert.equal(3, g.attr_('strokeWidth', 'c')); |
285 | }); | |
857a6931 KW |
286 | |
287 | /** | |
288 | * Test closest-point highlighting for stacked graph | |
289 | */ | |
89fdcedb | 290 | it('testClosestPointStackedCallback', function() { |
857a6931 | 291 | runClosestTest(true, 1, 3); |
89fdcedb | 292 | }); |
857a6931 KW |
293 | |
294 | /** | |
295 | * Closest-point highlighting with legend CSS - border around active series. | |
296 | */ | |
89fdcedb | 297 | it('testClosestPointCallbackCss1', function() { |
857a6931 | 298 | var css = "div.dygraph-legend > span { display: block; }\n" + |
4ee251cb | 299 | "div.dygraph-legend > span.highlight { border: 1px solid grey; }\n"; |
319d0361 | 300 | styleSheet.innerHTML = css; |
857a6931 | 301 | runClosestTest(false, 2, 4); |
319d0361 | 302 | styleSheet.innerHTML = ''; |
89fdcedb | 303 | }); |
857a6931 KW |
304 | |
305 | /** | |
306 | * Closest-point highlighting with legend CSS - show only closest series. | |
307 | */ | |
89fdcedb | 308 | it('testClosestPointCallbackCss2', function() { |
857a6931 | 309 | var css = "div.dygraph-legend > span { display: none; }\n" + |
4ee251cb | 310 | "div.dygraph-legend > span.highlight { display: inline; }\n"; |
319d0361 | 311 | styleSheet.innerHTML = css; |
857a6931 | 312 | runClosestTest(false, 10, 15); |
319d0361 | 313 | styleSheet.innerHTML = ''; |
857a6931 | 314 | // TODO(klausw): verify that the highlighted line is drawn on top? |
89fdcedb | 315 | }); |
a937d031 KW |
316 | |
317 | /** | |
b9a3ece4 KW |
318 | * Closest-point highlighting with locked series. |
319 | */ | |
89fdcedb | 320 | it('testSetSelectionLocking', function() { |
b9a3ece4 KW |
321 | var g = runClosestTest(false, 2, 4); |
322 | ||
323 | // Default behavior, 'b' is closest | |
324 | DygraphOps.dispatchMouseMove(g, 11, 4); | |
89fdcedb | 325 | assert.equal('b', g.getHighlightSeries()); |
b9a3ece4 KW |
326 | |
327 | // Now lock selection to 'c' | |
328 | g.setSelection(false, 'c', true); | |
329 | DygraphOps.dispatchMouseMove(g, 11, 4); | |
89fdcedb | 330 | assert.equal('c', g.getHighlightSeries()); |
b9a3ece4 KW |
331 | |
332 | // Unlock, should be back to 'b' | |
333 | g.clearSelection(); | |
334 | DygraphOps.dispatchMouseMove(g, 11, 4); | |
89fdcedb DV |
335 | assert.equal('b', g.getHighlightSeries()); |
336 | }); | |
b9a3ece4 KW |
337 | |
338 | /** | |
a937d031 KW |
339 | * This tests that closest point searches work for data containing NaNs. |
340 | * | |
341 | * It's intended to catch a regression where a NaN Y value confuses the | |
342 | * closest-point algorithm, treating it as closer as any previous point. | |
343 | */ | |
89fdcedb | 344 | it('testNaNData', function() { |
a937d031 | 345 | var dataNaN = [ |
1069800f | 346 | [9, -1, NaN, NaN], |
a937d031 KW |
347 | [10, -1, 1, 2], |
348 | [11, 0, 3, 1], | |
349 | [12, 1, 4, NaN], | |
350 | [13, 0, 2, 3], | |
351 | [14, -1, 1, 4]]; | |
352 | ||
353 | var h_row; | |
354 | var h_pts; | |
355 | ||
4ee251cb | 356 | var highlightCallback = function(e, x, pts, row) { |
89fdcedb | 357 | assert.equal(g, this); |
a937d031 KW |
358 | h_row = row; |
359 | h_pts = pts; | |
360 | }; | |
361 | ||
362 | var graph = document.getElementById("graph"); | |
363 | var g = new Dygraph(graph, dataNaN, | |
364 | { | |
365 | width: 600, | |
366 | height: 400, | |
367 | labels: ['x', 'a', 'b', 'c'], | |
368 | visibility: [false, true, true], | |
369 | highlightCallback: highlightCallback | |
370 | }); | |
371 | ||
372 | DygraphOps.dispatchMouseMove(g, 10.1, 0.9); | |
373 | //check correct row is returned | |
89fdcedb | 374 | assert.equal(1, h_row); |
a937d031 KW |
375 | |
376 | // Explicitly test closest point algorithms | |
377 | var dom = g.toDomCoords(10.1, 0.9); | |
89fdcedb | 378 | assert.equal(1, g.findClosestRow(dom[0])); |
a937d031 KW |
379 | |
380 | var res = g.findClosestPoint(dom[0], dom[1]); | |
89fdcedb DV |
381 | assert.equal(1, res.row); |
382 | assert.equal('b', res.seriesName); | |
a937d031 KW |
383 | |
384 | res = g.findStackedPoint(dom[0], dom[1]); | |
89fdcedb DV |
385 | assert.equal(1, res.row); |
386 | assert.equal('c', res.seriesName); | |
387 | }); | |
04c104d7 | 388 | |
7d463f49 KW |
389 | /** |
390 | * This tests that stacked point searches work for data containing NaNs. | |
391 | */ | |
89fdcedb | 392 | it('testNaNDataStack', function() { |
7d463f49 KW |
393 | var dataNaN = [ |
394 | [9, -1, NaN, NaN], | |
395 | [10, -1, 1, 2], | |
396 | [11, 0, 3, 1], | |
397 | [12, 1, NaN, 2], | |
398 | [13, 0, 2, 3], | |
399 | [14, -1, 1, 4], | |
400 | [15, 0, 2, NaN], | |
401 | [16, 1, 1, 3], | |
402 | [17, 1, NaN, 3], | |
403 | [18, 0, 2, 5], | |
404 | [19, 0, 1, 4]]; | |
405 | ||
406 | var h_row; | |
407 | var h_pts; | |
408 | ||
4ee251cb | 409 | var highlightCallback = function(e, x, pts, row) { |
89fdcedb | 410 | assert.equal(g, this); |
7d463f49 KW |
411 | h_row = row; |
412 | h_pts = pts; | |
413 | }; | |
414 | ||
415 | var graph = document.getElementById("graph"); | |
416 | var g = new Dygraph(graph, dataNaN, | |
417 | { | |
418 | width: 600, | |
419 | height: 400, | |
420 | labels: ['x', 'a', 'b', 'c'], | |
421 | visibility: [false, true, true], | |
422 | stackedGraph: true, | |
423 | highlightCallback: highlightCallback | |
424 | }); | |
425 | ||
426 | DygraphOps.dispatchMouseMove(g, 10.1, 0.9); | |
427 | //check correct row is returned | |
89fdcedb | 428 | assert.equal(1, h_row); |
7d463f49 KW |
429 | |
430 | // Explicitly test stacked point algorithm. | |
431 | var dom = g.toDomCoords(10.1, 0.9); | |
432 | var res = g.findStackedPoint(dom[0], dom[1]); | |
89fdcedb DV |
433 | assert.equal(1, res.row); |
434 | assert.equal('c', res.seriesName); | |
7d463f49 | 435 | |
30a5cfc6 KW |
436 | // All-NaN area at left, should get no points. |
437 | dom = g.toDomCoords(9.1, 0.9); | |
438 | res = g.findStackedPoint(dom[0], dom[1]); | |
89fdcedb DV |
439 | assert.equal(0, res.row); |
440 | assert.equal(undefined, res.seriesName); | |
30a5cfc6 KW |
441 | |
442 | // First gap, get 'c' since it's non-NaN. | |
7d463f49 KW |
443 | dom = g.toDomCoords(12.1, 0.9); |
444 | res = g.findStackedPoint(dom[0], dom[1]); | |
89fdcedb DV |
445 | assert.equal(3, res.row); |
446 | assert.equal('c', res.seriesName); | |
7d463f49 | 447 | |
30a5cfc6 | 448 | // Second gap, get 'b' since 'c' is NaN. |
7d463f49 KW |
449 | dom = g.toDomCoords(15.1, 0.9); |
450 | res = g.findStackedPoint(dom[0], dom[1]); | |
89fdcedb DV |
451 | assert.equal(6, res.row); |
452 | assert.equal('b', res.seriesName); | |
7d463f49 KW |
453 | |
454 | // Isolated points should work, finding series b in this case. | |
455 | dom = g.toDomCoords(15.9, 3.1); | |
456 | res = g.findStackedPoint(dom[0], dom[1]); | |
89fdcedb DV |
457 | assert.equal(7, res.row); |
458 | assert.equal('b', res.seriesName); | |
459 | }); | |
7d463f49 | 460 | |
89fdcedb | 461 | it('testGapHighlight', function() { |
492ea455 | 462 | var dataGap = [ |
04c104d7 KW |
463 | [1, null, 3], |
464 | [2, 2, null], | |
465 | [3, null, 5], | |
466 | [4, 4, null], | |
467 | [5, null, 7], | |
468 | [6, NaN, null], | |
469 | [8, 8, null], | |
470 | [10, 10, null]]; | |
471 | ||
472 | var h_row; | |
473 | var h_pts; | |
474 | ||
4ee251cb | 475 | var highlightCallback = function(e, x, pts, row) { |
89fdcedb | 476 | assert.equal(g, this); |
04c104d7 KW |
477 | h_row = row; |
478 | h_pts = pts; | |
479 | }; | |
480 | ||
481 | var graph = document.getElementById("graph"); | |
482 | var g = new Dygraph(graph, dataGap, { | |
483 | width: 400, | |
484 | height: 300, | |
485 | //stackedGraph: true, | |
486 | connectSeparatedPoints: true, | |
487 | drawPoints: true, | |
488 | labels: ['x', 'A', 'B'], | |
4ee251cb | 489 | highlightCallback: highlightCallback |
04c104d7 KW |
490 | }); |
491 | ||
492 | DygraphOps.dispatchMouseMove(g, 1.1, 10); | |
493 | //point from series B | |
89fdcedb DV |
494 | assert.equal(0, h_row); |
495 | assert.equal(1, h_pts.length); | |
496 | assert.equal(3, h_pts[0].yval); | |
497 | assert.equal('B', h_pts[0].name); | |
04c104d7 KW |
498 | |
499 | DygraphOps.dispatchMouseMove(g, 6.1, 10); | |
500 | // A is NaN at x=6 | |
89fdcedb | 501 | assert.equal(1, h_pts.length); |
04c104d7 | 502 | assert(isNaN(h_pts[0].yval)); |
89fdcedb | 503 | assert.equal('A', h_pts[0].name); |
04c104d7 KW |
504 | |
505 | DygraphOps.dispatchMouseMove(g, 8.1, 10); | |
506 | //point from series A | |
89fdcedb DV |
507 | assert.equal(6, h_row); |
508 | assert.equal(1, h_pts.length); | |
509 | assert.equal(8, h_pts[0].yval); | |
510 | assert.equal('A', h_pts[0].name); | |
511 | }); | |
4c10c8d2 | 512 | |
89fdcedb | 513 | it('testFailedResponse', function() { |
4c10c8d2 RK |
514 | |
515 | // Fake out the XMLHttpRequest so it doesn't do anything. | |
516 | XMLHttpRequest = function () {}; | |
517 | XMLHttpRequest.prototype.open = function () {}; | |
518 | XMLHttpRequest.prototype.send = function () {}; | |
519 | ||
520 | var highlightCallback = function(e, x, pts, row) { | |
89fdcedb | 521 | throw "should not reach here"; |
4c10c8d2 RK |
522 | }; |
523 | ||
524 | var graph = document.getElementById("graph"); | |
525 | graph.style.border = "2px solid black"; | |
526 | var g = new Dygraph(graph, "data.csv", { // fake name | |
527 | width: 400, | |
528 | height: 300, | |
529 | highlightCallback : highlightCallback | |
530 | }); | |
531 | ||
532 | DygraphOps.dispatchMouseOver_Point(g, 800, 800); | |
533 | DygraphOps.dispatchMouseMove_Point(g, 100, 100); | |
534 | DygraphOps.dispatchMouseMove_Point(g, 800, 800); | |
535 | ||
536 | var oldOnerror = window.onerror; | |
537 | var failed = false; | |
538 | window.onerror = function() { failed = true; return false; } | |
539 | ||
540 | DygraphOps.dispatchMouseOut_Point(g, 800, 800); // This call should not throw an exception. | |
541 | ||
fecbcf34 | 542 | assert.isFalse(failed, "exception thrown during mouseout"); |
89fdcedb | 543 | }); |
870a309c DV |
544 | |
545 | ||
546 | // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=355 | |
89fdcedb | 547 | it('testHighlightCallbackRow', function() { |
870a309c DV |
548 | var highlightRow; |
549 | var highlightCallback = function(e, x, pts, row) { | |
89fdcedb | 550 | assert.equal(g, this); |
870a309c DV |
551 | highlightRow = row; |
552 | }; | |
553 | ||
554 | var graph = document.getElementById("graph"); | |
555 | var g = new Dygraph(graph, | |
556 | "X,Y,Z\n" + | |
557 | "0,1,2\n" + // 0 | |
558 | "1,2,3\n" + // 100 | |
559 | "2,3,4\n" + // 200 | |
560 | "3,4,5\n" + // 300 | |
561 | "4,5,6\n", // 400 | |
562 | { // fake name | |
563 | width: 400, | |
564 | height: 300, | |
565 | highlightCallback : highlightCallback | |
566 | }); | |
567 | ||
568 | // Mouse over each of the points | |
569 | DygraphOps.dispatchMouseOver_Point(g, 0, 0); | |
570 | DygraphOps.dispatchMouseMove_Point(g, 0, 0); | |
89fdcedb | 571 | assert.equal(0, highlightRow); |
870a309c | 572 | DygraphOps.dispatchMouseMove_Point(g, 100, 0); |
89fdcedb | 573 | assert.equal(1, highlightRow); |
870a309c | 574 | DygraphOps.dispatchMouseMove_Point(g, 200, 0); |
89fdcedb | 575 | assert.equal(2, highlightRow); |
870a309c | 576 | DygraphOps.dispatchMouseMove_Point(g, 300, 0); |
89fdcedb | 577 | assert.equal(3, highlightRow); |
870a309c | 578 | DygraphOps.dispatchMouseMove_Point(g, 400, 0); |
89fdcedb | 579 | assert.equal(4, highlightRow); |
870a309c DV |
580 | |
581 | // Now zoom and verify that the row numbers still refer to rows in the data | |
582 | // array. | |
583 | g.updateOptions({dateWindow: [2, 4]}); | |
584 | DygraphOps.dispatchMouseOver_Point(g, 0, 0); | |
585 | DygraphOps.dispatchMouseMove_Point(g, 0, 0); | |
89fdcedb DV |
586 | assert.equal(2, highlightRow); |
587 | assert.equal('2: Y: 3 Z: 4', Util.getLegend()); | |
588 | }); | |
5fdfe3b1 | 589 | |
590 | /** | |
591 | * Test that underlay callback is called even when there are no series, | |
592 | * and that the y axis ranges are not NaN. | |
593 | */ | |
89fdcedb | 594 | it('testUnderlayCallback_noSeries', function() { |
5fdfe3b1 | 595 | var called = false; |
596 | var yMin, yMax; | |
597 | ||
598 | var callback = function(canvas, area, g) { | |
89fdcedb | 599 | assert.equal(g, this); |
5fdfe3b1 | 600 | called = true; |
601 | yMin = g.yAxisRange(0)[0]; | |
83b552f9 | 602 | yMax = g.yAxisRange(0)[1]; |
5fdfe3b1 | 603 | }; |
604 | ||
605 | var graph = document.getElementById("graph"); | |
606 | var g = new Dygraph(graph, "\n", { | |
607 | underlayCallback: callback | |
608 | }); | |
609 | ||
89fdcedb DV |
610 | assert.isTrue(called); |
611 | assert.isFalse(isNaN(yMin)); | |
612 | assert.isFalse(isNaN(yMax)); | |
613 | }); | |
113f6e16 | 614 | |
615 | /** | |
616 | * Test that underlay callback receives the correct y-axis range. | |
617 | */ | |
89fdcedb | 618 | it('testUnderlayCallback_yAxisRange', function() { |
113f6e16 | 619 | var called = false; |
620 | var yMin, yMax; | |
621 | ||
622 | var callback = function(canvas, area, g) { | |
89fdcedb | 623 | assert.equal(g, this); |
113f6e16 | 624 | yMin = g.yAxisRange(0)[0]; |
625 | yMax = g.yAxisRange(0)[1]; | |
626 | }; | |
627 | ||
628 | var graph = document.getElementById("graph"); | |
629 | var g = new Dygraph(graph, "\n", { | |
630 | valueRange: [0,10], | |
631 | underlayCallback: callback | |
632 | }); | |
633 | ||
89fdcedb DV |
634 | assert.equal(0, yMin); |
635 | assert.equal(10, yMax); | |
636 | }); | |
cd3f2b1a US |
637 | |
638 | /** | |
639 | * Test that drawPointCallback is called for isolated points and correct idx for the point is returned. | |
640 | */ | |
89fdcedb | 641 | it('testDrawPointCallback_idx', function() { |
4ee251cb DV |
642 | var indices = []; |
643 | ||
644 | var g; | |
645 | var callback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam,idx) { | |
89fdcedb | 646 | assert.equal(g, this); |
4ee251cb DV |
647 | indices.push(idx); |
648 | Dygraph.Circles.DEFAULT.apply(this, arguments); | |
649 | }; | |
650 | ||
651 | var graph = document.getElementById("graph"); | |
652 | ||
653 | var testdata = [[10, 2], [11, 3], [12, NaN], [13, 2], [14, NaN], [15, 3]]; | |
654 | var graphOpts = { | |
655 | labels: ['X', 'Y'], | |
656 | valueRange: [0, 4], | |
657 | drawPoints : false, | |
658 | drawPointCallback : callback, | |
659 | pointSize : 8 | |
660 | }; | |
661 | ||
662 | // Test that correct idx for isolated points are passed to the callback. | |
663 | g = new Dygraph(graph, testdata, graphOpts); | |
89fdcedb DV |
664 | assert.equal(2, indices.length); |
665 | assert.deepEqual([3, 5],indices); | |
4ee251cb DV |
666 | |
667 | // Test that correct indices for isolated points + gap points are passed to the callback when | |
668 | // drawGapEdgePoints is set. This should add one point at the right | |
669 | // edge of the segment at x=11, but not at the graph edge at x=10. | |
670 | indices = []; // Reset for new test | |
671 | graphOpts.drawGapEdgePoints = true; | |
672 | g = new Dygraph(graph, testdata, graphOpts); | |
89fdcedb DV |
673 | assert.equal(3, indices.length); |
674 | assert.deepEqual([1, 3, 5],indices); | |
4ee251cb DV |
675 | |
676 | ||
677 | //Test that correct indices are passed to the callback when zoomed in. | |
678 | indices = []; // Reset for new test | |
679 | graphOpts.dateWindow = [12.5,13.5] | |
680 | graphOpts.drawPoints = true; | |
681 | testdata = [[10, 2], [11, 3], [12, 4], [13, 2], [14, 5], [15, 3]]; | |
682 | g = new Dygraph(graph, testdata, graphOpts); | |
89fdcedb DV |
683 | assert.equal(3, indices.length); |
684 | assert.deepEqual([2, 3, 4],indices); | |
685 | }); | |
cd3f2b1a US |
686 | |
687 | /** | |
688 | * Test that the correct idx is returned for the point in the onHiglightCallback. | |
54119c34 | 689 | */ |
89fdcedb | 690 | it('testDrawHighlightPointCallback_idx', function() { |
4ee251cb DV |
691 | var idxToCheck = null; |
692 | ||
693 | var drawHighlightPointCallback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam,idx) { | |
89fdcedb | 694 | assert.equal(g, this); |
4ee251cb DV |
695 | idxToCheck = idx; |
696 | }; | |
697 | var testdata = [[1, 2], [2, 3], [3, NaN], [4, 2], [5, NaN], [6, 3]]; | |
698 | var graph = document.getElementById("graph"); | |
65129ba8 DV |
699 | var g = new Dygraph(graph, testdata, { |
700 | drawHighlightPointCallback: drawHighlightPointCallback, | |
701 | labels: ['X', 'Y'] | |
4ee251cb DV |
702 | }); |
703 | ||
89fdcedb | 704 | assert.isNull(idxToCheck); |
4ee251cb DV |
705 | DygraphOps.dispatchMouseMove(g, 3, 0); |
706 | // check that NaN point is not highlighted | |
89fdcedb | 707 | assert.isNull(idxToCheck); |
4ee251cb DV |
708 | DygraphOps.dispatchMouseMove(g, 1, 2); |
709 | // check that correct index is returned | |
89fdcedb | 710 | assert.equal(0,idxToCheck); |
4ee251cb | 711 | DygraphOps.dispatchMouseMove(g, 6, 3); |
89fdcedb DV |
712 | assert.equal(5,idxToCheck); |
713 | }); | |
714 | ||
715 | }); |