]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix chart crashing when only min is defined (#9718)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 4 Oct 2021 18:00:27 +0000 (21:00 +0300)
committerGitHub <noreply@github.com>
Mon, 4 Oct 2021 18:00:27 +0000 (14:00 -0400)
src/core/core.scale.js
test/specs/scale.linear.tests.js

index 1858078468caac0b67acf647549a427eef2cb98e..68b3c1b1c9c348d16f5245d138188bc86e2b32ad 100644 (file)
@@ -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))
index 9e8a61b6e6ad76f78da5006f5e26694d4da8c9be..77a6dd5e4dd1212c778c985d2d2de53f93eb58bb 100644 (file)
@@ -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',