]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Support isoWeekday when rounding (#7269)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 13 Apr 2020 23:34:18 +0000 (02:34 +0300)
committerGitHub <noreply@github.com>
Mon, 13 Apr 2020 23:34:18 +0000 (19:34 -0400)
src/scales/scale.time.js
test/specs/scale.time.tests.js

index 945bcc08210f6cac6a1f5a7476d4e61b4c73a565..49e5b1b191ca475f1b471c7e16bfcb86f0ca4d06 100644 (file)
@@ -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;
index a41341f37d6da51705d781433cfde01ce0d7c1f2..5d399eb5bc86484caa4bcdb66c65b2b926c24731 100644 (file)
@@ -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',