Skip to content

Commit

Permalink
Specify time scale min and max options in standard manner (#6097)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and simonbrunel committed Apr 2, 2019
1 parent 6a2d87f commit a43e3b0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
2 changes: 2 additions & 0 deletions docs/axes/cartesian/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
2 changes: 0 additions & 2 deletions docs/axes/cartesian/linear.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions docs/axes/cartesian/logarithmic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 0 additions & 2 deletions docs/axes/cartesian/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 27 additions & 11 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -371,15 +387,15 @@ 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;
} else {
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;
Expand Down Expand Up @@ -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
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions test/specs/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit a43e3b0

Please sign in to comment.