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