fix failing stackedGraph test
[dygraphs.git] / auto_tests / tests / stacked.js
1 /**
2 * @fileoverview Tests using the "stackedGraph" option.
3 *
4 * @author dan@dygraphs.com (Dan Vanderkam)
5 */
6 var stackedTestCase = TestCase("stacked");
7
8 stackedTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10 };
11
12 stackedTestCase.prototype.tearDown = function() {
13 };
14
15 stackedTestCase.prototype.testCorrectColors = function() {
16 var opts = {
17 width: 400,
18 height: 300,
19 stackedGraph: true,
20 drawXGrid: false,
21 drawYGrid: false,
22 drawXAxis: false,
23 drawYAxis: false,
24 valueRange: [0, 3],
25 colors: ['#00ff00', '#0000ff'],
26 fillAlpha: 0.15
27 };
28 var data = "X,Y1,Y2\n" +
29 "0,1,1\n" +
30 "1,1,1\n" +
31 "2,1,1\n" +
32 "3,1,1\n"
33 ;
34
35 var graph = document.getElementById("graph");
36 var g = new Dygraph(graph, data, opts);
37
38 // y pixels 299-201 = y2 = transparent blue
39 // y pixel 200 = y2 line (blue)
40 // y pixels 199-101 = y1 = transparent green
41 // y pixel 100 = y1 line (green)
42 // y pixels 0-99 = nothing (white)
43
44 var canvas = g.hidden_ctx_;
45 var imageData = canvas.getImageData(0, 0, 400, 300);
46
47 assertEquals(400, imageData.width);
48 assertEquals(300, imageData.height);
49
50 // returns an (r, g, b, alpha) tuple for the pixel.
51 // values are in [0, 255].
52 var getPixel = function(imageData, x, y) {
53 var i = 4 * (x + imageData.width * y);
54 var d = imageData.data;
55 return [d[i], d[i+1], d[i+2], d[i+3]];
56 };
57
58 // 38 = round(0.15 * 255)
59 assertEquals([0, 0, 255, 38], getPixel(imageData, 200, 250));
60 assertEquals([0, 255, 0, 38], getPixel(imageData, 200, 150));
61 };