]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add sanity check for stepSize (#9679)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 24 Sep 2021 22:13:10 +0000 (01:13 +0300)
committerGitHub <noreply@github.com>
Fri, 24 Sep 2021 22:13:10 +0000 (18:13 -0400)
src/scales/scale.linearbase.js
test/specs/scale.linear.tests.js

index dc00265ee194b7cc95ab243d25bb1f5c7c546a04..0930d38f5549add0419adfe0b20d2d568a5fdf80 100644 (file)
@@ -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;
index e2ad37e41a20d5948325ca8d38c89faad210b315..3307c39621af2e9e9fb0a4d171d3913c1407c45a 100644 (file)
@@ -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',