]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow the user to change the time label display formats
authoretimberg <evert.timberg@gmail.com>
Tue, 1 Dec 2015 01:42:48 +0000 (20:42 -0500)
committeretimberg <evert.timberg@gmail.com>
Tue, 1 Dec 2015 01:42:48 +0000 (20:42 -0500)
src/scales/scale.time.js
test/scale.time.tests.js

index 7ab929625ef49edd2c7610b14956fe282848d221..ad8fee7d4ffbbbba43e14f43374162ef53778b3b 100644 (file)
                        'quarter',
                        'year',
                ],
-               unit: {
-                       'millisecond': {
-                               display: 'SSS [ms]', // 002 ms
-                               maxStep: 1000,
-                       },
-                       'second': {
-                               display: 'h:mm:ss a', // 11:20:01 AM
-                               maxStep: 60,
-                       },
-                       'minute': {
-                               display: 'h:mm:ss a', // 11:20:01 AM
-                               maxStep: 60,
-                       },
-                       'hour': {
-                               display: 'MMM D, hA', // Sept 4, 5PM
-                               maxStep: 24,
-                       },
-                       'day': {
-                               display: 'll', // Sep 4 2015
-                               maxStep: 7,
-                       },
-                       'week': {
-                               display: 'll', // Week 46, or maybe "[W]WW - YYYY" ?
-                               maxStep: 4.3333,
-                       },
-                       'month': {
-                               display: 'MMM YYYY', // Sept 2015
-                               maxStep: 12,
-                       },
-                       'quarter': {
-                               display: '[Q]Q - YYYY', // Q3
-                               maxStep: 4,
-                       },
-                       'year': {
-                               display: 'YYYY', // 2015
-                               maxStep: false,
-                       },
-               }
        };
 
        var defaultConfig = {
                        format: false, // false == date objects or use 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, // defaults to unit's corresponding unitFormat below or override using pattern string from http://momentjs.com/docs/#/displaying/format/
+                       displayFormat: false, // DEPRECATED
+
+                       // defaults to unit's corresponding unitFormat below or override using pattern string from http://momentjs.com/docs/#/displaying/format/
+                       displayFormats: {
+                               'millisecond': 'SSS [ms]',
+                               'second': 'h:mm:ss a', // 11:20:01 AM
+                               'minute': 'h:mm:ss a', // 11:20:01 AM
+                               'hour': 'MMM D, hA', // Sept 4, 5PM
+                               'day': 'll', // Sep 4 2015
+                               'week': 'll', // Week 46, or maybe "[W]WW - YYYY" ?
+                               'month': 'MMM YYYY', // Sept 2015
+                               'quarter': '[Q]Q - YYYY', // Q3
+                               'year': 'YYYY', // 2015
+                       }, 
                },
        };
 
                        // Set unit override if applicable
                        if (this.options.time.unit) {
                                this.tickUnit = this.options.time.unit || 'day';
-                               this.displayFormat = time.unit[this.tickUnit].display;
+                               this.displayFormat = this.options.time.displayFormats[this.tickUnit];
                                this.tickRange = Math.ceil(this.lastTick.diff(this.firstTick, this.tickUnit, true));
                        } else {
                                // Determine the smallest needed unit of the time
                                // Start as small as possible
                                this.tickUnit = 'millisecond';
                                this.tickRange = Math.ceil(this.lastTick.diff(this.firstTick, this.tickUnit, true) + buffer);
-                               this.displayFormat = time.unit[this.tickUnit].display;
+                               this.displayFormat = this.options.time.displayFormats[this.tickUnit];
 
                                // Work our way up to comfort
                                helpers.each(time.units, function(format) {
                                        }
                                        this.tickUnit = format;
                                        this.tickRange = Math.ceil(this.lastTick.diff(this.firstTick, this.tickUnit) + buffer);
-                                       this.displayFormat = time.unit[format].display;
+                                       this.displayFormat = this.options.time.displayFormats[format];
 
                                }, this);
                        }
                },
                convertTicksToLabels: function() {
                        this.ticks = this.ticks.map(function(tick, index, ticks) {
-                               var formattedTick = tick.format(this.options.time.displayFormat ? this.options.time.displayFormat : time.unit[this.tickUnit].display);
+                               var formattedTick = tick.format(this.options.time.displayFormat ? this.options.time.displayFormat : this.options.time.displayFormats[this.tickUnit]);
 
                                if (this.options.ticks.userCallback) {
                                        return this.options.ticks.userCallback(formattedTick, index, ticks);
index 4f5c785e9010f652bd88d51daf498b63be25485f..215f85fe2e97c1be3a35990e0773e796f0559e32 100644 (file)
@@ -53,6 +53,17 @@ describe('Time scale tests', function() {
                                unit: false,
                                round: false,
                                displayFormat: false,
+                               displayFormats: {
+                                       'millisecond': 'SSS [ms]',
+                                       'second': 'h:mm:ss a', // 11:20:01 AM
+                                       'minute': 'h:mm:ss a', // 11:20:01 AM
+                                       'hour': 'MMM D, hA', // Sept 4, 5PM
+                                       'day': 'll', // Sep 4 2015
+                                       'week': 'll', // Week 46, or maybe "[W]WW - YYYY" ?
+                                       'month': 'MMM YYYY', // Sept 2015
+                                       'quarter': '[Q]Q - YYYY', // Q3
+                                       'year': 'YYYY', // 2015
+                               },
                        }
                });