From a2da3777b8931b37c644831dc371ccbfae9fd758 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 21 Dec 2011 16:29:22 -0500 Subject: [PATCH] Fix issue 131: setVisibility() broken with multiple axes This reworks the logic for visibility and axes. Invisible series are kept in the seriesToAxis map, but are ignored when calculating the layout and extreme values. I am slightly wary because I did not understand the comment about visibility in the deleted portion of computeYAxes_. It seemed to be describing the exact same problem as this bug, but did not really address it. Squashed commit of the following: commit 5916f4782d26e0933764607dd7cc93ebde981b2a Author: Dan Vanderkam Date: Wed Dec 21 16:26:56 2011 -0500 I do not believe so commit ae08301858225d9c9b2eb185cb45be8a3cf1387e Author: Dan Vanderkam Date: Wed Dec 21 16:25:52 2011 -0500 issue is fixed, but are there side-effects? commit 4cfbfae182d800bd15c738e879c707b283117983 Merge: c26c4fd 107c99e Author: Dan Vanderkam Date: Wed Dec 21 15:58:24 2011 -0500 Merge branch 'master' into issue131 commit c26c4fd945d1b515d2ebe7839fb5fc7a9cfa2cec Author: Dan Vanderkam Date: Wed Dec 21 15:51:03 2011 -0500 failing auto_test --- auto_tests/tests/multiple_axes.js | 34 ++++++++++++++++++++++++++++++++++ dygraph.js | 19 ++++++------------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/auto_tests/tests/multiple_axes.js b/auto_tests/tests/multiple_axes.js index 12c9c42..eb74f60 100644 --- a/auto_tests/tests/multiple_axes.js +++ b/auto_tests/tests/multiple_axes.js @@ -133,3 +133,37 @@ MultipleAxesTestCase.prototype.testMultiAxisLayout = function() { // assertTrue((child.offsetTop + child.offsetHeight) <= 350); } }; + +MultipleAxesTestCase.prototype.testTwoAxisVisibility = function() { + var data = []; + data.push([0,0,0]); + data.push([1,2,2000]); + data.push([2,4,1000]); + + g = new Dygraph( + document.getElementById("graph"), + data, + { + labels: [ 'X', 'bar', 'zot' ], + 'zot': { + axis: { + labelsKMB: true + } + } + } + ); + + assertTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0); + assertTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0); + + g.setVisibility(0, false); + + assertTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0); + assertTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0); + + g.setVisibility(0, true); + g.setVisibility(1, false); + + assertTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0); + assertTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0); +}; diff --git a/dygraph.js b/dygraph.js index d9997e9..f9d5f5e 100644 --- a/dygraph.js +++ b/dygraph.js @@ -2147,17 +2147,6 @@ Dygraph.prototype.computeYAxes_ = function() { } } - // Now we remove series from seriesToAxisMap_ which are not visible. We do - // this last so that hiding the first series doesn't destroy the axis - // properties of the primary axis. - var seriesToAxisFiltered = {}; - var vis = this.visibility(); - for (var i = 1; i < labels.length; i++) { - var s = labels[i]; - if (vis[i - 1]) seriesToAxisFiltered[s] = this.seriesToAxisMap_[s]; - } - this.seriesToAxisMap_ = seriesToAxisFiltered; - if (valueWindows != undefined) { // Restore valueWindow settings. for (var index = 0; index < valueWindows.length; index++) { @@ -2221,7 +2210,11 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { var minY = Infinity; // extremes[series[0]][0]; var maxY = -Infinity; // extremes[series[0]][1]; var extremeMinY, extremeMaxY; + for (var j = 0; j < series.length; j++) { + // this skips invisible series + if (!extremes.hasOwnProperty(series[j])) continue; + // Only use valid extremes to stop null data series' from corrupting the scale. extremeMinY = extremes[series[j]][0]; if (extremeMinY != null) { @@ -2234,9 +2227,9 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { } if (axis.includeZero && minY > 0) minY = 0; - // Ensure we have a valid scale, otherwise defualt to zero for safety. + // Ensure we have a valid scale, otherwise default to [0, 1] for safety. if (minY == Infinity) minY = 0; - if (maxY == -Infinity) maxY = 0; + if (maxY == -Infinity) maxY = 1; // Add some padding and round up to an integer to be human-friendly. var span = maxY - minY; -- 2.7.4