From: Matthias Winkelmann Date: Wed, 2 Mar 2016 12:53:35 +0000 (+0100) Subject: Fixes nnnick/Chart.js#2086 by introducing a new time.parser option with high priority... X-Git-Tag: v2.0.0~25^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d5f837843e5b6448087fdd2d7f125ac26ddc36ab;p=thirdparty%2FChart.js.git Fixes nnnick/Chart.js#2086 by introducing a new time.parser option with high priority to replace the (deprecated) time.format --- diff --git a/docs/01-Scales.md b/docs/01-Scales.md index a743c5b4c..58a6ad8da 100644 --- a/docs/01-Scales.md +++ b/docs/01-Scales.md @@ -168,7 +168,7 @@ The time scale extends the core scale class with the following tick template: position: "bottom", time: { // string/callback - By default, date objects are expected. You may use a pattern string from http://momentjs.com/docs/#/parsing/string-format/ to parse a time string format, or use a callback function that is passed the label, and must return a moment() instance. - format: false, + parser: false, // string - By default, unit will automatically be detected. Override with 'week', 'month', 'year', etc. (see supported time measurements) unit: false, // string - By default, no rounding is applied. To round, set to a supported time unit eg. 'week', 'month', 'year', etc. diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index b885ae23b..0958edd98 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -48,7 +48,8 @@ module.exports = function(Chart) { position: "bottom", time: { - format: false, // false == date objects or use pattern string from http://momentjs.com/docs/#/parsing/string-format/ + parser: false, // false == a pattern string from http://momentjs.com/docs/#/parsing/string-format/ or a custom callback that converts its argument to a moment + format: false, // DEPRECATED false == date objects, moment object, callback or a pattern string from http://momentjs.com/docs/#/parsing/string-format/ unit: false, // false == automatic or override with week, month, year, etc. round: false, // none, or override with week, month, year, etc. displayFormat: false, // DEPRECATED @@ -293,6 +294,12 @@ module.exports = function(Chart) { } }, parseTime: function(label) { + if (typeof this.options.time.parser === 'string') { + return moment(label, this.options.time.parser) + } + if (typeof this.options.time.parser === 'function') { + return this.options.time.parser(label); + } // Date objects if (typeof label.getMonth === 'function' || typeof label === 'number') { return moment(label); @@ -303,6 +310,7 @@ module.exports = function(Chart) { } // Custom parsing (return an instance of moment) if (typeof this.options.time.format !== 'string' && this.options.time.format.call) { + console.warn("options.time.format is deprecated and replaced by options.time.parser. See http://nnnick.github.io/Chart.js/docs-v2/#scales-time-scale"); return this.options.time.format(label); } // Moment format parsing