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

Fix chart crashing when only min is defined #9718

Merged
merged 1 commit into from Oct 4, 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/core/core.scale.js
Expand Up @@ -323,6 +323,10 @@ export default class Scale extends Element {
}
}

// Make sure min <= max when only min or max is defined by user and the data is outside that range
min = maxDefined && min > max ? max : min;
max = minDefined && min > max ? min : max;

return {
min: finiteOrDefault(min, finiteOrDefault(max, min)),
max: finiteOrDefault(max, finiteOrDefault(min, max))
Expand Down
22 changes: 22 additions & 0 deletions test/specs/scale.linear.tests.js
Expand Up @@ -51,6 +51,28 @@ describe('Linear Scale', function() {
expect(chart.scales.y.max).toBe(150);
});

it('Should handle when only a min value is provided', () => {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
yAxisID: 'y',
data: [200]
}],
},
options: {
scales: {
y: {
type: 'linear',
min: 250
}
}
}
});

expect(chart.scales.y.min).toBe(250);
});

it('Should handle when only a max value is provided', () => {
var chart = window.acquireChart({
type: 'line',
Expand Down