2 * @fileoverview Test cases for the callbacks.
4 * @author uemit.seren@gmail.com (Ümit Seren)
7 var CallbackTestCase
= TestCase("callback");
9 CallbackTestCase
.prototype.setUp
= function() {
10 document
.body
.innerHTML
= "<div id='graph'></div><div id='selection'></div>";
11 this.styleSheet
= document
.createElement("style");
12 this.styleSheet
.type
= "text/css";
13 document
.getElementsByTagName("head")[0].appendChild(this.styleSheet
);
16 CallbackTestCase
.prototype.tearDown
= function() {
19 var data
= "X,a\,b,c\n" +
27 * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback
28 * is properly called when the first series is hidden (setVisibility = false)
31 CallbackTestCase
.prototype.testHighlightCallbackIsCalled
= function() {
35 var highlightCallback
= function(e
, x
, pts
, row
) {
40 var graph
= document
.getElementById("graph");
41 var g
= new Dygraph(graph
, data
,
45 visibility
: [false, true, true],
46 highlightCallback
: highlightCallback
49 DygraphOps
.dispatchMouseMove(g
, 13, 10);
51 //check correct row is returned
52 assertEquals(3, h_row
);
53 //check there are only two points (because first series is hidden)
54 assertEquals(2, h_pts
.length
);
59 * Test that drawPointCallback isn't called when drawPoints is false
61 CallbackTestCase
.prototype.testDrawPointCallback_disabled
= function() {
64 var callback
= function() {
68 var graph
= document
.getElementById("graph");
69 var g
= new Dygraph(graph
, data
, {
70 drawPointCallback
: callback
,
77 * Test that drawPointCallback is called when drawPoints is true
79 CallbackTestCase
.prototype.testDrawPointCallback_enabled
= function() {
82 var callback
= function() {
86 var graph
= document
.getElementById("graph");
87 var g
= new Dygraph(graph
, data
, {
89 drawPointCallback
: callback
96 * Test that drawPointCallback is called when drawPoints is true
98 CallbackTestCase
.prototype.testDrawPointCallback_pointSize
= function() {
102 var callback
= function(g
, seriesName
, canvasContext
, cx
, cy
, color
, pointSizeParam
) {
103 pointSize
= pointSizeParam
;
107 var graph
= document
.getElementById("graph");
108 var g
= new Dygraph(graph
, data
, {
110 drawPointCallback
: callback
113 assertEquals(1.5, pointSize
);
114 assertEquals(12, count
); // one call per data point.
116 var g
= new Dygraph(graph
, data
, {
118 drawPointCallback
: callback
,
122 assertEquals(8, pointSize
);
126 * Test that drawPointCallback is called for isolated points when
127 * drawPoints is false, and also for gap points if that's enabled.
129 CallbackTestCase
.prototype.testDrawPointCallback_isolated
= function() {
133 var callback
= function(g
, seriesName
, canvasContext
, cx
, cy
, color
, pointSizeParam
) {
134 var dx
= g
.toDataXCoord(cx
);
136 Dygraph
.Circles
.DEFAULT
.apply(this, arguments
);
139 var graph
= document
.getElementById("graph");
140 var testdata
= [[10, 2], [11, 3], [12, NaN
], [13, 2], [14, NaN
], [15, 3]];
145 drawPointCallback
: callback
,
149 // Test that isolated points get drawn
150 g
= new Dygraph(graph
, testdata
, graphOpts
);
151 assertEquals(2, xvalues
.length
);
152 assertEquals(13, xvalues
[0]);
153 assertEquals(15, xvalues
[1]);
155 // Test that isolated points + gap points get drawn when
156 // drawGapEdgePoints is set. This should add one point at the right
157 // edge of the segment at x=11, but not at the graph edge at x=10.
158 xvalues
= []; // Reset for new test
159 graphOpts
.drawGapEdgePoints
= true;
160 g
= new Dygraph(graph
, testdata
, graphOpts
);
161 assertEquals(3, xvalues
.length
);
162 assertEquals(11, xvalues
[0]);
163 assertEquals(13, xvalues
[1]);
164 assertEquals(15, xvalues
[2]);
168 * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback
169 * is properly called when the first series is hidden (setVisibility = false)
172 CallbackTestCase
.prototype.testDrawHighlightPointCallbackIsCalled
= function() {
175 var drawHighlightPointCallback
= function() {
179 var graph
= document
.getElementById("graph");
180 var g
= new Dygraph(graph
, data
,
184 drawHighlightPointCallback
: drawHighlightPointCallback
188 DygraphOps
.dispatchMouseMove(g
, 13, 10);
193 * Test the closest-series highlighting methods for normal and stacked modes.
194 * Also pass in line widths for plain and highlighted lines for easier visual
195 * confirmation that the highlighted line is drawn on top of the others.
197 var runClosestTest
= function(isStacked
, widthNormal
, widthHighlighted
) {
202 var graph
= document
.getElementById("graph");
203 var g
= new Dygraph(graph
, data
,
207 visibility
: [false, true, true],
208 stackedGraph
: isStacked
,
209 strokeWidth
: widthNormal
,
210 strokeBorderWidth
: 2,
211 highlightCircleSize
: widthNormal
* 2,
212 highlightSeriesBackgroundAlpha
: 0.3,
214 highlightSeriesOpts
: {
215 strokeWidth
: widthHighlighted
,
216 highlightCircleSize
: widthHighlighted
* 2
220 var highlightCallback
= function(e
, x
, pts
, row
, set
) {
224 document
.getElementById('selection').innerHTML
='row=' + row
+ ', set=' + set
;
227 g
.updateOptions({highlightCallback
: highlightCallback
}, true);
230 DygraphOps
.dispatchMouseMove(g
, 11.45, 1.4);
231 assertEquals(1, h_row
);
232 assertEquals('c', h_series
);
234 //now move up in the same row
235 DygraphOps
.dispatchMouseMove(g
, 11.45, 1.5);
236 assertEquals(1, h_row
);
237 assertEquals('b', h_series
);
239 //and a bit to the right
240 DygraphOps
.dispatchMouseMove(g
, 11.55, 1.5);
241 assertEquals(2, h_row
);
242 assertEquals('c', h_series
);
244 DygraphOps
.dispatchMouseMove(g
, 11, 1.5);
245 assertEquals(1, h_row
);
246 assertEquals('c', h_series
);
248 //now move up in the same row
249 DygraphOps
.dispatchMouseMove(g
, 11, 2.5);
250 assertEquals(1, h_row
);
251 assertEquals('b', h_series
);
258 * Test basic closest-point highlighting.
260 CallbackTestCase
.prototype.testClosestPointCallback
= function() {
261 runClosestTest(false, 1, 3);
265 * Test setSelection() with series name
267 CallbackTestCase
.prototype.testSetSelection
= function() {
268 var g
= runClosestTest(false, 1, 3);
269 assertEquals(1, g
.attr_('strokeWidth', 'c'));
270 g
.setSelection(false, 'c');
271 assertEquals(3, g
.attr_('strokeWidth', 'c'));
275 * Test closest-point highlighting for stacked graph
277 CallbackTestCase
.prototype.testClosestPointStackedCallback
= function() {
278 runClosestTest(true, 1, 3);
282 * Closest-point highlighting with legend CSS - border around active series.
284 CallbackTestCase
.prototype.testClosestPointCallbackCss1
= function() {
285 var css
= "div.dygraph-legend > span { display: block; }\n" +
286 "div.dygraph-legend > span.highlight { border: 1px solid grey; }\n";
287 this.styleSheet
.innerHTML
= css
;
288 runClosestTest(false, 2, 4);
289 this.styleSheet
.innerHTML
= '';
293 * Closest-point highlighting with legend CSS - show only closest series.
295 CallbackTestCase
.prototype.testClosestPointCallbackCss2
= function() {
296 var css
= "div.dygraph-legend > span { display: none; }\n" +
297 "div.dygraph-legend > span.highlight { display: inline; }\n";
298 this.styleSheet
.innerHTML
= css
;
299 runClosestTest(false, 10, 15);
300 this.styleSheet
.innerHTML
= '';
301 // TODO(klausw): verify that the highlighted line is drawn on top?
305 * Closest-point highlighting with locked series.
307 CallbackTestCase
.prototype.testClosestPointCallbackCss1
= function() {
308 var g
= runClosestTest(false, 2, 4);
310 // Default behavior, 'b' is closest
311 DygraphOps
.dispatchMouseMove(g
, 11, 4);
312 assertEquals('b', g
.getHighlightSeries());
314 // Now lock selection to 'c'
315 g
.setSelection(false, 'c', true);
316 DygraphOps
.dispatchMouseMove(g
, 11, 4);
317 assertEquals('c', g
.getHighlightSeries());
319 // Unlock, should be back to 'b'
321 DygraphOps
.dispatchMouseMove(g
, 11, 4);
322 assertEquals('b', g
.getHighlightSeries());
326 * This tests that closest point searches work for data containing NaNs.
328 * It's intended to catch a regression where a NaN Y value confuses the
329 * closest-point algorithm, treating it as closer as any previous point.
331 CallbackTestCase
.prototype.testNaNData
= function() {
343 var highlightCallback
= function(e
, x
, pts
, row
) {
348 var graph
= document
.getElementById("graph");
349 var g
= new Dygraph(graph
, dataNaN
,
353 labels
: ['x', 'a', 'b', 'c'],
354 visibility
: [false, true, true],
355 highlightCallback
: highlightCallback
358 DygraphOps
.dispatchMouseMove(g
, 10.1, 0.9);
359 //check correct row is returned
360 assertEquals(1, h_row
);
362 // Explicitly test closest point algorithms
363 var dom
= g
.toDomCoords(10.1, 0.9);
364 assertEquals(1, g
.findClosestRow(dom
[0]));
366 var res
= g
.findClosestPoint(dom
[0], dom
[1]);
367 assertEquals(1, res
.row
);
368 assertEquals('b', res
.seriesName
);
370 res
= g
.findStackedPoint(dom
[0], dom
[1]);
371 assertEquals(1, res
.row
);
372 assertEquals('c', res
.seriesName
);
376 * This tests that stacked point searches work for data containing NaNs.
378 CallbackTestCase
.prototype.testNaNDataStack
= function() {
395 var highlightCallback
= function(e
, x
, pts
, row
) {
400 var graph
= document
.getElementById("graph");
401 var g
= new Dygraph(graph
, dataNaN
,
405 labels
: ['x', 'a', 'b', 'c'],
406 visibility
: [false, true, true],
408 highlightCallback
: highlightCallback
411 DygraphOps
.dispatchMouseMove(g
, 10.1, 0.9);
412 //check correct row is returned
413 assertEquals(1, h_row
);
415 // Explicitly test stacked point algorithm.
416 var dom
= g
.toDomCoords(10.1, 0.9);
417 var res
= g
.findStackedPoint(dom
[0], dom
[1]);
418 assertEquals(1, res
.row
);
419 assertEquals('c', res
.seriesName
);
421 // First gap, no data due to NaN contagion.
422 dom
= g
.toDomCoords(12.1, 0.9);
423 res
= g
.findStackedPoint(dom
[0], dom
[1]);
424 assertEquals(3, res
.row
);
425 assertEquals(undefined
, res
.seriesName
);
427 // Second gap, no data due to NaN contagion.
428 dom
= g
.toDomCoords(15.1, 0.9);
429 res
= g
.findStackedPoint(dom
[0], dom
[1]);
430 assertEquals(6, res
.row
);
431 assertEquals(undefined
, res
.seriesName
);
433 // Isolated points should work, finding series b in this case.
434 dom
= g
.toDomCoords(15.9, 3.1);
435 res
= g
.findStackedPoint(dom
[0], dom
[1]);
436 assertEquals(7, res
.row
);
437 assertEquals('b', res
.seriesName
);
440 CallbackTestCase
.prototype.testGapHighlight
= function() {
454 var highlightCallback
= function(e
, x
, pts
, row
) {
459 var graph
= document
.getElementById("graph");
460 var g
= new Dygraph(graph
, dataGap
, {
463 //stackedGraph: true,
464 connectSeparatedPoints
: true,
466 labels
: ['x', 'A', 'B'],
467 highlightCallback
: highlightCallback
470 DygraphOps
.dispatchMouseMove(g
, 1.1, 10);
471 //point from series B
472 assertEquals(0, h_row
);
473 assertEquals(1, h_pts
.length
);
474 assertEquals(3, h_pts
[0].yval
);
475 assertEquals('B', h_pts
[0].name
);
477 DygraphOps
.dispatchMouseMove(g
, 6.1, 10);
479 assertEquals(1, h_pts
.length
);
480 assert(isNaN(h_pts
[0].yval
));
481 assertEquals('A', h_pts
[0].name
);
483 DygraphOps
.dispatchMouseMove(g
, 8.1, 10);
484 //point from series A
485 assertEquals(6, h_row
);
486 assertEquals(1, h_pts
.length
);
487 assertEquals(8, h_pts
[0].yval
);
488 assertEquals('A', h_pts
[0].name
);