MCPcopy
hub / github.com/chartjs/Chart.js / getNearestCartesianItems

Function getNearestCartesianItems

src/core/core.interaction.js:168–197  ·  view source on GitHub ↗

* Helper function to get the items nearest to the event position for a cartesian chart * @param {Chart} chart - the chart to look at elements from * @param {Point} position - the point to be nearest to, in relative coordinates * @param {string} axis - the axes along which to measure distance * @

(chart, position, axis, intersect, useFinalPosition, includeInvisible)

Source from the content-addressed store, hash-verified

166 * @return {InteractionItem[]} the nearest items
167 */
168function getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) {
169 let items = [];
170 const distanceMetric = getDistanceMetricForAxis(axis);
171 let minDistance = Number.POSITIVE_INFINITY;
172
173 function evaluationFunc(element, datasetIndex, index) {
174 const inRange = element.inRange(position.x, position.y, useFinalPosition);
175 if (intersect && !inRange) {
176 return;
177 }
178
179 const center = element.getCenterPoint(useFinalPosition);
180 const pointInArea = !!includeInvisible || chart.isPointInArea(center);
181 if (!pointInArea && !inRange) {
182 return;
183 }
184
185 const distance = distanceMetric(position, center);
186 if (distance < minDistance) {
187 items = [{element, datasetIndex, index}];
188 minDistance = distance;
189 } else if (distance === minDistance) {
190 // Can have multiple items at the same distance in which case we sort by size
191 items.push({element, datasetIndex, index});
192 }
193 }
194
195 evaluateInteractionItems(chart, axis, position, evaluationFunc);
196 return items;
197}
198
199/**
200 * Helper function to get the items nearest to the event position considering all visible items in the chart

Callers 1

getNearestItemsFunction · 0.85

Calls 2

getDistanceMetricForAxisFunction · 0.85
evaluateInteractionItemsFunction · 0.85

Tested by

no test coverage detected