From: Mariss Tubelis Date: Sun, 5 Jan 2025 14:42:33 +0000 (+0100) Subject: Bugfix: Binary search wrapper returns non-existing index (#11991) X-Git-Tag: v4.4.8~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97b564b718f06dfc6f01d573b7780fd0f93c8d40;p=thirdparty%2FChart.js.git Bugfix: Binary search wrapper returns non-existing index (#11991) --- diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index 8a7160236..d84318b34 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -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); diff --git a/test/specs/core.interaction.tests.js b/test/specs/core.interaction.tests.js index 9d693f148..ab6377dc9 100644 --- a/test/specs/core.interaction.tests.js +++ b/test/specs/core.interaction.tests.js @@ -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) => {