From: stockiNail Date: Mon, 7 Sep 2020 20:19:45 +0000 (+0200) Subject: isoWeekday time options on time scale as number (#7768) X-Git-Tag: v3.0.0-beta.2~1^2~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fd34e786ab5668ecaf1488379525c1baf68617d9;p=thirdparty%2FChart.js.git isoWeekday time options on time scale as number (#7768) * fixed default of tension property of line element * Fixes isoWeekday on time scale #7749 * adds checking on isoWeekDay options in _generate function --- diff --git a/docs/docs/axes/cartesian/time.md b/docs/docs/axes/cartesian/time.md index 6fb5d1a36..43438bae1 100644 --- a/docs/docs/axes/cartesian/time.md +++ b/docs/docs/axes/cartesian/time.md @@ -28,7 +28,7 @@ The following options are provided by the time scale. You may also set options p | `bounds` | `string` | `'data'` | Determines the scale bounds. [more...](#scale-bounds) | `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.isoWeekday` | `boolean`\|`number` | `false` | If `boolean` and true and the unit is set to 'week', then the first day of the week will be Monday. Otherwise, it will be Sunday. If `number`, the index of the first day of the week (0 - Sunday, 6 - Saturday) | `time.parser` | `string`\|`function` | | 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 format string to use for the tooltip. diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 2df061883..f8ad236b1 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -1,6 +1,6 @@ import adapters from '../core/core.adapters'; import {isFinite, isNullOrUndef, mergeIf, valueOrDefault} from '../helpers/helpers.core'; -import {toRadians} from '../helpers/helpers.math'; +import {toRadians, isNumber} from '../helpers/helpers.math'; import Scale from '../core/core.scale'; import {_arrayUnique, _filterBetween, _lookup} from '../helpers/helpers.collection'; @@ -71,7 +71,7 @@ function parse(scale, input) { } if (round) { - value = round === 'week' && isoWeekday + value = round === 'week' && (isNumber(isoWeekday) || isoWeekday === true) ? scale._adapter.startOf(value, 'isoWeek', isoWeekday) : scale._adapter.startOf(value, round); } @@ -400,17 +400,18 @@ export default class TimeScale extends Scale { const minor = timeOpts.unit || determineUnitForAutoTicks(timeOpts.minUnit, min, max, me._getLabelCapacity(min)); const stepSize = valueOrDefault(timeOpts.stepSize, 1); const weekday = minor === 'week' ? timeOpts.isoWeekday : false; + const hasWeekday = isNumber(weekday) || weekday === true; const ticks = {}; let first = min; let time; // For 'week' unit, handle the first day of week option - if (weekday) { + if (hasWeekday) { first = +adapter.startOf(first, 'isoWeek', weekday); } // Align first ticks on unit - first = +adapter.startOf(first, weekday ? 'day' : minor); + first = +adapter.startOf(first, hasWeekday ? 'day' : minor); // Prevent browser from freezing in case user options request millions of milliseconds if (adapter.diff(max, min, minor) > 100000 * stepSize) { diff --git a/types/scales/index.d.ts b/types/scales/index.d.ts index de9398560..729aaf457 100644 --- a/types/scales/index.d.ts +++ b/types/scales/index.d.ts @@ -276,10 +276,11 @@ export type ITimeScaleOptions = ICartesianScaleOptions & { */ round: false | TimeUnit; /** - * If true and the unit is set to 'week', then the first day of the week will be Monday. Otherwise, it will be Sunday. + * If boolean and true and the unit is set to 'week', then the first day of the week will be Monday. Otherwise, it will be Sunday. + * If `number`, the index of the first day of the week (0 - Sunday, 6 - Saturday). * @default false */ - isoWeekday: false | string; + isoWeekday: false | number; /** * Sets how different time units are displayed. * @see https://www.chartjs.org/docs/next/axes/cartesian/time#display-formats