From: Dan Vanderkam Date: Tue, 3 Jan 2012 23:52:24 +0000 (-0500) Subject: Only show the secondary y-axis label when there is a secondary y-axis. X-Git-Tag: v1.0.0~378 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=107f9d8e86feef0bd886393edd40944c587ac2e7;p=dygraphs.git Only show the secondary y-axis label when there is a secondary y-axis. --- diff --git a/auto_tests/tests/multiple_axes.js b/auto_tests/tests/multiple_axes.js index 2c0f18f..bf75876 100644 --- a/auto_tests/tests/multiple_axes.js +++ b/auto_tests/tests/multiple_axes.js @@ -24,6 +24,17 @@ function getLegend() { return legend.textContent; } +// returns all text in tags w/ a given css class, sorted. +function getClassTexts(css_class) { + var texts = []; + var els = document.getElementsByClassName(css_class); + for (var i = 0; i < els.length; i++) { + texts[i] = els[i].textContent; + } + texts.sort(); + return texts; +} + MultipleAxesTestCase.getData = function() { var data = []; for (var i = 1; i <= 100; i++) { @@ -45,7 +56,7 @@ MultipleAxesTestCase.getData = function() { MultipleAxesTestCase.prototype.testBasicMultipleAxes = function() { var data = MultipleAxesTestCase.getData(); - g = new Dygraph( + var g = new Dygraph( document.getElementById("graph"), data, { @@ -71,7 +82,7 @@ MultipleAxesTestCase.prototype.testBasicMultipleAxes = function() { MultipleAxesTestCase.prototype.testNewStylePerAxisOptions = function() { var data = MultipleAxesTestCase.getData(); - g = new Dygraph( + var g = new Dygraph( document.getElementById("graph"), data, { @@ -101,7 +112,7 @@ MultipleAxesTestCase.prototype.testMultiAxisLayout = function() { var el = document.getElementById("graph"); - g = new Dygraph( + var g = new Dygraph( el, data, { @@ -178,7 +189,7 @@ MultipleAxesTestCase.prototype.testMultiChartLabels = function() { el.style.marginLeft = '200px'; el.style.marginTop = '200px'; - g = new Dygraph( + var g = new Dygraph( el, data, { @@ -198,23 +209,37 @@ MultipleAxesTestCase.prototype.testMultiChartLabels = function() { } ); - // returns all text in tags w/ a given css class, sorted. - function getTexts(css_class) { - var texts = []; - var els = document.getElementsByClassName(css_class); - for (var i = 0; i < els.length; i++) { - texts[i] = els[i].textContent; - } - texts.sort(); - return texts; - } - assertEquals(["Chart title", "x-axis", "y-axis", "y2-axis"], - getTexts("dygraph-label")); - assertEquals(["Chart title"], getTexts("dygraph-title")); - assertEquals(["x-axis"], getTexts("dygraph-xlabel")); - assertEquals(["y-axis"], getTexts("dygraph-ylabel")); - assertEquals(["y2-axis"], getTexts("dygraph-y2label")); + getClassTexts("dygraph-label")); + assertEquals(["Chart title"], getClassTexts("dygraph-title")); + assertEquals(["x-axis"], getClassTexts("dygraph-xlabel")); + assertEquals(["y-axis"], getClassTexts("dygraph-ylabel")); + assertEquals(["y2-axis"], getClassTexts("dygraph-y2label")); // TODO(danvk): check relative positioning here: title on top, y left of y2. }; + +// Check that a chart w/o a secondary y-axis will not get a y2label, even if one +// is specified. +MultipleAxesTestCase.prototype.testNoY2LabelWithoutSecondaryAxis = function() { + var g = new Dygraph( + document.getElementById("graph"), + MultipleAxesTestCase.getData(), + { + labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], + width: 640, + height: 350, + xlabel: 'x-axis', + ylabel: 'y-axis', + y2label: 'y2-axis', + title: 'Chart title' + } + ); + + assertEquals(["Chart title", "x-axis", "y-axis"], + getClassTexts("dygraph-label")); + assertEquals(["Chart title"], getClassTexts("dygraph-title")); + assertEquals(["x-axis"], getClassTexts("dygraph-xlabel")); + assertEquals(["y-axis"], getClassTexts("dygraph-ylabel")); + assertEquals([], getClassTexts("dygraph-y2label")); +}; diff --git a/dygraph-canvas.js b/dygraph-canvas.js index acf011f..5e6766d 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -551,7 +551,7 @@ DygraphCanvasRenderer.prototype._renderChartLabels = function() { this.container.appendChild(div); this.chartLabels.ylabel = div; } - if (this.attr_('y2label')) { + if (this.attr_('y2label') && this.dygraph_.numAxes() == 2) { div = createRotatedDiv(2, 'dygraph-label dygraph-y2label', this.attr_('y2label')); this.container.appendChild(div); diff --git a/tests/two-axes.html b/tests/two-axes.html index bb4a34a..3f40b07 100644 --- a/tests/two-axes.html +++ b/tests/two-axes.html @@ -66,7 +66,8 @@ { labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], labelsKMB: true, - ylabel: 'Primary y-axis' + ylabel: 'Primary y-axis', + y2label: 'Secondary y-axis', } );