* Helper function to get the items nearest to the event position for a cartesian chart * @param chart - the chart to look at elements from * @param position - the point to be nearest to, in relative coordinates * @param axis - the axes along which to measure distance * @param [intersect] - if tr
( chart: Chart, position: Point, axis: string, intersect?: boolean, useFinalPosition?: boolean, includeInvisible?: boolean )
| 209 | * @return the nearest items |
| 210 | */ |
| 211 | function getNearestCartesianItems( |
| 212 | chart: Chart, |
| 213 | position: Point, |
| 214 | axis: string, |
| 215 | intersect?: boolean, |
| 216 | useFinalPosition?: boolean, |
| 217 | includeInvisible?: boolean |
| 218 | ): InteractionItem[] { |
| 219 | let items: InteractionItem[] = []; |
| 220 | const distanceMetric = getDistanceMetricForAxis(axis); |
| 221 | let minDistance = Number.POSITIVE_INFINITY; |
| 222 | |
| 223 | function evaluationFunc(element: InteractionElement, datasetIndex: number, index: number) { |
| 224 | const inRange = element.inRange(position.x, position.y, useFinalPosition); |
| 225 | if (intersect && !inRange) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | const areaCenter = getAreaCenter(element, chart.chartArea, useFinalPosition); |
| 230 | if (areaCenter !== undefined) { |
| 231 | // A bar (see getAreaCenter). A branch of its own: sharing the code below with it made |
| 232 | // points allocate a centre object per element and pointer event. |
| 233 | // Measured to its visible part; with no part inside the area, only with includeInvisible. |
| 234 | const barCenter = areaCenter || (includeInvisible ? element.getCenterPoint(useFinalPosition) : null); |
| 235 | if (barCenter) { |
| 236 | addItem(distanceMetric(position, barCenter), element, datasetIndex, index); |
| 237 | } |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | const center = element.getCenterPoint(useFinalPosition); |
| 242 | const pointInArea = !!includeInvisible || chart.isPointInArea(center); |
| 243 | if (!pointInArea && !inRange) { |
| 244 | return; |
| 245 | } |
| 246 | addItem(distanceMetric(position, center), element, datasetIndex, index); |
| 247 | } |
| 248 | |
| 249 | function addItem(distance: number, element: InteractionElement, datasetIndex: number, index: number) { |
| 250 | if (distance < minDistance) { |
| 251 | items = [{element, datasetIndex, index}]; |
| 252 | minDistance = distance; |
| 253 | } else if (distance === minDistance) { |
| 254 | // Can have multiple items at the same distance in which case we sort by size |
| 255 | items.push({element, datasetIndex, index}); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | evaluateInteractionItems(chart, axis, position, evaluationFunc); |
| 260 | return items; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Helper function to get the items nearest to the event position considering all visible items in the chart |
no test coverage detected