From: Dan Vanderkam Date: Thu, 16 Aug 2012 15:47:59 +0000 (-0400) Subject: Fix & regression test for Issue 358:857a6931 breaks stacked graph highlighting X-Git-Tag: v1.0.0~195 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=9b3d945989b5cfc8b4c498f7c001f93b22ad73ac;p=dygraphs.git Fix & regression test for Issue 358:857a6931 breaks stacked graph highlighting --- diff --git a/auto_tests/tests/stacked.js b/auto_tests/tests/stacked.js index e1f2cbc..f7ca442 100644 --- a/auto_tests/tests/stacked.js +++ b/auto_tests/tests/stacked.js @@ -60,3 +60,41 @@ stackedTestCase.prototype.testCorrectColors = function() { assertEquals([0, 0, 255, 38], getPixel(imageData, 200, 250)); assertEquals([0, 255, 0, 38], getPixel(imageData, 200, 150)); }; + +// Regression test for http://code.google.com/p/dygraphs/issues/detail?id=358 +stackedTestCase.prototype.testSelectionValues = function() { + var opts = { + stackedGraph: true + }; + var data = "X,Y1,Y2\n" + + "0,1,1\n" + + "1,1,1\n" + + "2,1,1\n" + + "3,1,1\n" + ; + + var graph = document.getElementById("graph"); + g = new Dygraph(graph, data, opts); + + g.setSelection(0); + + var legend = document.getElementsByClassName("dygraph-legend"); + assertEquals(1, legend.length); + legend = legend[0]; + + assertEquals("0: Y1:1 Y2:1", legend.textContent); + + // Verify that the behavior is correct with highlightSeriesOpts as well. + g.updateOptions({ + highlightSeriesOpts: { + strokeWidth: 10 + } + }); + // NOTE: calling g.setSelection(0) here makes the test fail, due to an + // unrelated bug. + g.setSelection(1); + assertEquals("1: Y1:1 Y2:1", legend.textContent); + + g.setSelection(0, 'Y2'); + assertEquals("0: Y1:1 Y2:1", legend.textContent); +}; diff --git a/dygraph-layout.js b/dygraph-layout.js index 7e08e3e..9a0ac3d 100644 --- a/dygraph-layout.js +++ b/dygraph-layout.js @@ -409,12 +409,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. - var points = this.points[setIdx]; - for (var i = row + 1; i < points.length; i++) { - if ((points[i].xval == point.xval) && points[i].yval) { - unstackedPoint.yval -= points[i].yval; - break; - } + 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; } return unstackedPoint;