From 1f1fd7971b4c05efb11fa3127ce1f2ea088c72bb Mon Sep 17 00:00:00 2001 From: CodingMarco Date: Sun, 22 Jan 2023 23:06:28 +0100 Subject: [PATCH 1/2] Fix: don't generate ticks > max if max is specified (#11083) --- src/scales/scale.linearbase.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index 4c6248d5dc0..d2da5501eb7 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -123,7 +123,11 @@ function generateTicks(generationOptions, dataRange) { } for (; j < numSpaces; ++j) { - ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor}); + const tickValue = Math.round((niceMin + j * spacing) * factor) / factor; + if (maxDefined && tickValue > max) { + break; + } + ticks.push({value: tickValue}); } if (maxDefined && includeBounds && niceMax !== max) { From 707ed83e134e19bb98add46cca52c71182b3d363 Mon Sep 17 00:00:00 2001 From: CodingMarco Date: Sun, 5 Feb 2023 23:49:13 +0100 Subject: [PATCH 2/2] Add test "Should not generate any ticks > max if max is specified" (#11083) --- test/specs/scale.linear.tests.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/specs/scale.linear.tests.js b/test/specs/scale.linear.tests.js index 9fbe5467a2d..a8ad53995b1 100644 --- a/test/specs/scale.linear.tests.js +++ b/test/specs/scale.linear.tests.js @@ -684,6 +684,28 @@ describe('Linear Scale', function() { expect(getLabels(chart.scales.y)).toEqual(['1', '3', '5', '7', '9', '11']); }); + it('Should not generate any ticks > max if max is specified', function() { + var chart = window.acquireChart({ + type: 'line', + options: { + scales: { + x: { + type: 'linear', + min: 2.404e-8, + max: 2.4143e-8, + ticks: { + includeBounds: false, + }, + }, + }, + }, + }); + + expect(chart.scales.x.min).toBe(2.404e-8); + expect(chart.scales.x.max).toBe(2.4143e-8); + expect(chart.scales.x.ticks[chart.scales.x.ticks.length - 1].value).toBeLessThanOrEqual(2.4143e-8); + }); + it('Should not generate insane amounts of ticks with small stepSize and large range', function() { var chart = window.acquireChart({ type: 'bar',