]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Use time.unit option to create default min/max for empty chart (#5045)
authorjcopperfield <33193571+jcopperfield@users.noreply.github.com>
Fri, 15 Dec 2017 17:10:30 +0000 (18:10 +0100)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 15 Dec 2017 17:10:30 +0000 (18:10 +0100)
src/scales/scale.time.js
test/specs/scale.time.tests.js

index cfa9f829aa9e89ba92d46b5346f4286ce6a71925..90904f9e6f0e7b7ae9be4fe22e1ef477c6b7de2f 100644 (file)
@@ -504,6 +504,7 @@ module.exports = function(Chart) {
                        var me = this;
                        var chart = me.chart;
                        var timeOpts = me.options.time;
+                       var unit = timeOpts.unit || 'day';
                        var min = MAX_INTEGER;
                        var max = MIN_INTEGER;
                        var timestamps = [];
@@ -555,9 +556,9 @@ module.exports = function(Chart) {
                        min = parse(timeOpts.min, me) || min;
                        max = parse(timeOpts.max, me) || max;
 
-                       // In case there is no valid min/max, let's use today limits
-                       min = min === MAX_INTEGER ? +moment().startOf('day') : min;
-                       max = max === MIN_INTEGER ? +moment().endOf('day') + 1 : max;
+                       // In case there is no valid min/max, set limits based on unit time option
+                       min = min === MAX_INTEGER ? +moment().startOf(unit) : min;
+                       max = max === MIN_INTEGER ? +moment().endOf(unit) + 1 : max;
 
                        // Make sure that max is strictly higher than min (required by the lookup table)
                        me.min = Math.min(min, max);
index 19189040ee453d9d62d06c46d56273c0d8004ef6..e4c1739e6c579bce874afbaf8a9eb1ba917c3c45 100755 (executable)
@@ -703,7 +703,7 @@ describe('Time scale tests', function() {
                                expect(getTicksLabels(scale)).toEqual([
                                        '2017', '2019', '2020', '2025', '2042']);
                        });
-                       it ('should correctly handle empty `data.labels`', function() {
+                       it ('should correctly handle empty `data.labels` using "day" if `time.unit` is undefined`', function() {
                                var chart = this.chart;
                                var scale = chart.scales.x;
 
@@ -714,6 +714,19 @@ describe('Time scale tests', function() {
                                expect(scale.max).toEqual(+moment().endOf('day') + 1);
                                expect(getTicksLabels(scale)).toEqual([]);
                        });
+                       it ('should correctly handle empty `data.labels` using `time.unit`', function() {
+                               var chart = this.chart;
+                               var scale = chart.scales.x;
+                               var options = chart.options.scales.xAxes[0];
+
+                               options.time.unit = 'year';
+                               chart.data.labels = [];
+                               chart.update();
+
+                               expect(scale.min).toEqual(+moment().startOf('year'));
+                               expect(scale.max).toEqual(+moment().endOf('year') + 1);
+                               expect(getTicksLabels(scale)).toEqual([]);
+                       });
                });
 
                describe('is "data"', function() {
@@ -784,7 +797,7 @@ describe('Time scale tests', function() {
                                expect(getTicksLabels(scale)).toEqual([
                                        '2017', '2018', '2019', '2020', '2025', '2042', '2043']);
                        });
-                       it ('should correctly handle empty `data.labels`', function() {
+                       it ('should correctly handle empty `data.labels` using "day" if `time.unit` is undefined`', function() {
                                var chart = this.chart;
                                var scale = chart.scales.x;
 
@@ -796,6 +809,21 @@ describe('Time scale tests', function() {
                                expect(getTicksLabels(scale)).toEqual([
                                        '2018', '2020', '2043']);
                        });
+                       it ('should correctly handle empty `data.labels` and hidden datasets using `time.unit`', function() {
+                               var chart = this.chart;
+                               var scale = chart.scales.x;
+                               var options = chart.options.scales.xAxes[0];
+
+                               options.time.unit = 'year';
+                               chart.data.labels = [];
+                               var meta = chart.getDatasetMeta(1);
+                               meta.hidden = true;
+                               chart.update();
+
+                               expect(scale.min).toEqual(+moment().startOf('year'));
+                               expect(scale.max).toEqual(+moment().endOf('year') + 1);
+                               expect(getTicksLabels(scale)).toEqual([]);
+                       });
                });
        });