Skip to content

Commit 36a657a

Browse files
committed
Specify time scale options in standard manner
1 parent 79fc340 commit 36a657a

File tree

6 files changed

+68
-17
lines changed

6 files changed

+68
-17
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
@@ -34,8 +34,6 @@ The following options are provided by the time scale. You may also set options p
3434
| `ticks.source` | `string` | `'auto'` | How ticks are generated. [more...](#ticks-source)
3535
| `time.displayFormats` | `object` | | Sets how different time units are displayed. [more...](#display-formats)
3636
| `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.
37-
| `time.max` | [Time](#date-formats) | | If defined, this will override the data maximum.
38-
| `time.min` | [Time](#date-formats) | | If defined, this will override the data minimum.
3937
| `time.parser` | <code>string&#124;function</code> | | Custom parser for dates. [more...](#parser)
4038
| `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.
4139
| `time.tooltipFormat` | `string` | | The Moment.js format string to use for the tooltip.

src/scales/scale.time.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ function arrayUnique(items) {
8080
return out;
8181
}
8282

83+
function getMin(options) {
84+
return helpers.valueOrDefault(options.time.min, options.ticks.min);
85+
}
86+
87+
function getMax(options) {
88+
return helpers.valueOrDefault(options.time.max, options.ticks.max);
89+
}
90+
8391
/**
8492
* Returns an array of {time, pos} objects used to interpolate a specific `time` or position
8593
* (`pos`) on the scale, by searching entries before and after the requested value. `pos` is
@@ -371,15 +379,15 @@ function computeOffsets(table, ticks, min, max, options) {
371379
var first, last;
372380

373381
if (options.offset && ticks.length) {
374-
if (!options.time.min) {
382+
if (!getMin(options)) {
375383
first = interpolate(table, 'time', ticks[0], 'pos');
376384
if (ticks.length === 1) {
377385
start = 1 - first;
378386
} else {
379387
start = (interpolate(table, 'time', ticks[1], 'pos') - first) / 2;
380388
}
381389
}
382-
if (!options.time.max) {
390+
if (!getMax(options)) {
383391
last = interpolate(table, 'time', ticks[ticks.length - 1], 'pos');
384392
if (ticks.length === 1) {
385393
end = last;
@@ -477,6 +485,14 @@ module.exports = Scale.extend({
477485
console.warn('options.time.format is deprecated and replaced by options.time.parser.');
478486
}
479487

488+
if (time.min) {
489+
console.warn('options.time.min is deprecated. Please use options.ticks.min');
490+
}
491+
492+
if (time.max) {
493+
console.warn('options.time.max is deprecated. Please use options.ticks.max');
494+
}
495+
480496
// Backward compatibility: before introducing adapter, `displayFormats` was
481497
// supposed to contain *all* unit/string pairs but this can't be resolved
482498
// when loading the scale (adapters are loaded afterward), so let's populate
@@ -500,7 +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;
519+
var options = me.options;
520+
var timeOpts = options.time;
504521
var unit = timeOpts.unit || 'day';
505522
var min = MAX_INTEGER;
506523
var max = MIN_INTEGER;
@@ -553,8 +570,8 @@ module.exports = Scale.extend({
553570
max = Math.max(max, timestamps[timestamps.length - 1]);
554571
}
555572

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

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

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

608625
// Remove ticks outside the min/max range
609626
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)