From: Dan Vanderkam Date: Fri, 18 Oct 2013 02:57:51 +0000 (-0400) Subject: remaining changes from dygraph-utils.js X-Git-Tag: v1.1.0~75^2~4^2~1 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=55deb02f27185eb75dc110eb04a3fdbae958409b;p=dygraphs.git remaining changes from dygraph-utils.js --- diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 03a5b4e..6bf5fbc 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -59,7 +59,7 @@ var DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) { // --- check whether everything is ok before we return // NOTE(konigsberg): isIE is never defined in this object. Bug of some sort. - if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) + if (!this.isIE && !(Dygraph.isCanvasSupported(this.element))) throw "Canvas is not supported."; // internal state @@ -118,30 +118,6 @@ DygraphCanvasRenderer.prototype.clear = function() { }; /** - * Checks whether the browser supports the <canvas> tag. - * @private - */ -DygraphCanvasRenderer.isSupported = function(canvasName) { - var canvas = null; - try { - if (typeof(canvasName) == 'undefined' || canvasName === null) { - canvas = document.createElement("canvas"); - } else { - canvas = canvasName; - } - canvas.getContext("2d"); - } - catch (e) { - var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); - var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); - if ((!ie) || (ie[1] < 6) || (opera)) - return false; - return true; - } - return true; -}; - -/** * This method is responsible for drawing everything on the chart, including * lines, error bars, fills and axes. * It is called immediately after clear() on every frame, including during pans diff --git a/dygraph-utils.js b/dygraph-utils.js index 4e259a1..0ca6585 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -408,18 +408,18 @@ Dygraph.isOK = function(x) { }; /** - * @param { {x:?number,y:?number,yval:?number} } p The point to consider, valid + * @param {{x:?number,y:?number,yval:?number}} p The point to consider, valid * points are {x, y} objects - * @param { boolean } allowNaNY Treat point with y=NaN as valid - * @return { boolean } Whether the point has numeric x and y. + * @param {boolean=} opt_allowNaNY Treat point with y=NaN as valid + * @return {boolean} Whether the point has numeric x and y. * @private */ -Dygraph.isValidPoint = function(p, allowNaNY) { +Dygraph.isValidPoint = function(p, opt_allowNaNY) { if (!p) return false; // null or undefined object if (p.yval === null) return false; // missing point if (p.x === null || p.x === undefined) return false; if (p.y === null || p.y === undefined) return false; - if (isNaN(p.x) || (!allowNaNY && isNaN(p.y))) return false; + if (isNaN(p.x) || (!opt_allowNaNY && isNaN(p.y))) return false; return true; }; @@ -706,7 +706,7 @@ Dygraph.updateDeep = function (self, o) { }; /** - * @param {Object} o + * @param {*} o * @return {boolean} * @private */ @@ -1211,3 +1211,56 @@ Dygraph.toRGB_ = function(colorStr) { b: parseInt(bits[3], 10) }; }; + +/** + * Checks whether the browser supports the <canvas> tag. + * @param {HTMLCanvasElement=} opt_canvasElement Pass a canvas element as an + * optimization if you have one. + * @return {boolean} Whether the browser supports canvas. + */ +Dygraph.isCanvasSupported = function(opt_canvasElement) { + var canvas; + try { + canvas = opt_canvasElement || document.createElement("canvas"); + canvas.getContext("2d"); + } + catch (e) { + var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); + var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); + if ((!ie) || (ie[1] < 6) || (opera)) + return false; + return true; + } + return true; +}; + +/** + * Parses the value as a floating point number. This is like the parseFloat() + * built-in, but with a few differences: + * - the empty string is parsed as null, rather than NaN. + * - if the string cannot be parsed at all, an error is logged. + * If the string can't be parsed, this method returns null. + * @param {string} x The string to be parsed + * @param {number=} opt_line_no The line number from which the string comes. + * @param {string=} opt_line The text of the line from which the string comes. + */ +Dygraph.parseFloat_ = function(x, opt_line_no, opt_line) { + var val = parseFloat(x); + if (!isNaN(val)) return val; + + // Try to figure out what happeend. + // If the value is the empty string, parse it as null. + if (/^ *$/.test(x)) return null; + + // If it was actually "NaN", return it as NaN. + if (/^ *nan *$/i.test(x)) return NaN; + + // Looks like a parsing error. + var msg = "Unable to parse '" + x + "' as a number"; + if (opt_line !== undefined && opt_line_no !== undefined) { + msg += " on line " + (1+(opt_line_no||0)) + " ('" + opt_line + "') of CSV."; + } + Dygraph.error(msg); + + return null; +}; diff --git a/dygraph.js b/dygraph.js index 7874005..a95b91d 100644 --- a/dygraph.js +++ b/dygraph.js @@ -2903,40 +2903,6 @@ Dygraph.prototype.setXAxisOptions_ = function(isDate) { }; /** - * Parses the value as a floating point number. This is like the parseFloat() - * built-in, but with a few differences: - * - the empty string is parsed as null, rather than NaN. - * - if the string cannot be parsed at all, an error is logged. - * If the string can't be parsed, this method returns null. - * @param {String} x The string to be parsed - * @param {Number} opt_line_no The line number from which the string comes. - * @param {String} opt_line The text of the line from which the string comes. - * @private - */ - -// 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 val = parseFloat(x); - if (!isNaN(val)) return val; - - // Try to figure out what happeend. - // If the value is the empty string, parse it as null. - if (/^ *$/.test(x)) return null; - - // If it was actually "NaN", return it as NaN. - if (/^ *nan *$/i.test(x)) return NaN; - - // Looks like a parsing error. - var msg = "Unable to parse '" + x + "' as a number"; - if (opt_line !== null && opt_line_no !== null) { - msg += " on line " + (1+opt_line_no) + " ('" + opt_line + "') of CSV."; - } - Dygraph.error(msg); - - return null; -}; - -/** * @private * Parses a string in a special csv format. We expect a csv file where each * line is a date point, and the first field in each line is the date string. @@ -3005,8 +2971,8 @@ Dygraph.prototype.parseCSV_ = function(data) { (1 + i) + " ('" + line + "') which is not of this form."); fields[j] = [0, 0]; } else { - fields[j] = [this.parseFloat_(vals[0], i, line), - this.parseFloat_(vals[1], i, line)]; + fields[j] = [Dygraph.parseFloat_(vals[0], i, line), + Dygraph.parseFloat_(vals[1], i, line)]; } } } else if (this.getBooleanOption("errorBars")) { @@ -3017,8 +2983,8 @@ Dygraph.prototype.parseCSV_ = function(data) { (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] = [Dygraph.parseFloat_(inFields[j], i, line), + Dygraph.parseFloat_(inFields[j + 1], i, line)]; } } else if (this.getBooleanOption("customBars")) { // Bars are a low;center;high tuple @@ -3029,9 +2995,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] = [ Dygraph.parseFloat_(vals[0], i, line), + Dygraph.parseFloat_(vals[1], i, line), + Dygraph.parseFloat_(vals[2], i, line) ]; } else { Dygraph.warn('When using customBars, values must be either blank ' + 'or "low;center;high" tuples (got "' + val + @@ -3042,7 +3008,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] = Dygraph.parseFloat_(inFields[j], i, line); } } if (ret.length > 0 && fields[0] < ret[ret.length - 1][0]) {