Skip to content

Commit

Permalink
sort by distance
Browse files Browse the repository at this point in the history
  • Loading branch information
CrokinoleMaster committed Jun 25, 2018
1 parent 3ec8caa commit 28fdbc2
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/core/core.interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,29 @@ module.exports = {
if (options.intersect && !intersectsItem) {
items = [];
}
if (options.onePerDataset) {
var byIndex = {};
items.forEach(function(element) {
if (!byIndex[element._datasetIndex]) {
byIndex[element._datasetIndex] = element;
}
});
return Object.values(byIndex);
}
return items;

// group by dataset
var byIndex = items.reduce(function(res, element) {
if (!res[element._datasetIndex]) {
res[element._datasetIndex] = [element];
} else {
res[element._datasetIndex].push(element);
}
return res;
}, {});

// sort by distance
Object.values(byIndex).forEach(function(elements) {
elements.sort(function(a, b) {
var centerA = a.getCenterPoint();
var centerB = b.getCenterPoint();
var distanceA = distanceMetric(position, centerA);
var distanceB = distanceMetric(position, centerB);
return distanceA - distanceB;
})
});

return Object.values(byIndex).map(els => els[0]);
},

/**
Expand Down

0 comments on commit 28fdbc2

Please sign in to comment.