]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix chart crashing when max is defined but ticks are empty (#9641)
authorKurt Preston <github@kurtpreston.com>
Mon, 4 Oct 2021 17:30:16 +0000 (12:30 -0500)
committerGitHub <noreply@github.com>
Mon, 4 Oct 2021 17:30:16 +0000 (20:30 +0300)
* 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>
src/scales/scale.linearbase.js
test/specs/scale.linear.tests.js

index 0930d38f5549add0419adfe0b20d2d568a5fdf80..65631a618a18c22b2dfaeec67d6e05d1c0cbafdc 100644 (file)
@@ -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});
index 3307c39621af2e9e9fb0a4d171d3913c1407c45a..9e8a61b6e6ad76f78da5006f5e26694d4da8c9be 100644 (file)
@@ -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',