From: Kurt Preston Date: Mon, 4 Oct 2021 17:30:16 +0000 (-0500) Subject: Fix chart crashing when max is defined but ticks are empty (#9641) X-Git-Tag: v3.6.0~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60b094a9af087c48f7bfc5ff146f3bbfb29d4add;p=thirdparty%2FChart.js.git Fix chart crashing when max is defined but ticks are empty (#9641) * Fix chart crashing when max is defined but ticks are empty * Adding spec to reproduce scale bounds calculation error Co-authored-by: Kurt Preston --- diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index 0930d38f5..65631a618 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -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}); diff --git a/test/specs/scale.linear.tests.js b/test/specs/scale.linear.tests.js index 3307c3962..9e8a61b6e 100644 --- a/test/specs/scale.linear.tests.js +++ b/test/specs/scale.linear.tests.js @@ -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',