From 907121a86d47058a4149671ea8b8ddcc5db23c4e Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Mon, 25 Feb 2013 00:31:19 +0400 Subject: [PATCH] Fix wrong values in legend when using stackedGraph with missing values Consider all the series when searching previous stacked one and use Dygraph.isValidPoint to validate the point (not just !yval) See http://jsfiddle.net/6W7EQ/2/ --- auto_tests/tests/stacked.js | 34 ++++++++++++++++++++++++++++++++++ dygraph-layout.js | 20 ++++++++++---------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/auto_tests/tests/stacked.js b/auto_tests/tests/stacked.js index d9aadbf..b0ad492 100644 --- a/auto_tests/tests/stacked.js +++ b/auto_tests/tests/stacked.js @@ -121,3 +121,37 @@ stackedTestCase.prototype.testDuplicatedXValue = function() { assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 250)); assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 317, 250)); } + +// Validates regression when null values in stacked graphs show up +// incorrectly in the legend. +stackedTestCase.prototype.testNullValues = function() { + var opts = { + stackedGraph: true, + stepPlot:true + }; + var data = "X,Y1,Y2,Y3\n" + + "0,-5,-1,1\n" + + "1,1,,1\n" + + "2,1,2,3\n" + + "3,3,,4\n" + + "4,3,2,3\n" + ; + + var graph = document.getElementById("graph"); + g = new Dygraph(graph, data, opts); + + g.setSelection(0); + assertEquals("0: Y1: -5 Y2: -1 Y3: 1", Util.getLegend()); + + g.setSelection(1); + assertEquals("1: Y1: 1 Y3: 1", Util.getLegend()); + + g.setSelection(2); + assertEquals("2: Y1: 1 Y2: 2 Y3: 3", Util.getLegend()); + + g.setSelection(3); + assertEquals("3: Y1: 3 Y3: 4", Util.getLegend()); + + g.setSelection(4); + assertEquals("4: Y1: 3 Y2: 2 Y3: 3", Util.getLegend()); +}; diff --git a/dygraph-layout.js b/dygraph-layout.js index e30c7a1..4caafc0 100644 --- a/dygraph-layout.js +++ b/dygraph-layout.js @@ -381,7 +381,7 @@ DygraphLayout.prototype.removeAllDatasets = function() { DygraphLayout.prototype.unstackPointAtIndex = function(setIdx, row) { var point = this.points[setIdx][row]; // If the point is missing, no unstacking is necessary - if (!point.yval) { + if (!Dygraph.isValidPoint(point)) { return point; } @@ -397,15 +397,15 @@ DygraphLayout.prototype.unstackPointAtIndex = function(setIdx, row) { // The unstacked yval is equal to the current yval minus the yval of the // next point at the same xval. - if (setIdx == this.points.length - 1) { - // We're the last series, so no unstacking is necessary. - return unstackedPoint; - } - - var points = this.points[setIdx + 1]; - if (points[row].xval == point.xval && // should always be true? - points[row].yval) { - unstackedPoint.yval -= points[row].yval; + // We need to iterate over setIdx just in case some series have invalid values + // at current row + for(setIdx++; setIdx < this.points.length; setIdx++) { + var nextPoint = this.points[setIdx][row]; + if (nextPoint.xval == point.xval && // should always be true? + Dygraph.isValidPoint(nextPoint)) { + unstackedPoint.yval -= nextPoint.yval; + break; // stop at first valid point + } } return unstackedPoint; -- 2.7.4