From: Jukka Kurkela Date: Sat, 27 Mar 2021 19:55:54 +0000 (+0200) Subject: Time: Fix offset with low data counts (#8740) X-Git-Tag: v3.0.0-rc.6~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=64593ed74c4e6f83cc3c7c0301629b3a05f1a408;p=thirdparty%2FChart.js.git Time: Fix offset with low data counts (#8740) --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 144602ba5..de758952f 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -291,7 +291,7 @@ export default class TimeScale extends Scale { max = isFinite(max) && !isNaN(max) ? max : +adapter.endOf(Date.now(), unit) + 1; // Make sure that max is strictly higher than min (required by the timeseries lookup table) - me.min = Math.min(min, max); + me.min = Math.min(min, max - 1); me.max = Math.max(min + 1, max); } @@ -376,8 +376,9 @@ export default class TimeScale extends Scale { end = (last - me.getDecimalForValue(timestamps[timestamps.length - 2])) / 2; } } - start = _limitValue(start, 0, 0.25); - end = _limitValue(end, 0, 0.25); + const limit = timestamps.length < 3 ? 0.5 : 0.25; + start = _limitValue(start, 0, limit); + end = _limitValue(end, 0, limit); me._offsets = {start, end, factor: 1 / (start + 1 + end)}; } diff --git a/test/fixtures/scale.time/offset-with-1-tick.js b/test/fixtures/scale.time/offset-with-1-tick.js new file mode 100644 index 000000000..748730791 --- /dev/null +++ b/test/fixtures/scale.time/offset-with-1-tick.js @@ -0,0 +1,59 @@ +const data = { + datasets: [ + { + label: 6, + backgroundColor: 'red', + data: [ + { + x: '2021-03-24', + y: 464 + } + ] + }, + { + label: 1, + backgroundColor: 'red', + data: [ + { + x: '2021-03-24', + y: 464 + } + ] + }, + { + label: 17, + backgroundColor: 'blue', + data: [ + { + x: '2021-03-24', + y: 390 + } + ] + } + ] +}; + +module.exports = { + description: 'https://github.com/chartjs/Chart.js/issues/8718', + config: { + type: 'bar', + data, + options: { + scales: { + x: { + type: 'time', + time: { + unit: 'day', + }, + }, + y: { + display: false + } + } + } + }, + options: { + spriteText: true, + canvas: {width: 256, height: 128} + } +}; diff --git a/test/fixtures/scale.time/offset-with-1-tick.png b/test/fixtures/scale.time/offset-with-1-tick.png new file mode 100644 index 000000000..87870bff5 Binary files /dev/null and b/test/fixtures/scale.time/offset-with-1-tick.png differ diff --git a/test/fixtures/scale.time/offset-with-2-ticks.js b/test/fixtures/scale.time/offset-with-2-ticks.js new file mode 100644 index 000000000..f5708d013 --- /dev/null +++ b/test/fixtures/scale.time/offset-with-2-ticks.js @@ -0,0 +1,89 @@ +const data = { + datasets: [ + { + label: 1, + backgroundColor: 'orange', + data: [ + { + x: '2021-03-24', + y: 464 + } + ] + }, + { + label: 2, + backgroundColor: 'red', + data: [ + { + x: '2021-03-24', + y: 464 + } + ] + }, + { + label: 3, + backgroundColor: 'blue', + data: [ + { + x: '2021-03-24', + y: 390 + } + ] + }, + { + label: 4, + backgroundColor: 'purple', + data: [ + { + x: '2021-03-25', + y: 464 + } + ] + }, + { + label: 5, + backgroundColor: 'black', + data: [ + { + x: '2021-03-25', + y: 464 + } + ] + }, + { + label: 6, + backgroundColor: 'cyan', + data: [ + { + x: '2021-03-25', + y: 390 + } + ] + } + ] +}; + +module.exports = { + description: 'https://github.com/chartjs/Chart.js/issues/8718', + config: { + type: 'bar', + data, + options: { + scales: { + x: { + type: 'time', + time: { + unit: 'day', + }, + }, + y: { + display: false + } + } + } + }, + options: { + spriteText: true, + canvas: {width: 256, height: 128} + } +}; diff --git a/test/fixtures/scale.time/offset-with-2-ticks.png b/test/fixtures/scale.time/offset-with-2-ticks.png new file mode 100644 index 000000000..5b02f0015 Binary files /dev/null and b/test/fixtures/scale.time/offset-with-2-ticks.png differ diff --git a/test/fixtures/scale.time/offset-with-no-ticks.js b/test/fixtures/scale.time/offset-with-no-ticks.js index 010dd519b..c3b184e36 100644 --- a/test/fixtures/scale.time/offset-with-no-ticks.js +++ b/test/fixtures/scale.time/offset-with-no-ticks.js @@ -63,7 +63,6 @@ module.exports = { scales: { x: { type: 'time', - // offset: false, time: { unit: 'month', }, diff --git a/test/fixtures/scale.time/offset-with-no-ticks.png b/test/fixtures/scale.time/offset-with-no-ticks.png index a43c8c006..cfc78366b 100644 Binary files a/test/fixtures/scale.time/offset-with-no-ticks.png and b/test/fixtures/scale.time/offset-with-no-ticks.png differ