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

Function nearest

src/plugins/plugin.tooltip.js:57–90  ·  view source on GitHub ↗

* Gets the tooltip position nearest of the item nearest to the event position

(items, eventPosition)

Source from the content-addressed store, hash-verified

55 * Gets the tooltip position nearest of the item nearest to the event position
56 */
57 nearest(items, eventPosition) {
58 if (!items.length) {
59 return false;
60 }
61
62 let x = eventPosition.x;
63 let y = eventPosition.y;
64 let minDistance = Number.POSITIVE_INFINITY;
65 let i, len, nearestElement;
66
67 for (i = 0, len = items.length; i < len; ++i) {
68 const el = items[i].element;
69 if (el && el.hasValue()) {
70 const center = el.getCenterPoint();
71 const d = distanceBetweenPoints(eventPosition, center);
72
73 if (d < minDistance) {
74 minDistance = d;
75 nearestElement = el;
76 }
77 }
78 }
79
80 if (nearestElement) {
81 const tp = nearestElement.tooltipPosition();
82 x = tp.x;
83 y = tp.y;
84 }
85
86 return {
87 x,
88 y
89 };
90 }
91};
92
93// Helper to push or concat based on if the 2nd parameter is an array or not

Callers

nothing calls this directly

Calls 4

distanceBetweenPointsFunction · 0.85
hasValueMethod · 0.80
getCenterPointMethod · 0.65
tooltipPositionMethod · 0.45

Tested by

no test coverage detected