From d574a45ea4f06abe9b65be39a2ae4991f59257f3 Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Sat, 29 Dec 2012 21:00:42 -0500 Subject: [PATCH] Options now include defaults. Fix https://code.google.com/p/dygraphs/issues/detail?id=413 --- auto_tests/tests/Util.js | 30 +++++++++++++++++++++++++---- auto_tests/tests/axis_labels.js | 21 +++++++++++++++++++++ dygraph-options.js | 42 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 7fca030..1fce3a0 100644 --- a/auto_tests/tests/Util.js +++ b/auto_tests/tests/Util.js @@ -19,7 +19,7 @@ Util.getYLabels = function(axis_num, parent) { ary.push(y_labels[i].innerHTML); } return ary; -} +}; /** * Get the x-labels for a given axis. @@ -34,7 +34,7 @@ Util.getXLabels = function(parent) { ary.push(x_labels[i].innerHTML); } return ary; -} +}; /** * Returns all text in tags w/ a given css class, sorted. @@ -49,11 +49,33 @@ Util.getClassTexts = function(css_class, parent) { } texts.sort(); return texts; -} +}; Util.getLegend = function(parent) { parent = parent || document; var legend = parent.getElementsByClassName("dygraph-legend")[0]; return legend.textContent; -} +}; +/** + * Assert that all the elements in 'parent' with class 'className' is + * the expected font size. + */ +Util.assertFontSizes = function(parent, className, expectedSize) { + var expectedSizePx = expectedSize + "px"; + var labels = parent.getElementsByClassName(className); + assertTrue(labels.length > 0); + + // window.getComputedStyle is apparently compatible with all browsers + // (IE first became compatible with IE9.) + // If this test fails on earlier browsers, then enable something like this, + // because the font size is set by the parent div. + // if (!window.getComputedStyle) { + // fontSize = label.parentElement.style.fontSize; + // } + for (var idx = 0; idx < labels.length; idx++) { + var label = labels[idx]; + var fontSize = window.getComputedStyle(label).fontSize; + assertEquals(expectedSizePx, fontSize); + } +}; diff --git a/auto_tests/tests/axis_labels.js b/auto_tests/tests/axis_labels.js index d4812ea..00f3913 100644 --- a/auto_tests/tests/axis_labels.js +++ b/auto_tests/tests/axis_labels.js @@ -12,6 +12,13 @@ AxisLabelsTestCase.prototype.setUp = function() { AxisLabelsTestCase.prototype.tearDown = function() { }; +AxisLabelsTestCase.simpleData = + "X,Y,Y2\n" + + "0,-1,0.25\n" + + "1,0,0.5\n" + + "2,1,0.9\n" + + "3,0,0.7\n"; + /** * Takes in an array of strings and returns an array of floats. */ @@ -569,3 +576,17 @@ AxisLabelsTestCase.prototype.testIncludeZero = function() { g.updateOptions({ includeZero : false }); assertEquals(['500','600','700','800','900','1000'], Util.getYLabels()); } + +AxisLabelsTestCase.prototype.testAxisLabelFontSizeNull = function() { + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, AxisLabelsTestCase.simpleData, + { + axisLabelFontSize: null + }); + + // Be sure we're dealing with a 14-point default. + assertEquals(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); + + Util.assertFontSizes(graph, "dygraph-axis-label-x", 14); + Util.assertFontSizes(graph, "dygraph-axis-label-y", 14); +} diff --git a/dygraph-options.js b/dygraph-options.js index 815a893..f059ff9 100644 --- a/dygraph-options.js +++ b/dygraph-options.js @@ -193,14 +193,29 @@ DygraphOptions.prototype.reparseSeries = function() { * @param {String} name the name of the option. */ DygraphOptions.prototype.get = function(name) { + var result = this.getGlobalUser_(name); + if (result != null) { + return result; + } + return this.getGlobalDefault_(name); +}; + +DygraphOptions.prototype.getGlobalUser_ = function(name) { if (this.user_.hasOwnProperty(name)) { return this.user_[name]; } + return null; +}; + +DygraphOptions.prototype.getGlobalDefault_ = function(name) { if (this.global_.hasOwnProperty(name)) { return this.global_[name]; } + if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(name)) { + return Dygraph.DEFAULT_ATTRS[name]; + } return null; -}; +} /** * Get a value for a specific axis. If there is no specific value for the axis, @@ -218,12 +233,29 @@ DygraphOptions.prototype.getForAxis = function(name, axis) { // TODO(konigsberg): Accept only valid axis strings? axisIdx = (axis == "y2") ? 1 : 0; } + // Search the user-specified axis option first. + if (this.axes_[axisIdx]) { + var axisOptions = this.axes_[axisIdx].options; + if (axisOptions.hasOwnProperty(name)) { + return axisOptions[name]; + } + } - var axisOptions = this.axes_[axisIdx].options; - if (axisOptions.hasOwnProperty(name)) { - return axisOptions[name]; + // User-specified global options second. + var result = this.getGlobalUser_(name); + if (result != null) { + return result; } - return this.get(name); + + // Default axis options third. + var axisString = axis == 0 ? "y" : "y2"; + var defaultAxisOptions = Dygraph.DEFAULT_ATTRS.axes[axisString]; + if (defaultAxisOptions.hasOwnProperty(name)) { + return defaultAxisOptions[name]; + } + + // Default global options last. + return this.getGlobalDefault_(name); }; /** -- 2.7.4