From: Jukka Kurkela Date: Mon, 13 Apr 2020 23:34:18 +0000 (+0300) Subject: Support isoWeekday when rounding (#7269) X-Git-Tag: v3.0.0-beta.2~167 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32de1b6ebf710cc82cc47a8dea8ac2acee0d208d;p=thirdparty%2FChart.js.git Support isoWeekday when rounding (#7269) --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 945bcc082..49e5b1b19 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -70,7 +70,7 @@ function parse(scale, input) { const adapter = scale._adapter; const options = scale.options.time; - const parser = options.parser; + const {parser, round, isoWeekday} = options; let value = input; if (typeof parser === 'function') { @@ -88,8 +88,10 @@ function parse(scale, input) { return value; } - if (options.round) { - value = scale._adapter.startOf(value, options.round); + if (round) { + value = round === 'week' && isoWeekday + ? scale._adapter.startOf(value, 'isoWeek', isoWeekday) + : scale._adapter.startOf(value, round); } return +value; diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index a41341f37..5d399eb5b 100644 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -486,6 +486,37 @@ describe('Time scale tests', function() { expect(xScale.getLabelForValue(value)).toBe('Jan 1, 2015, 8:00:00 pm'); }); + it('should round to isoWeekday', function() { + var chart = window.acquireChart({ + type: 'line', + data: { + datasets: [{ + data: [{x: '2020-04-12T20:00:00', y: 1}, {x: '2020-04-13T20:00:00', y: 2}] + }] + }, + options: { + scales: { + x: { + type: 'time', + ticks: { + source: 'data' + }, + time: { + unit: 'week', + round: 'week', + isoWeekday: 1, + displayFormats: { + week: 'WW' + } + } + }, + } + } + }); + + expect(getLabels(chart.scales.x)).toEqual(['15', '16']); + }); + it('should get the correct label for a timestamp', function() { var chart = window.acquireChart({ type: 'line',