From debdb88d1ccca3648f45d6cc5d49896f3014cfdd Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Fri, 20 Jul 2012 15:35:14 -0400 Subject: [PATCH] create failing errorBars test --- auto_tests/tests/error_bars.js | 54 ++++++++++++++++++++++++++++++++++++++++++ auto_tests/tests/stacked.js | 5 ++-- dygraph.js | 7 ++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/auto_tests/tests/error_bars.js b/auto_tests/tests/error_bars.js index c9e3a38..1ede89b 100644 --- a/auto_tests/tests/error_bars.js +++ b/auto_tests/tests/error_bars.js @@ -89,3 +89,57 @@ errorBarsTestCase.prototype.testErrorBarsDrawn = function() { CanvasAssertions.assertBalancedSaveRestore(htx); }; +errorBarsTestCase.prototype.testErrorBarsCorrectColors = function() { + // Two constant series with constant error. + var data = [ + [0, [100, 50], [200, 50]], + [1, [100, 50], [200, 50]] + ]; + + var opts = { + errorBars: true, + sigma: 1.0, + fillAlpha: 0.15, + colors: ['#00ff00', '#0000ff'], + drawXGrid: false, + drawYGrid: false, + drawXAxis: false, + drawYAxis: false, + width: 400, + height: 300, + valueRange: [0, 300], + labels: ['X', 'Y1', 'Y2'] + }; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + // y-pixels (0=top, 299=bottom) + // 0- 48: empty (white) + // 49- 98: Y2 error bar + // 99: Y2 center line + // 100-148: Y2 error bar + // 149-198: Y1 error bar + // 199: Y1 center line + // 200-248: Y1 error bar + // 249-299: empty (white) + // TODO(danvk): test the edges of these regions. + + var ctx = g.hidden_.getContext("2d"); // bypass Proxy + var imageData = ctx.getImageData(0, 0, 400, 300); + + assertEquals(400, imageData.width); + assertEquals(300, imageData.height); + + // returns an (r, g, b, alpha) tuple for the pixel. + // values are in [0, 255]. + var getPixel = function(imageData, x, y) { + var i = 4 * (x + imageData.width * y); + var d = imageData.data; + return [d[i], d[i+1], d[i+2], d[i+3]]; + }; + + assertEquals([0, 0, 255, 38], getPixel(imageData, 200, 75)); + assertEquals([0, 0, 255, 38], getPixel(imageData, 200, 125)); + assertEquals([0, 255, 0, 38], getPixel(imageData, 200, 175)); + assertEquals([0, 255, 0, 38], getPixel(imageData, 200, 225)); +} diff --git a/auto_tests/tests/stacked.js b/auto_tests/tests/stacked.js index f1abbf5..e1f2cbc 100644 --- a/auto_tests/tests/stacked.js +++ b/auto_tests/tests/stacked.js @@ -41,8 +41,9 @@ stackedTestCase.prototype.testCorrectColors = function() { // y pixel 100 = y1 line (green) // y pixels 0-99 = nothing (white) - var canvas = g.hidden_ctx_; - var imageData = canvas.getImageData(0, 0, 400, 300); + // TODO(danvk): factor this and getPixel() into a utility usable by all tests. + var ctx = g.hidden_ctx_; + var imageData = ctx.getImageData(0, 0, 400, 300); assertEquals(400, imageData.width); assertEquals(300, imageData.height); diff --git a/dygraph.js b/dygraph.js index f97494a..3a60180 100644 --- a/dygraph.js +++ b/dygraph.js @@ -3041,6 +3041,13 @@ Dygraph.prototype.parseArray_ = function(data) { for (i = 1; i < data[0].length; i++) { this.attrs_.labels.push("Y" + i); } + } else { + var num_labels = this.attr_("labels"); + if (num_labels.length != data[0].length) { + this.error("Mismatch between number of labels (" + num_labels + + ") and number of columns in array (" + data[0].length + ")"); + return null; + } } if (Dygraph.isDateLike(data[0][0])) { -- 2.7.4