Skip to content

Commit 32de1b6

Browse files
authored
Support isoWeekday when rounding (#7269)
1 parent 1228981 commit 32de1b6

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/scales/scale.time.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function parse(scale, input) {
7070

7171
const adapter = scale._adapter;
7272
const options = scale.options.time;
73-
const parser = options.parser;
73+
const {parser, round, isoWeekday} = options;
7474
let value = input;
7575

7676
if (typeof parser === 'function') {
@@ -88,8 +88,10 @@ function parse(scale, input) {
8888
return value;
8989
}
9090

91-
if (options.round) {
92-
value = scale._adapter.startOf(value, options.round);
91+
if (round) {
92+
value = round === 'week' && isoWeekday
93+
? scale._adapter.startOf(value, 'isoWeek', isoWeekday)
94+
: scale._adapter.startOf(value, round);
9395
}
9496

9597
return +value;

test/specs/scale.time.tests.js

+31
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,37 @@ describe('Time scale tests', function() {
486486
expect(xScale.getLabelForValue(value)).toBe('Jan 1, 2015, 8:00:00 pm');
487487
});
488488

489+
it('should round to isoWeekday', function() {
490+
var chart = window.acquireChart({
491+
type: 'line',
492+
data: {
493+
datasets: [{
494+
data: [{x: '2020-04-12T20:00:00', y: 1}, {x: '2020-04-13T20:00:00', y: 2}]
495+
}]
496+
},
497+
options: {
498+
scales: {
499+
x: {
500+
type: 'time',
501+
ticks: {
502+
source: 'data'
503+
},
504+
time: {
505+
unit: 'week',
506+
round: 'week',
507+
isoWeekday: 1,
508+
displayFormats: {
509+
week: 'WW'
510+
}
511+
}
512+
},
513+
}
514+
}
515+
});
516+
517+
expect(getLabels(chart.scales.x)).toEqual(['15', '16']);
518+
});
519+
489520
it('should get the correct label for a timestamp', function() {
490521
var chart = window.acquireChart({
491522
type: 'line',

0 commit comments

Comments
 (0)