Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / error_bars.js
CommitLineData
f35016e8
DV
1/**
2 * @fileoverview FILL THIS IN
3 *
4 * @author danvk@google.com (Dan Vanderkam)
5 */
89fdcedb 6describe("error-bars", function() {
f35016e8 7
89fdcedb 8beforeEach(function() {
f35016e8 9 document.body.innerHTML = "<div id='graph'></div>";
89fdcedb 10});
f35016e8 11
319d0361 12var _origFunc = Dygraph.getContext;
89fdcedb 13beforeEach(function() {
f35016e8
DV
14 document.body.innerHTML = "<div id='graph'></div>";
15 Dygraph.getContext = function(canvas) {
319d0361 16 return new Proxy(_origFunc(canvas));
f35016e8 17 }
89fdcedb 18});
f35016e8 19
89fdcedb 20afterEach(function() {
319d0361 21 Dygraph.getContext = _origFunc;
89fdcedb 22});
f35016e8 23
89fdcedb 24it('testErrorBarsDrawn', function() {
f35016e8
DV
25 var opts = {
26 width: 480,
27 height: 320,
bfb3e0a4
RK
28 axes : {
29 x : {
30 drawGrid: false,
31 drawAxis: false,
32 },
33 y : {
34 drawGrid: false,
35 drawAxis: false,
36 }
37 },
f35016e8 38 customBars: true,
65129ba8
DV
39 errorBars: true,
40 labels: ['X', 'Y']
f35016e8
DV
41 };
42 var data = [
43 [1, [10, 10, 100]],
44 [2, [15, 20, 110]],
45 [3, [10, 30, 100]],
46 [4, [15, 40, 110]],
47 [5, [10, 120, 100]],
48 [6, [15, 50, 110]],
49 [7, [10, 70, 100]],
50 [8, [15, 90, 110]],
51 [9, [10, 50, 100]]
52 ];
53
54 var graph = document.getElementById("graph");
55 var g = new Dygraph(graph, data, opts);
56
89fdcedb 57 var htx = g.hidden_ctx_;
f35016e8
DV
58
59 var attrs = {}; // TODO(danvk): fill in
60
61 for (var i = 0; i < data.length - 1; i++) {
62 // bottom line
63 var xy1 = g.toDomCoords(data[i][0], data[i][1][0]);
64 var xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][0]);
65 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);
66
67 // top line
68 xy1 = g.toDomCoords(data[i][0], data[i][1][2]);
69 xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][2]);
70 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);
71
72 // middle line
73 xy1 = g.toDomCoords(data[i][0], data[i][1][1]);
74 xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][1]);
75 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);
76 }
77
78 g.updateOptions({logscale: true});
79
80 for (var i = 0; i < data.length - 1; i++) {
81 // bottom line
82 var xy1 = g.toDomCoords(data[i][0], data[i][1][0]);
83 var xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][0]);
84 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);
85
86 // top line
87 xy1 = g.toDomCoords(data[i][0], data[i][1][2]);
88 xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][2]);
89 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);
90
91 // middle line
92 xy1 = g.toDomCoords(data[i][0], data[i][1][1]);
93 xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][1]);
94 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);
95 }
b941564d 96 g.destroy(); // Restore balanced saves and restores.
6278f6fe 97 CanvasAssertions.assertBalancedSaveRestore(htx);
89fdcedb 98});
f35016e8 99
89fdcedb 100it('testErrorBarsCorrectColors', function() {
debdb88d
DV
101 // Two constant series with constant error.
102 var data = [
103 [0, [100, 50], [200, 50]],
104 [1, [100, 50], [200, 50]]
105 ];
106
107 var opts = {
108 errorBars: true,
109 sigma: 1.0,
110 fillAlpha: 0.15,
111 colors: ['#00ff00', '#0000ff'],
bfb3e0a4
RK
112 axes : {
113 x : {
114 drawGrid: false,
115 drawAxis: false,
116 },
117 y : {
118 drawGrid: false,
119 drawAxis: false,
120 }
121 },
debdb88d
DV
122 width: 400,
123 height: 300,
124 valueRange: [0, 300],
125 labels: ['X', 'Y1', 'Y2']
126 };
127 var graph = document.getElementById("graph");
128 var g = new Dygraph(graph, data, opts);
129
130 // y-pixels (0=top, 299=bottom)
131 // 0- 48: empty (white)
132 // 49- 98: Y2 error bar
133 // 99: Y2 center line
134 // 100-148: Y2 error bar
135 // 149-198: Y1 error bar
136 // 199: Y1 center line
137 // 200-248: Y1 error bar
138 // 249-299: empty (white)
139 // TODO(danvk): test the edges of these regions.
140
89fdcedb
DV
141 assert.deepEqual([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 75));
142 assert.deepEqual([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 125));
143 assert.deepEqual([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 175));
144 assert.deepEqual([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 225));
145});
7522b21b
DV
146
147
148// Regression test for http://code.google.com/p/dygraphs/issues/detail?id=392
89fdcedb 149it('testRollingAveragePreservesNaNs', function() {
7522b21b 150 var graph = document.getElementById("graph");
a49c164a
DE
151 var data =
152 [
7522b21b
DV
153 [1, [null, null], [3,1]],
154 [2, [2, 1], [null, null]],
155 [3, [null, null], [5,1]],
156 [4, [4, 0.5], [null, null]],
157 [5, [null, null], [7,1]],
158 [6, [NaN, NaN], [null, null]],
159 [8, [8, 1], [null, null]],
160 [10, [10, 1], [null, null]]
a49c164a
DE
161 ];
162 var g = new Dygraph(graph, data,
163 {
7522b21b
DV
164 labels: ['x', 'A', 'B' ],
165 connectSeparatedPoints: true,
166 drawPoints: true,
167 errorBars: true
168 }
169 );
170
a49c164a
DE
171 var in_series = g.dataHandler_.extractSeries(data, 1, g.attributes_);
172
89fdcedb
DV
173 assert.equal(null, in_series[4][1]);
174 assert.equal(null, in_series[4][2][0]);
175 assert.equal(null, in_series[4][2][1]);
dc910fce
DV
176 assert(isNaN(in_series[5][1]));
177 assert(isNaN(in_series[5][2][0]));
178 assert(isNaN(in_series[5][2][1]));
a49c164a
DE
179
180 var out_series = g.dataHandler_.rollingAverage(in_series, 1, g.attributes_);
dc910fce
DV
181 assert(isNaN(out_series[5][1]));
182 assert(isNaN(out_series[5][2][0]));
183 assert(isNaN(out_series[5][2][1]));
89fdcedb
DV
184 assert.equal(null, out_series[4][1]);
185 assert.equal(null, out_series[4][2][0]);
186 assert.equal(null, out_series[4][2][1]);
187});
188
189});