@@ -5,7 +5,8 @@ import {_lookupByKey, _rlookupByKey} from '../helpers/helpers.collection';
55/**
66 * @typedef { import("./core.controller").default } Chart
77 * @typedef { import("../platform/platform.base").IEvent } IEvent
8- * @typedef {{axis?:'x'|'y'|'xy', intersect:boolean} } IInteractionOptions
8+ * @typedef {{axis?: string, intersect?: boolean} } InteractionOptions
9+ * @typedef {{datasetIndex: number, index: number, element: import("../core/core.element").default} } InteractionItem
910 */
1011
1112/**
@@ -121,17 +122,18 @@ function getDistanceMetricForAxis(axis) {
121122 * @param {Chart } chart - the chart
122123 * @param {object } position - the point to be nearest to
123124 * @param {string } axis - the axis mode. x|y|xy
124- * @return {object[] } the nearest items
125+ * @param {boolean } [useFinalPosition] - use the element's animation target instead of current position
126+ * @return {InteractionItem[] } the nearest items
125127 */
126- function getIntersectItems ( chart , position , axis ) {
128+ function getIntersectItems ( chart , position , axis , useFinalPosition ) {
127129 const items = [ ] ;
128130
129131 if ( ! _isPointInArea ( position , chart . chartArea ) ) {
130132 return items ;
131133 }
132134
133135 const evaluationFunc = function ( element , datasetIndex , index ) {
134- if ( element . inRange ( position . x , position . y ) ) {
136+ if ( element . inRange ( position . x , position . y , useFinalPosition ) ) {
135137 items . push ( { element, datasetIndex, index} ) ;
136138 }
137139 } ;
@@ -146,9 +148,10 @@ function getIntersectItems(chart, position, axis) {
146148 * @param {object } position - the point to be nearest to
147149 * @param {string } axis - the axes along which to measure distance
148150 * @param {boolean } [intersect] - if true, only consider items that intersect the position
149- * @return {object[] } the nearest items
151+ * @param {boolean } [useFinalPosition] - use the elements animation target instead of current position
152+ * @return {InteractionItem[] } the nearest items
150153 */
151- function getNearestItems ( chart , position , axis , intersect ) {
154+ function getNearestItems ( chart , position , axis , intersect , useFinalPosition ) {
152155 const distanceMetric = getDistanceMetricForAxis ( axis ) ;
153156 let minDistance = Number . POSITIVE_INFINITY ;
154157 let items = [ ] ;
@@ -158,11 +161,11 @@ function getNearestItems(chart, position, axis, intersect) {
158161 }
159162
160163 const evaluationFunc = function ( element , datasetIndex , index ) {
161- if ( intersect && ! element . inRange ( position . x , position . y ) ) {
164+ if ( intersect && ! element . inRange ( position . x , position . y , useFinalPosition ) ) {
162165 return ;
163166 }
164167
165- const center = element . getCenterPoint ( ) ;
168+ const center = element . getCenterPoint ( useFinalPosition ) ;
166169 const distance = distanceMetric ( position , center ) ;
167170 if ( distance < minDistance ) {
168171 items = [ { element, datasetIndex, index} ] ;
@@ -191,14 +194,17 @@ export default {
191194 * @since v2.4.0
192195 * @param {Chart } chart - the chart we are returning items from
193196 * @param {Event } e - the event we are find things at
194- * @param {IInteractionOptions } options - options to use during interaction
195- * @return {Object[] } Array of elements that are under the point. If none are found, an empty array is returned
197+ * @param {InteractionOptions } options - options to use
198+ * @param {boolean } [useFinalPosition] - use final element position (animation target)
199+ * @return {InteractionItem[] } - items that are found
196200 */
197- index ( chart , e , options ) {
201+ index ( chart , e , options , useFinalPosition ) {
198202 const position = getRelativePosition ( e , chart ) ;
199203 // Default axis for index mode is 'x' to match old behaviour
200204 const axis = options . axis || 'x' ;
201- const items = options . intersect ? getIntersectItems ( chart , position , axis ) : getNearestItems ( chart , position , axis ) ;
205+ const items = options . intersect
206+ ? getIntersectItems ( chart , position , axis , useFinalPosition )
207+ : getNearestItems ( chart , position , axis , false , useFinalPosition ) ;
202208 const elements = [ ] ;
203209
204210 if ( ! items . length ) {
@@ -224,13 +230,16 @@ export default {
224230 * @function Chart.Interaction.modes.dataset
225231 * @param {Chart } chart - the chart we are returning items from
226232 * @param {Event } e - the event we are find things at
227- * @param {IInteractionOptions } options - options to use during interaction
228- * @return {Object[] } Array of elements that are under the point. If none are found, an empty array is returned
233+ * @param {InteractionOptions } options - options to use
234+ * @param {boolean } [useFinalPosition] - use final element position (animation target)
235+ * @return {InteractionItem[] } - items that are found
229236 */
230- dataset ( chart , e , options ) {
237+ dataset ( chart , e , options , useFinalPosition ) {
231238 const position = getRelativePosition ( e , chart ) ;
232239 const axis = options . axis || 'xy' ;
233- let items = options . intersect ? getIntersectItems ( chart , position , axis ) : getNearestItems ( chart , position , axis ) ;
240+ let items = options . intersect
241+ ? getIntersectItems ( chart , position , axis , useFinalPosition ) :
242+ getNearestItems ( chart , position , axis , false , useFinalPosition ) ;
234243
235244 if ( items . length > 0 ) {
236245 const datasetIndex = items [ 0 ] . datasetIndex ;
@@ -250,48 +259,51 @@ export default {
250259 * @function Chart.Interaction.modes.intersect
251260 * @param {Chart } chart - the chart we are returning items from
252261 * @param {Event } e - the event we are find things at
253- * @param {IInteractionOptions } options - options to use
254- * @return {Object[] } Array of elements that are under the point. If none are found, an empty array is returned
262+ * @param {InteractionOptions } options - options to use
263+ * @param {boolean } [useFinalPosition] - use final element position (animation target)
264+ * @return {InteractionItem[] } - items that are found
255265 */
256- point ( chart , e , options ) {
266+ point ( chart , e , options , useFinalPosition ) {
257267 const position = getRelativePosition ( e , chart ) ;
258268 const axis = options . axis || 'xy' ;
259- return getIntersectItems ( chart , position , axis ) ;
269+ return getIntersectItems ( chart , position , axis , useFinalPosition ) ;
260270 } ,
261271
262272 /**
263273 * nearest mode returns the element closest to the point
264274 * @function Chart.Interaction.modes.intersect
265275 * @param {Chart } chart - the chart we are returning items from
266276 * @param {Event } e - the event we are find things at
267- * @param {IInteractionOptions } options - options to use
268- * @return {Object[] } Array of elements that are under the point. If none are found, an empty array is returned
277+ * @param {InteractionOptions } options - options to use
278+ * @param {boolean } [useFinalPosition] - use final element position (animation target)
279+ * @return {InteractionItem[] } - items that are found
269280 */
270- nearest ( chart , e , options ) {
281+ nearest ( chart , e , options , useFinalPosition ) {
271282 const position = getRelativePosition ( e , chart ) ;
272283 const axis = options . axis || 'xy' ;
273- return getNearestItems ( chart , position , axis , options . intersect ) ;
284+ return getNearestItems ( chart , position , axis , options . intersect , useFinalPosition ) ;
274285 } ,
275286
276287 /**
277288 * x mode returns the elements that hit-test at the current x coordinate
278289 * @function Chart.Interaction.modes.x
279290 * @param {Chart } chart - the chart we are returning items from
280291 * @param {Event } e - the event we are find things at
281- * @param {IInteractionOptions } options - options to use
282- * @return {Object[] } Array of elements that are under the point. If none are found, an empty array is returned
292+ * @param {InteractionOptions } options - options to use
293+ * @param {boolean } [useFinalPosition] - use final element position (animation target)
294+ * @return {InteractionItem[] } - items that are found
283295 */
284- x ( chart , e , options ) {
296+ x ( chart , e , options , useFinalPosition ) {
285297 const position = getRelativePosition ( e , chart ) ;
286298 const items = [ ] ;
287299 let intersectsItem = false ;
288300
289301 evaluateAllVisibleItems ( chart , ( element , datasetIndex , index ) => {
290- if ( element . inXRange ( position . x ) ) {
302+ if ( element . inXRange ( position . x , useFinalPosition ) ) {
291303 items . push ( { element, datasetIndex, index} ) ;
292304 }
293305
294- if ( element . inRange ( position . x , position . y ) ) {
306+ if ( element . inRange ( position . x , position . y , useFinalPosition ) ) {
295307 intersectsItem = true ;
296308 }
297309 } ) ;
@@ -309,20 +321,21 @@ export default {
309321 * @function Chart.Interaction.modes.y
310322 * @param {Chart } chart - the chart we are returning items from
311323 * @param {Event } e - the event we are find things at
312- * @param {IInteractionOptions } options - options to use
313- * @return {Object[] } Array of elements that are under the point. If none are found, an empty array is returned
324+ * @param {InteractionOptions } options - options to use
325+ * @param {boolean } [useFinalPosition] - use final element position (animation target)
326+ * @return {InteractionItem[] } - items that are found
314327 */
315- y ( chart , e , options ) {
328+ y ( chart , e , options , useFinalPosition ) {
316329 const position = getRelativePosition ( e , chart ) ;
317330 const items = [ ] ;
318331 let intersectsItem = false ;
319332
320333 evaluateAllVisibleItems ( chart , ( element , datasetIndex , index ) => {
321- if ( element . inYRange ( position . y ) ) {
334+ if ( element . inYRange ( position . y , useFinalPosition ) ) {
322335 items . push ( { element, datasetIndex, index} ) ;
323336 }
324337
325- if ( element . inRange ( position . x , position . y ) ) {
338+ if ( element . inRange ( position . x , position . y , useFinalPosition ) ) {
326339 intersectsItem = true ;
327340 }
328341 } ) ;
0 commit comments