Skip to content

Commit

Permalink
Specify time scale options in standard manner
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Mar 25, 2019
1 parent 79fc340 commit 36a657a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/axes/cartesian/README.md
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
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
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
Expand Up @@ -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` | <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
31 changes: 24 additions & 7 deletions src/scales/scale.time.js
Expand Up @@ -80,6 +80,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 +379,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 @@ -477,6 +485,14 @@ module.exports = Scale.extend({
console.warn('options.time.format is deprecated and replaced by options.time.parser.');
}

if (time.min) {
console.warn('options.time.min is deprecated. Please use options.ticks.min');
}

if (time.max) {
console.warn('options.time.max is deprecated. Please use options.ticks.max');
}

// Backward compatibility: before introducing adapter, `displayFormats` was
// supposed to contain *all* unit/string pairs but this can't be resolved
// when loading the scale (adapters are loaded afterward), so let's populate
Expand All @@ -500,7 +516,8 @@ module.exports = Scale.extend({
var me = this;
var chart = me.chart;
var adapter = me._adapter;
var timeOpts = me.options.time;
var options = me.options;
var timeOpts = options.time;
var unit = timeOpts.unit || 'day';
var min = MAX_INTEGER;
var max = MIN_INTEGER;
Expand Down Expand Up @@ -553,8 +570,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 +619,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
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 36a657a

Please sign in to comment.