X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;ds=sidebyside;f=auto_tests%2Ftests%2Ferror_bars.js;h=e04acde7c510232484a1bd5dc38e3bdc0711ae2e;hb=a02a53659ac6e1a81fd1cad0c404104a17cd653b;hp=1e9d0f4526ff6a3ef8c0e4ca3ea4b9eb1fe10b29;hpb=ba57e1bb6097fe00538c146fea845d8931f819f0;p=dygraphs.git diff --git a/auto_tests/tests/error_bars.js b/auto_tests/tests/error_bars.js index 1e9d0f4..e04acde 100644 --- a/auto_tests/tests/error_bars.js +++ b/auto_tests/tests/error_bars.js @@ -3,34 +3,41 @@ * * @author danvk@google.com (Dan Vanderkam) */ -var errorBarsTestCase = TestCase("error-bars"); +describe("error-bars", function() { -errorBarsTestCase.prototype.setUp = function() { +beforeEach(function() { document.body.innerHTML = "
"; -}; +}); -errorBarsTestCase._origFunc = Dygraph.getContext; -errorBarsTestCase.prototype.setUp = function() { +var _origFunc = Dygraph.getContext; +beforeEach(function() { document.body.innerHTML = ""; Dygraph.getContext = function(canvas) { - return new Proxy(errorBarsTestCase._origFunc(canvas)); + return new Proxy(_origFunc(canvas)); } -}; +}); -errorBarsTestCase.prototype.tearDown = function() { - Dygraph.getContext = errorBarsTestCase._origFunc; -}; +afterEach(function() { + Dygraph.getContext = _origFunc; +}); -errorBarsTestCase.prototype.testErrorBarsDrawn = function() { +it('testErrorBarsDrawn', function() { var opts = { width: 480, height: 320, - drawXGrid: false, - drawYGrid: false, - drawXAxis: false, - drawYAxis: false, + axes : { + x : { + drawGrid: false, + drawAxis: false, + }, + y : { + drawGrid: false, + drawAxis: false, + } + }, customBars: true, - errorBars: true + errorBars: true, + labels: ['X', 'Y'] }; var data = [ [1, [10, 10, 100]], @@ -47,7 +54,7 @@ errorBarsTestCase.prototype.testErrorBarsDrawn = function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - htx = g.hidden_ctx_; + var htx = g.hidden_ctx_; var attrs = {}; // TODO(danvk): fill in @@ -88,9 +95,9 @@ errorBarsTestCase.prototype.testErrorBarsDrawn = function() { } g.destroy(); // Restore balanced saves and restores. CanvasAssertions.assertBalancedSaveRestore(htx); -}; +}); -errorBarsTestCase.prototype.testErrorBarsCorrectColors = function() { +it('testErrorBarsCorrectColors', function() { // Two constant series with constant error. var data = [ [0, [100, 50], [200, 50]], @@ -102,10 +109,16 @@ errorBarsTestCase.prototype.testErrorBarsCorrectColors = function() { sigma: 1.0, fillAlpha: 0.15, colors: ['#00ff00', '#0000ff'], - drawXGrid: false, - drawYGrid: false, - drawXAxis: false, - drawYAxis: false, + axes : { + x : { + drawGrid: false, + drawAxis: false, + }, + y : { + drawGrid: false, + drawAxis: false, + } + }, width: 400, height: 300, valueRange: [0, 300], @@ -125,18 +138,65 @@ errorBarsTestCase.prototype.testErrorBarsCorrectColors = function() { // 249-299: empty (white) // TODO(danvk): test the edges of these regions. - assertEquals([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 75)); - assertEquals([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 125)); - assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 175)); - assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 225)); -}; + assert.deepEqual([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 75)); + assert.deepEqual([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 125)); + assert.deepEqual([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 175)); + assert.deepEqual([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 225)); +}); + +// Regression test for https://github.com/danvk/dygraphs/issues/517 +// This verifies that the error bars have alpha=fillAlpha, even if the series +// color has its own alpha value. +it('testErrorBarsForAlphaSeriesCorrectColors', function() { + var data = [ + [0, [100, 50]], + [2, [100, 50]] + ]; + + var opts = { + errorBars: true, + sigma: 1.0, + fillAlpha: 0.15, + strokeWidth: 10, + colors: ['rgba(0, 255, 0, 0.5)'], + axes : { + x : { + drawGrid: false, + drawAxis: false, + }, + y : { + drawGrid: false, + drawAxis: false, + } + }, + width: 400, + height: 300, + valueRange: [0, 300], + labels: ['X', 'Y'] + }; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + // y-pixels (0=top, 299=bottom) + // 0-148: empty (white) + // 149-198: Y error bar + // 199: Y center line + // 200-248: Y error bar + // 249-299: empty (white) + + // 38 = 255 * 0.15 (fillAlpha) + // 146 = 255 * (0.15 * 0.5 + 1 * 0.5) (fillAlpha from error bar + alpha from series line) + assert.deepEqual([0, 255, 0, 38], Util.samplePixel(g.hidden_, 1, 175)); + assert.deepEqual([0, 255, 0, 146], Util.samplePixel(g.hidden_, 200, 199)); + assert.deepEqual([0, 255, 0, 38], Util.samplePixel(g.hidden_, 1, 225)); +}); // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=392 -errorBarsTestCase.prototype.testRollingAveragePreservesNaNs = function() { +it('testRollingAveragePreservesNaNs', function() { var graph = document.getElementById("graph"); - var g = new Dygraph(graph, - [ + var data = + [ [1, [null, null], [3,1]], [2, [2, 1], [null, null]], [3, [null, null], [5,1]], @@ -145,8 +205,9 @@ errorBarsTestCase.prototype.testRollingAveragePreservesNaNs = function() { [6, [NaN, NaN], [null, null]], [8, [8, 1], [null, null]], [10, [10, 1], [null, null]] - ] - , { + ]; + var g = new Dygraph(graph, data, + { labels: ['x', 'A', 'B' ], connectSeparatedPoints: true, drawPoints: true, @@ -154,26 +215,22 @@ errorBarsTestCase.prototype.testRollingAveragePreservesNaNs = function() { } ); - var in_series = [ - [1, [null, null]], - [2, [2, 1]], - [3, [null, null]], - [4, [4, 0.5]], - [5, [null, null]], - [6, [NaN, NaN]], - [8, [8, 1]], - [10, [10, 1]] - ]; - assertEquals(null, in_series[4][1][0]); - assertEquals(null, in_series[4][1][1]); - assertNaN(in_series[5][1][0]); - assertNaN(in_series[5][1][1]); - - var out_series = g.rollingAverage(in_series, 1); - assertNaN(out_series[5][1][0]); - assertNaN(out_series[5][1][1]); - assertNaN(out_series[5][1][2]); - assertEquals(null, out_series[4][1][0]); - assertEquals(null, out_series[4][1][1]); - assertEquals(null, out_series[4][1][1]); -}; + var in_series = g.dataHandler_.extractSeries(data, 1, g.attributes_); + + assert.equal(null, in_series[4][1]); + assert.equal(null, in_series[4][2][0]); + assert.equal(null, in_series[4][2][1]); + assert(isNaN(in_series[5][1])); + assert(isNaN(in_series[5][2][0])); + assert(isNaN(in_series[5][2][1])); + + var out_series = g.dataHandler_.rollingAverage(in_series, 1, g.attributes_); + assert(isNaN(out_series[5][1])); + assert(isNaN(out_series[5][2][0])); + assert(isNaN(out_series[5][2][1])); + assert.equal(null, out_series[4][1]); + assert.equal(null, out_series[4][2][0]); + assert.equal(null, out_series[4][2][1]); +}); + +});