Skip to content

Commit b767c24

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

File tree

6 files changed

+77
-17
lines changed

6 files changed

+77
-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: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ function arrayUnique(items) {
8080
return out;
8181
}
8282

83+
/**
84+
* @TODO remove options.time.min at v3
85+
*/
86+
function getSpecifiedMin(options) {
87+
var timeMin = options.time.min;
88+
if (!helpers.isNullOrUndef(timeMin)) {
89+
console.warn('options.time.min is deprecated. Pleaese use options.ticks.min');
90+
return timeMin;
91+
}
92+
return options.ticks.min;
93+
}
94+
95+
/**
96+
* @TODO remove options.time.max at v3
97+
*/
98+
function getSpecifiedMax(options) {
99+
var timeMax = options.time.max;
100+
if (!helpers.isNullOrUndef(timeMax)) {
101+
console.warn('options.time.max is deprecated. Pleaese use options.ticks.max');
102+
return timeMax;
103+
}
104+
return options.ticks.max;
105+
}
106+
107+
83108
/**
84109
* Returns an array of {time, pos} objects used to interpolate a specific `time` or position
85110
* (`pos`) on the scale, by searching entries before and after the requested value. `pos` is
@@ -371,15 +396,15 @@ function computeOffsets(table, ticks, min, max, options) {
371396
var first, last;
372397

373398
if (options.offset && ticks.length) {
374-
if (!options.time.min) {
399+
if (!getSpecifiedMin(options)) {
375400
first = interpolate(table, 'time', ticks[0], 'pos');
376401
if (ticks.length === 1) {
377402
start = 1 - first;
378403
} else {
379404
start = (interpolate(table, 'time', ticks[1], 'pos') - first) / 2;
380405
}
381406
}
382-
if (!options.time.max) {
407+
if (!getSpecifiedMax(options)) {
383408
last = interpolate(table, 'time', ticks[ticks.length - 1], 'pos');
384409
if (ticks.length === 1) {
385410
end = last;
@@ -500,7 +525,8 @@ module.exports = Scale.extend({
500525
var me = this;
501526
var chart = me.chart;
502527
var adapter = me._adapter;
503-
var timeOpts = me.options.time;
528+
var options = me.options;
529+
var timeOpts = options.time;
504530
var unit = timeOpts.unit || 'day';
505531
var min = MAX_INTEGER;
506532
var max = MIN_INTEGER;
@@ -553,8 +579,8 @@ module.exports = Scale.extend({
553579
max = Math.max(max, timestamps[timestamps.length - 1]);
554580
}
555581

556-
min = parse(me, timeOpts.min) || min;
557-
max = parse(me, timeOpts.max) || max;
582+
min = parse(me, getSpecifiedMin(options)) || min;
583+
max = parse(me, getSpecifiedMax(options)) || max;
558584

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

604630
// Enforce limits with user min/max options
605-
min = parse(me, timeOpts.min) || min;
606-
max = parse(me, timeOpts.max) || max;
631+
min = parse(me, getSpecifiedMin(options)) || min;
632+
max = parse(me, getSpecifiedMax(options)) || max;
607633

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