MCPcopy Create free account
hub / github.com/chartjs/Chart.js / nearest

Function nearest

src/plugins/plugin.tooltip.js:92–126  ·  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

90 * Gets the tooltip position nearest of the item nearest to the event position
91 */
92 nearest(items, eventPosition) {
93 if (!items.length) {
94 return false;
95 }
96
97 const chartArea = getChartArea(this);
98 let x = eventPosition.x;
99 let y = eventPosition.y;
100 let minDistance = Number.POSITIVE_INFINITY;
101 let i, len, nearestElement;
102
103 for (i = 0, len = items.length; i < len; ++i) {
104 const el = items[i].element;
105 if (el && el.hasValue()) {
106 const center = getMeasurePoint(el, chartArea);
107 const d = distanceBetweenPoints(eventPosition, center);
108
109 if (d < minDistance) {
110 minDistance = d;
111 nearestElement = el;
112 }
113 }
114 }
115
116 if (nearestElement) {
117 const tp = getAnchorPosition(nearestElement, chartArea);
118 x = tp.x;
119 y = tp.y;
120 }
121
122 return {
123 x,
124 y
125 };
126 }
127};
128
129// Helper to push or concat based on if the 2nd parameter is an array or not

Callers

nothing calls this directly

Calls 5

getChartAreaFunction · 0.85
getMeasurePointFunction · 0.85
distanceBetweenPointsFunction · 0.85
getAnchorPositionFunction · 0.85
hasValueMethod · 0.80

Tested by

no test coverage detected