From d5f837843e5b6448087fdd2d7f125ac26ddc36ab Mon Sep 17 00:00:00 2001 From: Matthias Winkelmann Date: Wed, 2 Mar 2016 13:53:35 +0100 Subject: [PATCH 1/4] Fixes nnnick/Chart.js#2086 by introducing a new time.parser option with high priority to replace the (deprecated) time.format --- docs/01-Scales.md | 2 +- src/scales/scale.time.js | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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 From b69b388b3109082d096065a4e029291d35d91c7d Mon Sep 17 00:00:00 2001 From: Matthias Winkelmann Date: Wed, 2 Mar 2016 14:16:52 +0100 Subject: [PATCH 2/4] fixed travis built --- src/scales/scale.time.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 0958edd98..03563df31 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -295,7 +295,7 @@ module.exports = function(Chart) { }, parseTime: function(label) { if (typeof this.options.time.parser === 'string') { - return moment(label, this.options.time.parser) + return moment(label, this.options.time.parser); } if (typeof this.options.time.parser === 'function') { return this.options.time.parser(label); From fc46e25f2314e9c4524e8e65698a479978711feb Mon Sep 17 00:00:00 2001 From: Matthias Winkelmann Date: Wed, 2 Mar 2016 16:40:58 +0100 Subject: [PATCH 3/4] fixed test and added a test for a custom date parser --- test/scale.time.tests.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/scale.time.tests.js b/test/scale.time.tests.js index 93b963dfb..2558f046e 100644 --- a/test/scale.time.tests.js +++ b/test/scale.time.tests.js @@ -42,6 +42,7 @@ describe('Time scale tests', function() { autoSkipPadding: 20 }, time: { + parser: false, format: false, unit: false, round: false, @@ -168,6 +169,43 @@ describe('Time scale tests', function() { expect(scale.ticks).toEqual(['Jan 1, 2015', 'Jan 3, 2015', 'Jan 5, 2015', 'Jan 7, 2015', 'Jan 9, 2015', 'Jan 11, 2015']); }); + it('should allow custom time parsers', function() { + // Helper to build date objects + + + var scaleID = 'myScale'; + var mockData = { + datasets: [{ + data: [{ + x: 375058800, + y: 1 + }], + }] + }; + var verticalScaleConfig = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time')); + verticalScaleConfig.time.unit = 'day'; + verticalScaleConfig.time.round = true; + verticalScaleConfig.time.parser = function customTimeParser(label) { + console.log("got "+label+" returning "+moment.unix(label)) + return moment.unix(label); + } + + var mockContext = window.createMockContext(); + var Constructor = Chart.scaleService.getScaleConstructor('time'); + var scale = new Constructor({ + ctx: mockContext, + options: verticalScaleConfig, + chart: { + data: mockData + }, + id: scaleID + }); + scale.update(400, 50); + + // Counts down because the lines are drawn top to bottom + expect(scale.ticks).toEqual(['Nov 20, 1981', 'Nov 20, 1981']); + }); + it('should build ticks using the config unit', function() { var scaleID = 'myScale'; From 9410eaabbfd85826c42e541d43987888f951749e Mon Sep 17 00:00:00 2001 From: Matthias Winkelmann Date: Wed, 2 Mar 2016 16:51:48 +0100 Subject: [PATCH 4/4] fixed timezone bug in test --- test/scale.time.tests.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/scale.time.tests.js b/test/scale.time.tests.js index 2558f046e..f5c3f470a 100644 --- a/test/scale.time.tests.js +++ b/test/scale.time.tests.js @@ -177,7 +177,7 @@ describe('Time scale tests', function() { var mockData = { datasets: [{ data: [{ - x: 375058800, + x: 375068900, y: 1 }], }] @@ -186,7 +186,6 @@ describe('Time scale tests', function() { verticalScaleConfig.time.unit = 'day'; verticalScaleConfig.time.round = true; verticalScaleConfig.time.parser = function customTimeParser(label) { - console.log("got "+label+" returning "+moment.unix(label)) return moment.unix(label); }