Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sanity check for stepSize #9679

Merged
merged 1 commit into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/scales/scale.linearbase.js
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions test/specs/scale.linear.tests.js
Expand Up @@ -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',
Expand Down