Skip to content

Commit

Permalink
Fix chart crashing when max is defined but ticks are empty (#9641)
Browse files Browse the repository at this point in the history
* Fix chart crashing when max is defined but ticks are empty

* Adding spec to reproduce scale bounds calculation error

Co-authored-by: Kurt Preston <kpreston@drw.com>
  • Loading branch information
KurtPreston and Kurt Preston committed Oct 4, 2021
1 parent 0093b0f commit 60b094a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/scales/scale.linearbase.js
Expand Up @@ -128,7 +128,7 @@ function generateTicks(generationOptions, dataRange) {

if (maxDefined && includeBounds && niceMax !== max) {
// If the previous tick is too close to max, replace it with max, else add max
if (almostEquals(ticks[ticks.length - 1].value, max, relativeLabelSize(max, minSpacing, generationOptions))) {
if (ticks.length && almostEquals(ticks[ticks.length - 1].value, max, relativeLabelSize(max, minSpacing, generationOptions))) {
ticks[ticks.length - 1].value = max;
} else {
ticks.push({value: max});
Expand Down
23 changes: 23 additions & 0 deletions test/specs/scale.linear.tests.js
Expand Up @@ -51,6 +51,29 @@ describe('Linear Scale', function() {
expect(chart.scales.y.max).toBe(150);
});

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

expect(chart.scales.y).not.toEqual(undefined); // must construct
expect(chart.scales.y.max).toBe(150);
});

it('Should correctly determine the max & min of string data values', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down

0 comments on commit 60b094a

Please sign in to comment.