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