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 // 38 = round(0.15 * 255)
51 assertEquals([0, 0, 255, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
52 assertEquals([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 150));
55 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=358
56 stackedTestCase
.prototype.testSelectionValues
= function() {
60 var data
= "X,Y1,Y2\n" +
67 var graph
= document
.getElementById("graph");
68 g
= new Dygraph(graph
, data
, opts
);
72 assertEquals("0: Y1: 1 Y2: 1", Util
.getLegend());
74 // Verify that the behavior is correct with highlightSeriesOpts as well.
76 highlightSeriesOpts
: {
81 assertEquals("0: Y1: 1 Y2: 1", Util
.getLegend());
84 assertEquals("1: Y1: 1 Y2: 1", Util
.getLegend());
86 g
.setSelection(0, 'Y2');
87 assertEquals("0: Y1: 1 Y2: 1", Util
.getLegend());
90 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=176
91 stackedTestCase
.prototype.testDuplicatedXValue
= function() {
103 "2,1\n" + // duplicate x-value!
107 var graph
= document
.getElementById("graph");
108 g
= new Dygraph(graph
, data
, opts
);
110 assert(g
.yAxisRange()[1] < 2);
112 assertEquals([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 200, 250));
113 assertEquals([0, 255, 0, 38], Util
.samplePixel(g
.hidden_
, 317, 250));
116 // Validates regression when null values in stacked graphs show up
117 // incorrectly in the legend.
118 stackedTestCase
.prototype.testNullValues
= function() {
123 var data
= "X,Y1,Y2,Y3\n" +
131 var graph
= document
.getElementById("graph");
132 g
= new Dygraph(graph
, data
, opts
);
135 assertEquals("0: Y1: -5 Y2: -1 Y3: 1", Util
.getLegend());
138 assertEquals("1: Y1: 1 Y3: 1", Util
.getLegend());
141 assertEquals("2: Y1: 1 Y2: 2 Y3: 3", Util
.getLegend());
144 assertEquals("3: Y1: 3 Y3: 4", Util
.getLegend());
147 assertEquals("4: Y1: 3 Y2: 2 Y3: 3", Util
.getLegend());
150 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=438
151 stackedTestCase
.prototype.testMissingValueAtZero
= function() {
155 var data
= "X,Y1,Y2\n" +
161 var graph
= document
.getElementById("graph");
162 g
= new Dygraph(graph
, data
, opts
);
165 assertEquals("0: Y2: 1", Util
.getLegend());
168 assertEquals("1: Y1: 1 Y2: 2", Util
.getLegend());
171 assertEquals("2: Y2: 3", Util
.getLegend());
174 stackedTestCase
.prototype.testInterpolation
= function() {
176 colors
: ['#ff0000', '#00ff00', '#0000ff'],
180 // The last series is all-NaN, it ought to be treated as all zero
181 // for stacking purposes.
195 var graph
= document
.getElementById("graph");
196 g
= new Dygraph(graph
, data
, opts
);
198 var htx
= g
.hidden_ctx_
;
201 // Check that lines are drawn at the expected positions, using
202 // interpolated values for missing data.
203 CanvasAssertions
.assertLineDrawn(
204 htx
, g
.toDomCoords(100, 4), g
.toDomCoords(101, 4), {strokeStyle
: '#00ff00'});
205 CanvasAssertions
.assertLineDrawn(
206 htx
, g
.toDomCoords(102, 6), g
.toDomCoords(103, 7), {strokeStyle
: '#ff0000'});
207 CanvasAssertions
.assertLineDrawn(
208 htx
, g
.toDomCoords(107, 8), g
.toDomCoords(108, 9), {strokeStyle
: '#0000ff'});
209 CanvasAssertions
.assertLineDrawn(
210 htx
, g
.toDomCoords(108, 12), g
.toDomCoords(109, 12), {strokeStyle
: '#ff0000'});
212 // Check that the expected number of line segments gets drawn
213 // for each series. Gaps don't get a line.
214 assertEquals(7, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
215 assertEquals(4, CanvasAssertions
.numLinesDrawn(htx
, '#00ff00'));
216 assertEquals(2, CanvasAssertions
.numLinesDrawn(htx
, '#0000ff'));
218 // Check that the selection returns the original (non-stacked)
219 // values and skips gaps.
221 assertEquals("101: Y1: 1 Y2: 2 Y3: 2", Util
.getLegend());
224 assertEquals("108: Y1: 1 Y2: 2 Y3: 9", Util
.getLegend());
227 assertEquals("109: Y1: 1", Util
.getLegend());
230 stackedTestCase
.prototype.testInterpolationOptions
= function() {
232 colors
: ['#ff0000', '#00ff00', '#0000ff'],
243 var choices
= ['all', 'inside', 'none'];
249 for (var i
= 0; i
< choices
.length
; ++i
) {
250 var graph
= document
.getElementById("graph");
251 opts
['stackedGraphNaNFill'] = choices
[i
];
252 g
= new Dygraph(graph
, data
, opts
);
254 var htx
= g
.hidden_ctx_
;
257 // Check top lines get drawn at the expected positions.
258 for (var j
= 0; j
< stackedY
[i
].length
- 1; ++j
) {
259 CanvasAssertions
.assertLineDrawn(
261 g
.toDomCoords(100 + j
, stackedY
[i
][j
]),
262 g
.toDomCoords(101 + j
, stackedY
[i
][j
+ 1]),
263 {strokeStyle
: '#ff0000'});