]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Specify time scale min and max options in standard manner (#6097)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Tue, 2 Apr 2019 07:43:26 +0000 (00:43 -0700)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Tue, 2 Apr 2019 07:43:26 +0000 (09:43 +0200)
docs/axes/cartesian/README.md
docs/axes/cartesian/linear.md
docs/axes/cartesian/logarithmic.md
docs/axes/cartesian/time.md
src/scales/scale.time.js
test/specs/scale.time.tests.js

index 2c4ab87ef9d020ca66547e838cd46eae0e7be2aa..c9e126cc07d559894afe5e482e1a882f79eff65a 100644 (file)
@@ -26,6 +26,8 @@ The following options are common to all cartesian axes but do not apply to other
 
 | Name | Type | Default | Description
 | ---- | ---- | ------- | -----------
+| `min` | `number` | | User defined minimum value for the scale, overrides minimum value from data.
+| `max` | `number` | | User defined maximum value for the scale, overrides maximum value from data.
 | `autoSkip` | `boolean` | `true` | If true, automatically calculates how many labels can be shown and hides labels accordingly. Labels will be rotated up to `maxRotation` before skipping any. Turn `autoSkip` off to show all labels no matter what.
 | `autoSkipPadding` | `number` | `0` | Padding between the ticks on the horizontal axis when `autoSkip` is enabled.
 | `labelOffset` | `number` | `0` | Distance in pixels to offset the label from the centre point of the tick (in the x direction for the x axis, and the y direction for the y axis). *Note: this can cause labels at the edges to be cropped by the edge of the canvas*
index ad82c552a9518ab551d64aa2e07c3a4b091c47d2..ef4833b56e0224a0ec84af20f12db0ee5e447e5a 100644 (file)
@@ -9,8 +9,6 @@ The following options are provided by the linear scale. They are all located in
 | Name | Type | Default | Description
 | ---- | ---- | ------- | -----------
 | `beginAtZero` | `boolean` | | if true, scale will include 0 if it is not already included.
-| `min` | `number` | | User defined minimum number for the scale, overrides minimum value from data. [more...](#axis-range-settings)
-| `max` | `number` | | User defined maximum number for the scale, overrides maximum value from data. [more...](#axis-range-settings)
 | `maxTicksLimit` | `number` | `11` | Maximum number of ticks and gridlines to show.
 | `precision` | `number` | | if defined and `stepSize` is not specified, the step size will be rounded to this many decimal places.
 | `stepSize` | `number` | | User defined fixed step size for the scale. [more...](#step-size)
index 4878f6bee7f776d13ddac59171cc6b232b31afa7..b9e0d8d1bc5880a08aa6048bd2626f505f0845e5 100644 (file)
@@ -4,9 +4,4 @@ The logarithmic scale is use to chart numerical data. It can be placed on either
 
 ## Tick Configuration Options
 
-The following options are provided by the logarithmic scale. They are all located in the `ticks` sub options. These options extend the [common tick configuration](README.md#tick-configuration).
-
-| Name | Type | Default | Description
-| ---- | ---- | ------- | -----------
-| `min` | `number` | | User defined minimum number for the scale, overrides minimum value from data.
-| `max` | `number` | | User defined maximum number for the scale, overrides maximum value from data.
+The logarithmic scale options extend the [common tick configuration](README.md#tick-configuration). This scale does not define any options that are unique to it.
index 61e04da749b7315ad2242ecc99107189ac7d8cf3..bdd4d1b65f4a91428858f4afddefa1c8a2c38c2e 100644 (file)
@@ -38,8 +38,6 @@ The following options are provided by the time scale. You may also set options p
 | `ticks.source` | `string` | `'auto'` | How ticks are generated. [more...](#ticks-source)
 | `time.displayFormats` | `object` | | Sets how different time units are displayed. [more...](#display-formats)
 | `time.isoWeekday` | `boolean` | `false` | If true and the unit is set to 'week', then the first day of the week will be Monday. Otherwise, it will be Sunday.
-| `time.max` | [Time](#date-formats) | | If defined, this will override the data maximum.
-| `time.min` | [Time](#date-formats) | | If defined, this will override the data minimum.
 | `time.parser` | <code>string&#124;function</code> | | Custom parser for dates. [more...](#parser)
 | `time.round` | `string` | `false` | If defined, dates will be rounded to the start of this unit. See [Time Units](#time-units) below for the allowed units.
 | `time.tooltipFormat` | `string` | | The Moment.js format string to use for the tooltip.
index 0db15bb7859c17ec74921bc306340fde8fae7424..69b9bb3d7b8cdc02fd5e540752340ef9685b7e62 100644 (file)
@@ -60,6 +60,14 @@ var INTERVALS = {
 
 var UNITS = Object.keys(INTERVALS);
 
+function deprecated(value, previous, current) {
+       if (value !== undefined) {
+               console.warn(
+                       'time scale: "' + previous + '" is deprecated. ' +
+                       'Please use "' + current + '" instead');
+       }
+}
+
 function sorter(a, b) {
        return a - b;
 }
@@ -80,6 +88,14 @@ function arrayUnique(items) {
        return out;
 }
 
+function getMin(options) {
+       return helpers.valueOrDefault(options.time.min, options.ticks.min);
+}
+
+function getMax(options) {
+       return helpers.valueOrDefault(options.time.max, options.ticks.max);
+}
+
 /**
  * Returns an array of {time, pos} objects used to interpolate a specific `time` or position
  * (`pos`) on the scale, by searching entries before and after the requested value. `pos` is
@@ -371,7 +387,7 @@ function computeOffsets(table, ticks, min, max, options) {
        var first, last;
 
        if (options.offset && ticks.length) {
-               if (!options.time.min) {
+               if (!getMin(options)) {
                        first = interpolate(table, 'time', ticks[0], 'pos');
                        if (ticks.length === 1) {
                                start = 1 - first;
@@ -379,7 +395,7 @@ function computeOffsets(table, ticks, min, max, options) {
                                start = (interpolate(table, 'time', ticks[1], 'pos') - first) / 2;
                        }
                }
-               if (!options.time.max) {
+               if (!getMax(options)) {
                        last = interpolate(table, 'time', ticks[ticks.length - 1], 'pos');
                        if (ticks.length === 1) {
                                end = last;
@@ -473,9 +489,9 @@ module.exports = Scale.extend({
                var adapter = me._adapter = new adapters._date(options.adapters.date);
 
                // DEPRECATIONS: output a message only one time per update
-               if (time.format) {
-                       console.warn('options.time.format is deprecated and replaced by options.time.parser.');
-               }
+               deprecated(time.format, 'time.format', 'time.parser');
+               deprecated(time.min, 'time.min', 'ticks.min');
+               deprecated(time.max, 'time.max', 'ticks.max');
 
                // Backward compatibility: before introducing adapter, `displayFormats` was
                // supposed to contain *all* unit/string pairs but this can't be resolved
@@ -500,8 +516,8 @@ module.exports = Scale.extend({
                var me = this;
                var chart = me.chart;
                var adapter = me._adapter;
-               var timeOpts = me.options.time;
-               var unit = timeOpts.unit || 'day';
+               var options = me.options;
+               var unit = options.time.unit || 'day';
                var min = MAX_INTEGER;
                var max = MIN_INTEGER;
                var timestamps = [];
@@ -553,8 +569,8 @@ module.exports = Scale.extend({
                        max = Math.max(max, timestamps[timestamps.length - 1]);
                }
 
-               min = parse(me, timeOpts.min) || min;
-               max = parse(me, timeOpts.max) || max;
+               min = parse(me, getMin(options)) || min;
+               max = parse(me, getMax(options)) || max;
 
                // In case there is no valid min/max, set limits based on unit time option
                min = min === MAX_INTEGER ? +adapter.startOf(Date.now(), unit) : min;
@@ -602,8 +618,8 @@ module.exports = Scale.extend({
                }
 
                // Enforce limits with user min/max options
-               min = parse(me, timeOpts.min) || min;
-               max = parse(me, timeOpts.max) || max;
+               min = parse(me, getMin(options)) || min;
+               max = parse(me, getMax(options)) || max;
 
                // Remove ticks outside the min/max range
                for (i = 0, ilen = timestamps.length; i < ilen; ++i) {
index 5258fba5d385f840a78806af20c5f49b9425ed97..3312c68ca3919165f1ce3de27c2b079186d44488 100755 (executable)
@@ -371,6 +371,47 @@ describe('Time scale tests', function() {
                        config.time.unit = 'day';
                });
 
+               it('should use the min option when less than first label for building ticks', function() {
+                       config.ticks.min = '2014-12-29T04:00:00';
+
+                       var scale = createScale(mockData, config);
+                       expect(scale.ticks[0]).toEqual('Jan 1');
+               });
+
+               it('should use the min option when greater than first label for building ticks', function() {
+                       config.ticks.min = '2015-01-02T04:00:00';
+
+                       var scale = createScale(mockData, config);
+                       expect(scale.ticks[0]).toEqual('Jan 2');
+               });
+
+               it('should use the max option when greater than last label for building ticks', function() {
+                       config.ticks.max = '2015-01-05T06:00:00';
+
+                       var scale = createScale(mockData, config);
+                       expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 3');
+               });
+
+               it('should use the max option when less than last label for building ticks', function() {
+                       config.ticks.max = '2015-01-02T23:00:00';
+
+                       var scale = createScale(mockData, config);
+                       expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 2');
+               });
+       });
+
+       describe('when specifying limits in a deprecated fashion', function() {
+               var mockData = {
+                       labels: ['2015-01-01T20:00:00', '2015-01-02T20:00:00', '2015-01-03T20:00:00'],
+               };
+
+               var config;
+               beforeEach(function() {
+                       config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
+                       config.ticks.source = 'labels';
+                       config.time.unit = 'day';
+               });
+
                it('should use the min option when less than first label for building ticks', function() {
                        config.time.min = '2014-12-29T04:00:00';