b8c095eb651b58111419c6b43074d5597280fa5b
2 * @fileoverview Tests using the "stackedGraph" option.
4 * @author dan@dygraphs.com (Dan Vanderkam)
6 var stackedTestCase
= TestCase("stacked");
8 stackedTestCase
._origGetContext
= Dygraph
.getContext
;
10 stackedTestCase
.prototype.setUp
= function() {
11 document
.body
.innerHTML
= "<div id='graph'></div>";
12 Dygraph
.getContext
= function(canvas
) {
13 return new Proxy(stackedTestCase
._origGetContext(canvas
));
17 stackedTestCase
.prototype.tearDown
= function() {
18 Dygraph
.getContext
= stackedTestCase
._origGetContext
;
21 stackedTestCase
.prototype.testCorrectColors
= function() {
31 colors
: ['#00ff00', '#0000ff'],
34 var data
= "X,Y1,Y2\n" +
41 var graph
= document
.getElementById("graph");
42 var g
= new Dygraph(graph
, data
, opts
);
44 // y pixels 299-201 = y2 = transparent blue
45 // y pixel 200 = y2 line (blue)
46 // y pixels 199-101 = y1 = transparent green
47 // y pixel 100 = y1 line (green)
48 // y pixels 0-99 = nothing (white)
50 // TODO(danvk): factor this and getPixel() into a utility usable by all tests.
51 var ctx
= g
.hidden_ctx_
;
52 var imageData
= ctx
.getImageData(0, 0, 400, 300);
54 assertEquals(400, imageData
.width
);
55 assertEquals(300, imageData
.height
);
57 // returns an (r, g, b, alpha) tuple for the pixel.
58 // values are in [0, 255].
59 var getPixel
= function(imageData
, x
, y
) {
60 var i
= 4 * (x
+ imageData
.width
* y
);
61 var d
= imageData
.data
;
62 return [d
[i
], d
[i
+1], d
[i
+2], d
[i
+3]];
65 // 38 = round(0.15 * 255)
66 assertEquals([0, 0, 255, 38], getPixel(imageData
, 200, 250));
67 assertEquals([0, 255, 0, 38], getPixel(imageData
, 200, 150));
70 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=358
71 stackedTestCase
.prototype.testSelectionValues
= function() {
75 var data
= "X,Y1,Y2\n" +
82 var graph
= document
.getElementById("graph");
83 g
= new Dygraph(graph
, data
, opts
);
87 assertEquals("0: Y1: 1 Y2: 1", Util
.getLegend());
89 // Verify that the behavior is correct with highlightSeriesOpts as well.
91 highlightSeriesOpts
: {
96 assertEquals("0: Y1: 1 Y2: 1", Util
.getLegend());
99 assertEquals("1: Y1: 1 Y2: 1", Util
.getLegend());
101 g
.setSelection(0, 'Y2');
102 assertEquals("0: Y1: 1 Y2: 1", Util
.getLegend());
105 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=176
106 stackedTestCase
.prototype.testDuplicatedXValue
= function() {
114 var data
= "X,Y1\n" +
118 "2,1\n" + // duplicate x-value!
122 var graph
= document
.getElementById("graph");
123 g
= new Dygraph(graph
, data
, opts
);
125 assert(g
.yAxisRange()[1] < 2);
127 assertEquals([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
128 assertEquals([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 317, 250));
131 // Validates regression when null values in stacked graphs show up
132 // incorrectly in the legend.
133 stackedTestCase
.prototype.testNullValues
= function() {
138 var data
= "X,Y1,Y2,Y3\n" +
146 var graph
= document
.getElementById("graph");
147 g
= new Dygraph(graph
, data
, opts
);
150 assertEquals("0: Y1: -5 Y2: -1 Y3: 1", Util
.getLegend());
153 assertEquals("1: Y1: 1 Y3: 1", Util
.getLegend());
156 assertEquals("2: Y1: 1 Y2: 2 Y3: 3", Util
.getLegend());
159 assertEquals("3: Y1: 3 Y3: 4", Util
.getLegend());
162 assertEquals("4: Y1: 3 Y2: 2 Y3: 3", Util
.getLegend());
165 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=438
166 stackedTestCase
.prototype.testMissingValueAtZero
= function() {
170 var data
= "X,Y1,Y2\n" +
176 var graph
= document
.getElementById("graph");
177 g
= new Dygraph(graph
, data
, opts
);
180 assertEquals("0: Y2: 1", Util
.getLegend());
183 assertEquals("1: Y1: 1 Y2: 2", Util
.getLegend());
186 assertEquals("2: Y2: 3", Util
.getLegend());
189 stackedTestCase
.prototype.testInterpolation
= function() {
191 colors
: ['#ff0000', '#00ff00', '#0000ff'],
195 // The last series is all-NaN, it ought to be treated as all zero
196 // for stacking purposes.
210 var graph
= document
.getElementById("graph");
211 g
= new Dygraph(graph
, data
, opts
);
213 var htx
= g
.hidden_ctx_
;
216 // Check that lines are drawn at the expected positions, using
217 // interpolated values for missing data.
218 CanvasAssertions
.assertLineDrawn(
219 htx
, g
.toDomCoords(100, 4), g
.toDomCoords(101, 4), {strokeStyle
: '#00ff00'});
220 CanvasAssertions
.assertLineDrawn(
221 htx
, g
.toDomCoords(102, 6), g
.toDomCoords(103, 7), {strokeStyle
: '#ff0000'});
222 CanvasAssertions
.assertLineDrawn(
223 htx
, g
.toDomCoords(107, 8), g
.toDomCoords(108, 9), {strokeStyle
: '#0000ff'});
224 CanvasAssertions
.assertLineDrawn(
225 htx
, g
.toDomCoords(108, 12), g
.toDomCoords(109, 12), {strokeStyle
: '#ff0000'});
227 // Check that the expected number of line segments gets drawn
228 // for each series. Gaps don't get a line.
229 assertEquals(7, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
230 assertEquals(4, CanvasAssertions
.numLinesDrawn(htx
, '#00ff00'));
231 assertEquals(2, CanvasAssertions
.numLinesDrawn(htx
, '#0000ff'));
233 // Check that the selection returns the original (non-stacked)
234 // values and skips gaps.
236 assertEquals("101: Y1: 1 Y2: 2 Y3: 2", Util
.getLegend());
239 assertEquals("108: Y1: 1 Y2: 2 Y3: 9", Util
.getLegend());
242 assertEquals("109: Y1: 1", Util
.getLegend());
245 stackedTestCase
.prototype.testInterpolationOptions
= function() {
247 colors
: ['#ff0000', '#00ff00', '#0000ff'],
258 var choices
= ['all', 'inside', 'none'];
264 for (var i
= 0; i
< choices
.length
; ++i
) {
265 var graph
= document
.getElementById("graph");
266 opts
['stackedGraphNaNFill'] = choices
[i
];
267 g
= new Dygraph(graph
, data
, opts
);
269 var htx
= g
.hidden_ctx_
;
272 // Check top lines get drawn at the expected positions.
273 for (var j
= 0; j
< stackedY
[i
].length
- 1; ++j
) {
274 CanvasAssertions
.assertLineDrawn(
276 g
.toDomCoords(100 + j
, stackedY
[i
][j
]),
277 g
.toDomCoords(101 + j
, stackedY
[i
][j
+ 1]),
278 {strokeStyle
: '#ff0000'});