diff --git a/docs/axes/cartesian/README.md b/docs/axes/cartesian/README.md index 86611b2fdb4..5a643354ca4 100644 --- a/docs/axes/cartesian/README.md +++ b/docs/axes/cartesian/README.md @@ -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* diff --git a/docs/axes/cartesian/linear.md b/docs/axes/cartesian/linear.md index ad82c552a95..ef4833b56e0 100644 --- a/docs/axes/cartesian/linear.md +++ b/docs/axes/cartesian/linear.md @@ -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) diff --git a/docs/axes/cartesian/logarithmic.md b/docs/axes/cartesian/logarithmic.md index 4878f6bee7f..b9e0d8d1bc5 100644 --- a/docs/axes/cartesian/logarithmic.md +++ b/docs/axes/cartesian/logarithmic.md @@ -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. diff --git a/docs/axes/cartesian/time.md b/docs/axes/cartesian/time.md index a3d3e016cab..4a8be874c2f 100644 --- a/docs/axes/cartesian/time.md +++ b/docs/axes/cartesian/time.md @@ -34,8 +34,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` | string|function | | 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. diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 7d1df171e0f..e9f5bf3ad7d 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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) { diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 5258fba5d38..3312c68ca39 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -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';