Skip to content

Commit 16c7457

Browse files
benmccannsimonbrunel
authored andcommitted
Specify time scale min and max options in standard manner (chartjs#6097)
1 parent 046adec commit 16c7457

File tree

6 files changed

+71
-21
lines changed

6 files changed

+71
-21
lines changed

docs/axes/cartesian/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The following options are common to all cartesian axes but do not apply to other
2626

2727
| Name | Type | Default | Description
2828
| ---- | ---- | ------- | -----------
29+
| `min` | `number` | | User defined minimum value for the scale, overrides minimum value from data.
30+
| `max` | `number` | | User defined maximum value for the scale, overrides maximum value from data.
2931
| `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.
3032
| `autoSkipPadding` | `number` | `0` | Padding between the ticks on the horizontal axis when `autoSkip` is enabled.
3133
| `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*

docs/axes/cartesian/linear.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The following options are provided by the linear scale. They are all located in
99
| Name | Type | Default | Description
1010
| ---- | ---- | ------- | -----------
1111
| `beginAtZero` | `boolean` | | if true, scale will include 0 if it is not already included.
12-
| `min` | `number` | | User defined minimum number for the scale, overrides minimum value from data. [more...](#axis-range-settings)
13-
| `max` | `number` | | User defined maximum number for the scale, overrides maximum value from data. [more...](#axis-range-settings)
1412
| `maxTicksLimit` | `number` | `11` | Maximum number of ticks and gridlines to show.
1513
| `precision` | `number` | | if defined and `stepSize` is not specified, the step size will be rounded to this many decimal places.
1614
| `stepSize` | `number` | | User defined fixed step size for the scale. [more...](#step-size)

docs/axes/cartesian/logarithmic.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@ The logarithmic scale is use to chart numerical data. It can be placed on either
44

55
## Tick Configuration Options
66

7-
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).
8-
9-
| Name | Type | Default | Description
10-
| ---- | ---- | ------- | -----------
11-
| `min` | `number` | | User defined minimum number for the scale, overrides minimum value from data.
12-
| `max` | `number` | | User defined maximum number for the scale, overrides maximum value from data.
7+
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.

docs/axes/cartesian/time.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ The following options are provided by the time scale. You may also set options p
3838
| `ticks.source` | `string` | `'auto'` | How ticks are generated. [more...](#ticks-source)
3939
| `time.displayFormats` | `object` | | Sets how different time units are displayed. [more...](#display-formats)
4040
| `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.
41-
| `time.max` | [Time](#date-formats) | | If defined, this will override the data maximum.
42-
| `time.min` | [Time](#date-formats) | | If defined, this will override the data minimum.
4341
| `time.parser` | <code>string&#124;function</code> | | Custom parser for dates. [more...](#parser)
4442
| `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.
4543
| `time.tooltipFormat` | `string` | | The Moment.js format string to use for the tooltip.

src/scales/scale.time.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ var INTERVALS = {
6060

6161
var UNITS = Object.keys(INTERVALS);
6262

63+
function deprecated(value, previous, current) {
64+
if (value !== undefined) {
65+
console.warn(
66+
'time scale: "' + previous + '" is deprecated. ' +
67+
'Please use "' + current + '" instead');
68+
}
69+
}
70+
6371
function sorter(a, b) {
6472
return a - b;
6573
}
@@ -80,6 +88,14 @@ function arrayUnique(items) {
8088
return out;
8189
}
8290

91+
function getMin(options) {
92+
return helpers.valueOrDefault(options.time.min, options.ticks.min);
93+
}
94+
95+
function getMax(options) {
96+
return helpers.valueOrDefault(options.time.max, options.ticks.max);
97+
}
98+
8399
/**
84100
* Returns an array of {time, pos} objects used to interpolate a specific `time` or position
85101
* (`pos`) on the scale, by searching entries before and after the requested value. `pos` is
@@ -371,15 +387,15 @@ function computeOffsets(table, ticks, min, max, options) {
371387
var first, last;
372388

373389
if (options.offset && ticks.length) {
374-
if (!options.time.min) {
390+
if (!getMin(options)) {
375391
first = interpolate(table, 'time', ticks[0], 'pos');
376392
if (ticks.length === 1) {
377393
start = 1 - first;
378394
} else {
379395
start = (interpolate(table, 'time', ticks[1], 'pos') - first) / 2;
380396
}
381397
}
382-
if (!options.time.max) {
398+
if (!getMax(options)) {
383399
last = interpolate(table, 'time', ticks[ticks.length - 1], 'pos');
384400
if (ticks.length === 1) {
385401
end = last;
@@ -473,9 +489,9 @@ module.exports = Scale.extend({
473489
var adapter = me._adapter = new adapters._date(options.adapters.date);
474490

475491
// DEPRECATIONS: output a message only one time per update
476-
if (time.format) {
477-
console.warn('options.time.format is deprecated and replaced by options.time.parser.');
478-
}
492+
deprecated(time.format, 'time.format', 'time.parser');
493+
deprecated(time.min, 'time.min', 'ticks.min');
494+
deprecated(time.max, 'time.max', 'ticks.max');
479495

480496
// Backward compatibility: before introducing adapter, `displayFormats` was
481497
// supposed to contain *all* unit/string pairs but this can't be resolved
@@ -500,8 +516,8 @@ module.exports = Scale.extend({
500516
var me = this;
501517
var chart = me.chart;
502518
var adapter = me._adapter;
503-
var timeOpts = me.options.time;
504-
var unit = timeOpts.unit || 'day';
519+
var options = me.options;
520+
var unit = options.time.unit || 'day';
505521
var min = MAX_INTEGER;
506522
var max = MIN_INTEGER;
507523
var timestamps = [];
@@ -553,8 +569,8 @@ module.exports = Scale.extend({
553569
max = Math.max(max, timestamps[timestamps.length - 1]);
554570
}
555571

556-
min = parse(me, timeOpts.min) || min;
557-
max = parse(me, timeOpts.max) || max;
572+
min = parse(me, getMin(options)) || min;
573+
max = parse(me, getMax(options)) || max;
558574

559575
// In case there is no valid min/max, set limits based on unit time option
560576
min = min === MAX_INTEGER ? +adapter.startOf(Date.now(), unit) : min;
@@ -602,8 +618,8 @@ module.exports = Scale.extend({
602618
}
603619

604620
// Enforce limits with user min/max options
605-
min = parse(me, timeOpts.min) || min;
606-
max = parse(me, timeOpts.max) || max;
621+
min = parse(me, getMin(options)) || min;
622+
max = parse(me, getMax(options)) || max;
607623

608624
// Remove ticks outside the min/max range
609625
for (i = 0, ilen = timestamps.length; i < ilen; ++i) {

test/specs/scale.time.tests.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,47 @@ describe('Time scale tests', function() {
371371
config.time.unit = 'day';
372372
});
373373

374+
it('should use the min option when less than first label for building ticks', function() {
375+
config.ticks.min = '2014-12-29T04:00:00';
376+
377+
var scale = createScale(mockData, config);
378+
expect(scale.ticks[0]).toEqual('Jan 1');
379+
});
380+
381+
it('should use the min option when greater than first label for building ticks', function() {
382+
config.ticks.min = '2015-01-02T04:00:00';
383+
384+
var scale = createScale(mockData, config);
385+
expect(scale.ticks[0]).toEqual('Jan 2');
386+
});
387+
388+
it('should use the max option when greater than last label for building ticks', function() {
389+
config.ticks.max = '2015-01-05T06:00:00';
390+
391+
var scale = createScale(mockData, config);
392+
expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 3');
393+
});
394+
395+
it('should use the max option when less than last label for building ticks', function() {
396+
config.ticks.max = '2015-01-02T23:00:00';
397+
398+
var scale = createScale(mockData, config);
399+
expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 2');
400+
});
401+
});
402+
403+
describe('when specifying limits in a deprecated fashion', function() {
404+
var mockData = {
405+
labels: ['2015-01-01T20:00:00', '2015-01-02T20:00:00', '2015-01-03T20:00:00'],
406+
};
407+
408+
var config;
409+
beforeEach(function() {
410+
config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
411+
config.ticks.source = 'labels';
412+
config.time.unit = 'day';
413+
});
414+
374415
it('should use the min option when less than first label for building ticks', function() {
375416
config.time.min = '2014-12-29T04:00:00';
376417

0 commit comments

Comments
 (0)