Skip to content

Commit 8e35eef

Browse files
kurklesimonbrunel
authored andcommitted
Fix nearest interaction mode to return all items (chartjs#5857)
Return all items that are at the nearest distance to the point and add unit tests for nearest + axis: 'x' and nearest + axis: 'y'
1 parent 630f199 commit 8e35eef

File tree

3 files changed

+208
-178
lines changed

3 files changed

+208
-178
lines changed

docs/general/interactions/modes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var chart = new Chart(ctx, {
2020
```
2121

2222
## nearest
23-
Gets the item that is nearest to the point. The nearest item is determined based on the distance to the center of the chart item (point, bar). If 2 or more items are at the same distance, the one with the smallest area is used. If `intersect` is true, this is only triggered when the mouse position intersects an item in the graph. This is very useful for combo charts where points are hidden behind bars.
23+
Gets the items that are at the nearest distance to the point. The nearest item is determined based on the distance to the center of the chart item (point, bar). You can use the `axis` setting to define which directions are used in distance calculation. If `intersect` is true, this is only triggered when the mouse position intersects an item in the graph. This is very useful for combo charts where points are hidden behind bars.
2424

2525
```javascript
2626
var chart = new Chart(ctx, {

src/core/core.interaction.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,7 @@ module.exports = {
243243
var position = getRelativePosition(e, chart);
244244
options.axis = options.axis || 'xy';
245245
var distanceMetric = getDistanceMetricForAxis(options.axis);
246-
var nearestItems = getNearestItems(chart, position, options.intersect, distanceMetric);
247-
248-
// We have multiple items at the same distance from the event. Now sort by smallest
249-
if (nearestItems.length > 1) {
250-
nearestItems.sort(function(a, b) {
251-
var sizeA = a.getArea();
252-
var sizeB = b.getArea();
253-
var ret = sizeA - sizeB;
254-
255-
if (ret === 0) {
256-
// if equal sort by dataset index
257-
ret = a._datasetIndex - b._datasetIndex;
258-
}
259-
260-
return ret;
261-
});
262-
}
263-
264-
// Return only 1 item
265-
return nearestItems.slice(0, 1);
246+
return getNearestItems(chart, position, options.intersect, distanceMetric);
266247
},
267248

268249
/**

0 commit comments

Comments
 (0)