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 * This tests that closest point searches work for data containing NaNs.
307 * It's intended to catch a regression where a NaN Y value confuses the
308 * closest-point algorithm, treating it as closer as any previous point.
310 CallbackTestCase
.prototype.testNaNData
= function() {
322 var highlightCallback
= function(e
, x
, pts
, row
) {
327 var graph
= document
.getElementById("graph");
328 var g
= new Dygraph(graph
, dataNaN
,
332 labels
: ['x', 'a', 'b', 'c'],
333 visibility
: [false, true, true],
334 highlightCallback
: highlightCallback
337 DygraphOps
.dispatchMouseMove(g
, 10.1, 0.9);
338 //check correct row is returned
339 assertEquals(1, h_row
);
341 // Explicitly test closest point algorithms
342 var dom
= g
.toDomCoords(10.1, 0.9);
343 assertEquals(1, g
.findClosestRow(dom
[0]));
345 var res
= g
.findClosestPoint(dom
[0], dom
[1]);
346 assertEquals(1, res
.row
);
347 assertEquals('b', res
.seriesName
);
349 res
= g
.findStackedPoint(dom
[0], dom
[1]);
350 assertEquals(1, res
.row
);
351 assertEquals('c', res
.seriesName
);
355 * This tests that stacked point searches work for data containing NaNs.
357 CallbackTestCase
.prototype.testNaNDataStack
= function() {
374 var highlightCallback
= function(e
, x
, pts
, row
) {
379 var graph
= document
.getElementById("graph");
380 var g
= new Dygraph(graph
, dataNaN
,
384 labels
: ['x', 'a', 'b', 'c'],
385 visibility
: [false, true, true],
387 highlightCallback
: highlightCallback
390 DygraphOps
.dispatchMouseMove(g
, 10.1, 0.9);
391 //check correct row is returned
392 assertEquals(1, h_row
);
394 // Explicitly test stacked point algorithm.
395 var dom
= g
.toDomCoords(10.1, 0.9);
396 var res
= g
.findStackedPoint(dom
[0], dom
[1]);
397 assertEquals(1, res
.row
);
398 assertEquals('c', res
.seriesName
);
400 // First gap, no data due to NaN contagion.
401 dom
= g
.toDomCoords(12.1, 0.9);
402 res
= g
.findStackedPoint(dom
[0], dom
[1]);
403 assertEquals(3, res
.row
);
404 assertEquals(undefined
, res
.seriesName
);
406 // Second gap, no data due to NaN contagion.
407 dom
= g
.toDomCoords(15.1, 0.9);
408 res
= g
.findStackedPoint(dom
[0], dom
[1]);
409 assertEquals(6, res
.row
);
410 assertEquals(undefined
, res
.seriesName
);
412 // Isolated points should work, finding series b in this case.
413 dom
= g
.toDomCoords(15.9, 3.1);
414 res
= g
.findStackedPoint(dom
[0], dom
[1]);
415 assertEquals(7, res
.row
);
416 assertEquals('b', res
.seriesName
);
419 CallbackTestCase
.prototype.testGapHighlight
= function() {
433 var highlightCallback
= function(e
, x
, pts
, row
) {
438 var graph
= document
.getElementById("graph");
439 var g
= new Dygraph(graph
, dataGap
, {
442 //stackedGraph: true,
443 connectSeparatedPoints
: true,
445 labels
: ['x', 'A', 'B'],
446 highlightCallback
: highlightCallback
449 DygraphOps
.dispatchMouseMove(g
, 1.1, 10);
450 //point from series B
451 assertEquals(0, h_row
);
452 assertEquals(1, h_pts
.length
);
453 assertEquals(3, h_pts
[0].yval
);
454 assertEquals('B', h_pts
[0].name
);
456 DygraphOps
.dispatchMouseMove(g
, 6.1, 10);
458 assertEquals(1, h_pts
.length
);
459 assert(isNaN(h_pts
[0].yval
));
460 assertEquals('A', h_pts
[0].name
);
462 DygraphOps
.dispatchMouseMove(g
, 8.1, 10);
463 //point from series A
464 assertEquals(6, h_row
);
465 assertEquals(1, h_pts
.length
);
466 assertEquals(8, h_pts
[0].yval
);
467 assertEquals('A', h_pts
[0].name
);