]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Bugfix: Binary search wrapper returns non-existing index (#11991)
authorMariss Tubelis <mariss@mariss.no>
Sun, 5 Jan 2025 14:42:33 +0000 (15:42 +0100)
committerGitHub <noreply@github.com>
Sun, 5 Jan 2025 14:42:33 +0000 (09:42 -0500)
src/core/core.interaction.js
test/specs/core.interaction.tests.js

index 8a71602365162d94a329400e7ede2eeae70bc378..d84318b34d5cb1ae45812eb2dd52059a77dccaf9 100644 (file)
@@ -40,7 +40,7 @@ function binarySearch(metaset, axis, value, intersect) {
         result.lo -= Math.max(0, distanceToDefinedLo);
 
         const distanceToDefinedHi = (_parsed
-          .slice(result.hi - 1)
+          .slice(result.hi)
           .findIndex(
             point => !isNullOrUndef(point[vScale.axis])));
         result.hi += Math.max(0, distanceToDefinedHi);
index 9d693f1488c635c4a533be2d602ea4006aed98a6..ab6377dc94d3b66e56e145941abd162a7dadbf0e 100644 (file)
@@ -972,6 +972,26 @@ describe('Core.Interaction', function() {
       data: [12, -1, null, null, null, null, -1, 2],
       clickPointIndex: 4,
       expectedNearestPointIndex: 6
+    },
+    {
+      data: [null, 2],
+      clickPointIndex: 0,
+      expectedNearestPointIndex: 1
+    },
+    {
+      data: [2, null],
+      clickPointIndex: 1,
+      expectedNearestPointIndex: 0
+    },
+    {
+      data: [null, null, 2],
+      clickPointIndex: 0,
+      expectedNearestPointIndex: 2
+    },
+    {
+      data: [2, null, null],
+      clickPointIndex: 2,
+      expectedNearestPointIndex: 0
     }
   ];
   testCases.forEach(({data, clickPointIndex, expectedNearestPointIndex}, i) => {