From bf19fbea68f3b0f42c764ae4dbe1806323a26fc5 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Sun, 21 Jul 2013 11:13:01 -0400 Subject: [PATCH] consolidate parseFloat_ (!) --- dygraph-layout.js | 14 -------------- dygraph-options.js | 10 +++++----- dygraph.js | 29 ++++++++++++++++------------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/dygraph-layout.js b/dygraph-layout.js index 7b1bc7a..ad83292 100644 --- a/dygraph-layout.js +++ b/dygraph-layout.js @@ -263,20 +263,6 @@ DygraphLayout.prototype._evaluateLineCharts = function() { } }; -/** - * Optimized replacement for parseFloat, which was way too slow when almost - * all values were type number, with few edge cases, none of which were strings. - */ -DygraphLayout.parseFloat_ = function(val) { - // parseFloat(null) is NaN - if (val === null) { - return NaN; - } - - // Assume it's a number or NaN. If it's something else, I'll be shocked. - return val; -}; - DygraphLayout.prototype._evaluateLineTicks = function() { var i, tick, label, pos; this.xticks = []; diff --git a/dygraph-options.js b/dygraph-options.js index 2dc88ae..e912aa7 100644 --- a/dygraph-options.js +++ b/dygraph-options.js @@ -76,7 +76,7 @@ var DygraphOptions = function(dygraph) { * @type {Object.} * @private */ -DygraphOptions.AXIS_STRING_MAPPINGS_ = { +var AXIS_STRING_MAPPINGS_ = { 'y' : 0, 'Y' : 0, 'y1' : 0, @@ -89,10 +89,10 @@ DygraphOptions.AXIS_STRING_MAPPINGS_ = { * @param {string|number} axis * @private */ -DygraphOptions.axisToIndex_ = function(axis) { +var axisToIndex_ = function(axis) { if (typeof(axis) == "string") { - if (DygraphOptions.AXIS_STRING_MAPPINGS_.hasOwnProperty(axis)) { - return DygraphOptions.AXIS_STRING_MAPPINGS_[axis]; + if (AXIS_STRING_MAPPINGS_.hasOwnProperty(axis)) { + return AXIS_STRING_MAPPINGS_[axis]; } throw "Unknown axis : " + axis; } @@ -196,7 +196,7 @@ DygraphOptions.prototype.reparseSeries = function() { for (var idx = 0; idx < this.labels_.length; idx++) { var seriesName = this.labels_[idx]; var optionsForSeries = this.user_.series[seriesName] || {}; - var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]); + var yAxis = axisToIndex_(optionsForSeries["axis"]); this.series_[seriesName] = { idx: idx, diff --git a/dygraph.js b/dygraph.js index bc5d4b6..c1be2c2 100644 --- a/dygraph.js +++ b/dygraph.js @@ -355,6 +355,9 @@ Dygraph.DEFAULT_ATTRS = { } }; +// Utility functions +var parseFloat_; + // Directions for panning and zooming. Use bit operations when combined // values are possible. var HORIZONTAL = 1; @@ -2285,11 +2288,11 @@ Dygraph.seriesToPoints_ = function(series, bars, setName, boundaryIdStart) { for (var i = 0; i < series.length; ++i) { var item = series[i]; var yraw = bars ? item[1][0] : item[1]; - var yval = yraw === null ? null : DygraphLayout.parseFloat_(yraw); + var yval = yraw === null ? null : parseFloat_(yraw); var point = { x: NaN, y: NaN, - xval: DygraphLayout.parseFloat_(item[0]), + xval: parseFloat_(item[0]), yval: yval, name: setName, // TODO(danvk): is this really necessary? idx: i + boundaryIdStart @@ -2298,8 +2301,8 @@ Dygraph.seriesToPoints_ = function(series, bars, setName, boundaryIdStart) { if (bars) { point.y_top = NaN; point.y_bottom = NaN; - point.yval_minus = DygraphLayout.parseFloat_(item[1][1]); - point.yval_plus = DygraphLayout.parseFloat_(item[1][2]); + point.yval_minus = parseFloat_(item[1][1]); + point.yval_plus = parseFloat_(item[1][2]); } points.push(point); } @@ -3119,7 +3122,7 @@ Dygraph.prototype.setXAxisOptions_ = function(isDate) { */ // Parse the x as a float or return null if it's not a number. -Dygraph.prototype.parseFloat_ = function(x, opt_line_no, opt_line) { +var parseFloat_ = function(x, opt_line_no, opt_line) { var val = parseFloat(x); if (!isNaN(val)) return val; @@ -3210,8 +3213,8 @@ Dygraph.prototype.parseCSV_ = function(data) { "form."); fields[j] = [0, 0]; } else { - fields[j] = [this.parseFloat_(vals[0], i, line), - this.parseFloat_(vals[1], i, line)]; + fields[j] = [parseFloat_(vals[0], i, line), + parseFloat_(vals[1], i, line)]; } } } else if (this.attr_("errorBars")) { @@ -3222,8 +3225,8 @@ Dygraph.prototype.parseCSV_ = function(data) { 'values (' + (inFields.length - 1) + "): '" + line + "'"); } for (j = 1; j < inFields.length; j += 2) { - fields[(j + 1) / 2] = [this.parseFloat_(inFields[j], i, line), - this.parseFloat_(inFields[j + 1], i, line)]; + fields[(j + 1) / 2] = [parseFloat_(inFields[j], i, line), + parseFloat_(inFields[j + 1], i, line)]; } } else if (this.attr_("customBars")) { // Bars are a low;center;high tuple @@ -3234,9 +3237,9 @@ Dygraph.prototype.parseCSV_ = function(data) { } else { vals = val.split(";"); if (vals.length == 3) { - fields[j] = [ this.parseFloat_(vals[0], i, line), - this.parseFloat_(vals[1], i, line), - this.parseFloat_(vals[2], i, line) ]; + fields[j] = [ parseFloat_(vals[0], i, line), + parseFloat_(vals[1], i, line), + parseFloat_(vals[2], i, line) ]; } else { Dygraph.warn('When using customBars, values must be either blank ' + 'or "low;center;high" tuples (got "' + val + @@ -3247,7 +3250,7 @@ Dygraph.prototype.parseCSV_ = function(data) { } else { // Values are just numbers for (j = 1; j < inFields.length; j++) { - fields[j] = this.parseFloat_(inFields[j], i, line); + fields[j] = parseFloat_(inFields[j], i, line); } } if (ret.length > 0 && fields[0] < ret[ret.length - 1][0]) { -- 2.7.4