]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix: don't generate ticks > max if max is specified (#11116)
authorCodingMarco <codingmarco@gmail.com>
Fri, 10 Feb 2023 12:57:58 +0000 (13:57 +0100)
committerGitHub <noreply@github.com>
Fri, 10 Feb 2023 12:57:58 +0000 (07:57 -0500)
* Fix: don't generate ticks > max if max is specified (#11083)

* Add test "Should not generate any ticks > max if max is specified" (#11083)

src/scales/scale.linearbase.js
test/specs/scale.linear.tests.js

index 4c6248d5dc0344a4c45fbeb4e11ddac4c4deac5c..d2da5501eb75f02d04878a4245e3ccad5b1f7ecb 100644 (file)
@@ -123,7 +123,11 @@ function generateTicks(generationOptions, dataRange) {
   }
 
   for (; j < numSpaces; ++j) {
-    ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
+    const tickValue = Math.round((niceMin + j * spacing) * factor) / factor;
+    if (maxDefined && tickValue > max) {
+      break;
+    }
+    ticks.push({value: tickValue});
   }
 
   if (maxDefined && includeBounds && niceMax !== max) {
index 9fbe5467a2d2484d22525107b6b2db2fb3778330..a8ad53995b1f119aa22d7289f0574803bc33f264 100644 (file)
@@ -684,6 +684,28 @@ describe('Linear Scale', function() {
     expect(getLabels(chart.scales.y)).toEqual(['1', '3', '5', '7', '9', '11']);
   });
 
+  it('Should not generate any ticks > max if max is specified', function() {
+    var chart = window.acquireChart({
+      type: 'line',
+      options: {
+        scales: {
+          x: {
+            type: 'linear',
+            min: 2.404e-8,
+            max: 2.4143e-8,
+            ticks: {
+              includeBounds: false,
+            },
+          },
+        },
+      },
+    });
+
+    expect(chart.scales.x.min).toBe(2.404e-8);
+    expect(chart.scales.x.max).toBe(2.4143e-8);
+    expect(chart.scales.x.ticks[chart.scales.x.ticks.length - 1].value).toBeLessThanOrEqual(2.4143e-8);
+  });
+
   it('Should not generate insane amounts of ticks with small stepSize and large range', function() {
     var chart = window.acquireChart({
       type: 'bar',