2 * @fileoverview Tests using the "stackedGraph" option.
4 * @author dan@dygraphs.com (Dan Vanderkam)
6 describe("stacked", function() {
8 var _origGetContext
= Dygraph
.getContext
;
10 beforeEach(function() {
11 document
.body
.innerHTML
= "<div id='graph'></div>";
12 Dygraph
.getContext
= function(canvas
) {
13 return new Proxy(_origGetContext(canvas
));
17 afterEach(function() {
18 Dygraph
.getContext
= _origGetContext
;
21 it('testCorrectColors', function() {
37 colors
: ['#00ff00', '#0000ff'],
40 var data
= "X,Y1,Y2\n" +
47 var graph
= document
.getElementById("graph");
48 var g
= new Dygraph(graph
, data
, opts
);
50 // y pixels 299-201 = y2 = transparent blue
51 // y pixel 200 = y2 line (blue)
52 // y pixels 199-101 = y1 = transparent green
53 // y pixel 100 = y1 line (green)
54 // y pixels 0-99 = nothing (white)
56 // 38 = round(0.15 * 255)
57 assert
.deepEqual([0, 0, 255, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
58 assert
.deepEqual([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 150));
61 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=358
62 it('testSelectionValues', function() {
66 var data
= "X,Y1,Y2\n" +
73 var graph
= document
.getElementById("graph");
74 var g
= new Dygraph(graph
, data
, opts
);
78 assert
.equal("0: Y1: 1 Y2: 1", Util
.getLegend());
80 // Verify that the behavior is correct with highlightSeriesOpts as well.
82 highlightSeriesOpts
: {
87 assert
.equal("0: Y1: 1 Y2: 1", Util
.getLegend());
90 assert
.equal("1: Y1: 1 Y2: 1", Util
.getLegend());
92 g
.setSelection(0, 'Y2');
93 assert
.equal("0: Y1: 1 Y2: 1", Util
.getLegend());
96 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=176
97 it('testDuplicatedXValue', function() {
105 var data
= "X,Y1\n" +
109 "2,1\n" + // duplicate x-value!
113 var graph
= document
.getElementById("graph");
114 var g
= new Dygraph(graph
, data
, opts
);
116 assert(g
.yAxisRange()[1] < 2);
118 assert
.deepEqual([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
119 assert
.deepEqual([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 317, 250));
122 // Validates regression when null values in stacked graphs show up
123 // incorrectly in the legend.
124 it('testNullValues', function() {
129 var data
= "X,Y1,Y2,Y3\n" +
137 var graph
= document
.getElementById("graph");
138 var g
= new Dygraph(graph
, data
, opts
);
141 assert
.equal("0: Y1: -5 Y2: -1 Y3: 1", Util
.getLegend());
144 assert
.equal("1: Y1: 1 Y3: 1", Util
.getLegend());
147 assert
.equal("2: Y1: 1 Y2: 2 Y3: 3", Util
.getLegend());
150 assert
.equal("3: Y1: 3 Y3: 4", Util
.getLegend());
153 assert
.equal("4: Y1: 3 Y2: 2 Y3: 3", Util
.getLegend());
156 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=438
157 it('testMissingValueAtZero', function() {
161 var data
= "X,Y1,Y2\n" +
167 var graph
= document
.getElementById("graph");
168 var g
= new Dygraph(graph
, data
, opts
);
171 assert
.equal("0: Y2: 1", Util
.getLegend());
174 assert
.equal("1: Y1: 1 Y2: 2", Util
.getLegend());
177 assert
.equal("2: Y2: 3", Util
.getLegend());
180 it('testInterpolation', function() {
182 colors
: ['#ff0000', '#00ff00', '#0000ff'],
184 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4']
187 // The last series is all-NaN, it ought to be treated as all zero
188 // for stacking purposes.
202 var graph
= document
.getElementById("graph");
203 var g
= new Dygraph(graph
, data
, opts
);
205 var htx
= g
.hidden_ctx_
;
208 // Check that lines are drawn at the expected positions, using
209 // interpolated values for missing data.
210 CanvasAssertions
.assertLineDrawn(
211 htx
, g
.toDomCoords(100, 4), g
.toDomCoords(101, 4), {strokeStyle
: '#00ff00'});
212 CanvasAssertions
.assertLineDrawn(
213 htx
, g
.toDomCoords(102, 6), g
.toDomCoords(103, 7), {strokeStyle
: '#ff0000'});
214 CanvasAssertions
.assertLineDrawn(
215 htx
, g
.toDomCoords(107, 8), g
.toDomCoords(108, 9), {strokeStyle
: '#0000ff'});
216 CanvasAssertions
.assertLineDrawn(
217 htx
, g
.toDomCoords(108, 12), g
.toDomCoords(109, 12), {strokeStyle
: '#ff0000'});
219 // Check that the expected number of line segments gets drawn
220 // for each series. Gaps don't get a line.
221 assert
.equal(7, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
222 assert
.equal(4, CanvasAssertions
.numLinesDrawn(htx
, '#00ff00'));
223 assert
.equal(2, CanvasAssertions
.numLinesDrawn(htx
, '#0000ff'));
225 // Check that the selection returns the original (non-stacked)
226 // values and skips gaps.
228 assert
.equal("101: Y1: 1 Y2: 2 Y3: 2", Util
.getLegend());
231 assert
.equal("108: Y1: 1 Y2: 2 Y3: 9", Util
.getLegend());
234 assert
.equal("109: Y1: 1", Util
.getLegend());
237 it('testInterpolationOptions', function() {
239 colors
: ['#ff0000', '#00ff00', '#0000ff'],
241 labels
: ['X', 'Y1', 'Y2', 'Y3']
251 var choices
= ['all', 'inside', 'none'];
257 for (var i
= 0; i
< choices
.length
; ++i
) {
258 var graph
= document
.getElementById("graph");
259 opts
['stackedGraphNaNFill'] = choices
[i
];
260 var g
= new Dygraph(graph
, data
, opts
);
262 var htx
= g
.hidden_ctx_
;
265 // Check top lines get drawn at the expected positions.
266 for (var j
= 0; j
< stackedY
[i
].length
- 1; ++j
) {
267 CanvasAssertions
.assertLineDrawn(
269 g
.toDomCoords(100 + j
, stackedY
[i
][j
]),
270 g
.toDomCoords(101 + j
, stackedY
[i
][j
+ 1]),
271 {strokeStyle
: '#ff0000'});
276 it('testMultiAxisInterpolation', function() {
277 // Setting 2 axes to test that each axis stacks separately
279 colors
: ['#ff0000', '#00ff00', '#0000ff'],
295 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4']
298 // The last series is all-NaN, it ought to be treated as all zero
299 // for stacking purposes.
313 var graph
= document
.getElementById("graph");
314 var g
= new Dygraph(graph
, data
, opts
);
316 var htx
= g
.hidden_ctx_
;
319 // Check that lines are drawn at the expected positions, using
320 // interpolated values for missing data.
321 CanvasAssertions
.assertLineDrawn(
322 htx
, g
.toDomCoords(100, 2), g
.toDomCoords(101, 2), {strokeStyle
: '#00ff00'});
323 CanvasAssertions
.assertLineDrawn(
324 htx
, g
.toDomCoords(102, 3), g
.toDomCoords(103, 3), {strokeStyle
: '#ff0000'});
325 CanvasAssertions
.assertLineDrawn(
326 htx
, g
.toDomCoords(107, 2.71), g
.toDomCoords(108, 3), {strokeStyle
: '#0000ff'});
327 CanvasAssertions
.assertLineDrawn(
328 htx
, g
.toDomCoords(108, 3), g
.toDomCoords(109, 3), {strokeStyle
: '#ff0000'});
330 // Check that the expected number of line segments gets drawn
331 // for each series. Gaps don't get a line.
332 assert
.equal(7, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
333 assert
.equal(4, CanvasAssertions
.numLinesDrawn(htx
, '#00ff00'));
334 assert
.equal(2, CanvasAssertions
.numLinesDrawn(htx
, '#0000ff'));
336 // Check that the selection returns the original (non-stacked)
337 // values and skips gaps.
339 assert
.equal("101: Y1: 1 Y2: 2 Y3: 2", Util
.getLegend());
342 assert
.equal("108: Y1: 1 Y2: 2 Y3: 9", Util
.getLegend());
345 assert
.equal("109: Y1: 1", Util
.getLegend());