From: Jukka Kurkela Date: Fri, 24 Sep 2021 22:13:10 +0000 (+0300) Subject: Add sanity check for stepSize (#9679) X-Git-Tag: v3.6.0~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5587738fa4cf86bcd89de09da04a5d245a092bef;p=thirdparty%2FChart.js.git Add sanity check for stepSize (#9679) --- diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index dc00265ee..0930d38f5 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -222,6 +222,10 @@ export default class LinearScaleBase extends Scale { if (stepSize) { maxTicks = Math.ceil(this.max / stepSize) - Math.floor(this.min / stepSize) + 1; + if (maxTicks > 1000) { + console.warn(`scales.${this.id}.ticks.stepSize: ${stepSize} would result generating up to ${maxTicks} ticks. Limiting to 1000.`); + maxTicks = 1000; + } } else { maxTicks = this.computeTickLimit(); maxTicksLimit = maxTicksLimit || 11; diff --git a/test/specs/scale.linear.tests.js b/test/specs/scale.linear.tests.js index e2ad37e41..3307c3962 100644 --- a/test/specs/scale.linear.tests.js +++ b/test/specs/scale.linear.tests.js @@ -639,6 +639,29 @@ describe('Linear Scale', function() { expect(getLabels(chart.scales.y)).toEqual(['1', '3', '5', '7', '9', '11']); }); + it('Should not generate insane amounts of ticks with small stepSize and large range', function() { + var chart = window.acquireChart({ + type: 'bar', + options: { + scales: { + y: { + type: 'linear', + min: 1, + max: 1E10, + ticks: { + stepSize: 2, + autoSkip: false + } + } + } + } + }); + + expect(chart.scales.y.min).toBe(1); + expect(chart.scales.y.max).toBe(1E10); + expect(chart.scales.y.ticks.length).toBeLessThanOrEqual(1000); + }); + it('Should create decimal steps if stepSize is a decimal number', function() { var chart = window.acquireChart({ type: 'bar',