2 * @fileoverview Tests using the "stackedGraph" option.
4 * @author dan@dygraphs.com (Dan Vanderkam)
7 import Dygraph from
'../../src/dygraph';
8 import * as utils from
'../../src/dygraph-utils';
10 import Proxy from
'./Proxy';
11 import CanvasAssertions from
'./CanvasAssertions';
12 import Util from
'./Util';
14 describe("stacked", function() {
17 useProxyCanvas(utils
, Proxy
);
19 it('testCorrectColors', function() {
35 colors
: ['#00ff00', '#0000ff'],
38 var data
= "X,Y1,Y2\n" +
45 var graph
= document
.getElementById("graph");
46 var g
= new Dygraph(graph
, data
, opts
);
48 // y pixels 299-201 = y2 = transparent blue
49 // y pixel 200 = y2 line (blue)
50 // y pixels 199-101 = y1 = transparent green
51 // y pixel 100 = y1 line (green)
52 // y pixels 0-99 = nothing (white)
54 // 38 = round(0.15 * 255)
55 assert
.deepEqual([0, 0, 255, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
56 assert
.deepEqual([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 150));
59 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=358
60 it('testSelectionValues', function() {
64 var data
= "X,Y1,Y2\n" +
71 var graph
= document
.getElementById("graph");
72 var g
= new Dygraph(graph
, data
, opts
);
76 assert
.equal("0: Y1: 1 Y2: 1", Util
.getLegend());
78 // Verify that the behavior is correct with highlightSeriesOpts as well.
80 highlightSeriesOpts
: {
85 assert
.equal("0: Y1: 1 Y2: 1", Util
.getLegend());
88 assert
.equal("1: Y1: 1 Y2: 1", Util
.getLegend());
90 g
.setSelection(0, 'Y2');
91 assert
.equal("0: Y1: 1 Y2: 1", Util
.getLegend());
94 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=176
95 it('testDuplicatedXValue', function() {
103 var data
= "X,Y1\n" +
107 "2,1\n" + // duplicate x-value!
111 var graph
= document
.getElementById("graph");
112 var g
= new Dygraph(graph
, data
, opts
);
114 assert(g
.yAxisRange()[1] < 2);
116 assert
.deepEqual([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
117 assert
.deepEqual([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 317, 250));
120 // Validates regression when null values in stacked graphs show up
121 // incorrectly in the legend.
122 it('testNullValues', function() {
127 var data
= "X,Y1,Y2,Y3\n" +
135 var graph
= document
.getElementById("graph");
136 var g
= new Dygraph(graph
, data
, opts
);
139 assert
.equal("0: Y1: -5 Y2: -1 Y3: 1", Util
.getLegend());
142 assert
.equal("1: Y1: 1 Y3: 1", Util
.getLegend());
145 assert
.equal("2: Y1: 1 Y2: 2 Y3: 3", Util
.getLegend());
148 assert
.equal("3: Y1: 3 Y3: 4", Util
.getLegend());
151 assert
.equal("4: Y1: 3 Y2: 2 Y3: 3", Util
.getLegend());
154 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=438
155 it('testMissingValueAtZero', function() {
159 var data
= "X,Y1,Y2\n" +
165 var graph
= document
.getElementById("graph");
166 var g
= new Dygraph(graph
, data
, opts
);
169 assert
.equal("0: Y2: 1", Util
.getLegend());
172 assert
.equal("1: Y1: 1 Y2: 2", Util
.getLegend());
175 assert
.equal("2: Y2: 3", Util
.getLegend());
178 it('testInterpolation', function() {
180 colors
: ['#ff0000', '#00ff00', '#0000ff'],
182 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4']
185 // The last series is all-NaN, it ought to be treated as all zero
186 // for stacking purposes.
200 var graph
= document
.getElementById("graph");
201 var g
= new Dygraph(graph
, data
, opts
);
203 var htx
= g
.hidden_ctx_
;
206 // Check that lines are drawn at the expected positions, using
207 // interpolated values for missing data.
208 CanvasAssertions
.assertLineDrawn(
209 htx
, g
.toDomCoords(100, 4), g
.toDomCoords(101, 4), {strokeStyle
: '#00ff00'});
210 CanvasAssertions
.assertLineDrawn(
211 htx
, g
.toDomCoords(102, 6), g
.toDomCoords(103, 7), {strokeStyle
: '#ff0000'});
212 CanvasAssertions
.assertLineDrawn(
213 htx
, g
.toDomCoords(107, 8), g
.toDomCoords(108, 9), {strokeStyle
: '#0000ff'});
214 CanvasAssertions
.assertLineDrawn(
215 htx
, g
.toDomCoords(108, 12), g
.toDomCoords(109, 12), {strokeStyle
: '#ff0000'});
217 // Check that the expected number of line segments gets drawn
218 // for each series. Gaps don't get a line.
219 assert
.equal(7, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
220 assert
.equal(4, CanvasAssertions
.numLinesDrawn(htx
, '#00ff00'));
221 assert
.equal(2, CanvasAssertions
.numLinesDrawn(htx
, '#0000ff'));
223 // Check that the selection returns the original (non-stacked)
224 // values and skips gaps.
226 assert
.equal("101: Y1: 1 Y2: 2 Y3: 2", Util
.getLegend());
229 assert
.equal("108: Y1: 1 Y2: 2 Y3: 9", Util
.getLegend());
232 assert
.equal("109: Y1: 1", Util
.getLegend());
235 it('testInterpolationOptions', function() {
237 colors
: ['#ff0000', '#00ff00', '#0000ff'],
239 labels
: ['X', 'Y1', 'Y2', 'Y3']
249 var choices
= ['all', 'inside', 'none'];
255 for (var i
= 0; i
< choices
.length
; ++i
) {
256 var graph
= document
.getElementById("graph");
257 opts
['stackedGraphNaNFill'] = choices
[i
];
258 var g
= new Dygraph(graph
, data
, opts
);
260 var htx
= g
.hidden_ctx_
;
263 // Check top lines get drawn at the expected positions.
264 for (var j
= 0; j
< stackedY
[i
].length
- 1; ++j
) {
265 CanvasAssertions
.assertLineDrawn(
267 g
.toDomCoords(100 + j
, stackedY
[i
][j
]),
268 g
.toDomCoords(101 + j
, stackedY
[i
][j
+ 1]),
269 {strokeStyle
: '#ff0000'});
274 it('testMultiAxisInterpolation', function() {
275 // Setting 2 axes to test that each axis stacks separately
277 colors
: ['#ff0000', '#00ff00', '#0000ff'],
293 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4']
296 // The last series is all-NaN, it ought to be treated as all zero
297 // for stacking purposes.
311 var graph
= document
.getElementById("graph");
312 var g
= new Dygraph(graph
, data
, opts
);
314 var htx
= g
.hidden_ctx_
;
317 // Check that lines are drawn at the expected positions, using
318 // interpolated values for missing data.
319 CanvasAssertions
.assertLineDrawn(
320 htx
, g
.toDomCoords(100, 2), g
.toDomCoords(101, 2), {strokeStyle
: '#00ff00'});
321 CanvasAssertions
.assertLineDrawn(
322 htx
, g
.toDomCoords(102, 3), g
.toDomCoords(103, 3), {strokeStyle
: '#ff0000'});
323 CanvasAssertions
.assertLineDrawn(
324 htx
, g
.toDomCoords(107, 2.71), g
.toDomCoords(108, 3), {strokeStyle
: '#0000ff'});
325 CanvasAssertions
.assertLineDrawn(
326 htx
, g
.toDomCoords(108, 3), g
.toDomCoords(109, 3), {strokeStyle
: '#ff0000'});
328 // Check that the expected number of line segments gets drawn
329 // for each series. Gaps don't get a line.
330 assert
.equal(7, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
331 assert
.equal(4, CanvasAssertions
.numLinesDrawn(htx
, '#00ff00'));
332 assert
.equal(2, CanvasAssertions
.numLinesDrawn(htx
, '#0000ff'));
334 // Check that the selection returns the original (non-stacked)
335 // values and skips gaps.
337 assert
.equal("101: Y1: 1 Y2: 2 Y3: 2", Util
.getLegend());
340 assert
.equal("108: Y1: 1 Y2: 2 Y3: 9", Util
.getLegend());
343 assert
.equal("109: Y1: 1", Util
.getLegend());