]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fixes nnnick/Chart.js#2086 by introducing a new time.parser option with high priority...
authorMatthias Winkelmann <matthi@matthi.coffee>
Wed, 2 Mar 2016 12:53:35 +0000 (13:53 +0100)
committerMatthias Winkelmann <matthi@matthi.coffee>
Wed, 2 Mar 2016 12:53:35 +0000 (13:53 +0100)
docs/01-Scales.md
src/scales/scale.time.js

index a743c5b4c92624a3a681fa9321fe9504d041c104..58a6ad8da4215a58339dceb8140e3fa1e6158c94 100644 (file)
@@ -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.
index b885ae23b5a8a39d1a41f2011ca36301249fa3ec..0958edd983614e055dbea9a749e7d61ff2ac3d87 100644 (file)
@@ -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