From: Jukka Kurkela Date: Mon, 14 Sep 2020 15:36:35 +0000 (+0300) Subject: Merge x/y interaction mode functions (#7794) X-Git-Tag: v3.0.0-beta.2~1^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2563ff174e7f3df01f3aa7ec66c645774e4bda36;p=thirdparty%2FChart.js.git Merge x/y interaction mode functions (#7794) --- diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index 9b130769d..6ee310623 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -180,6 +180,31 @@ function getNearestItems(chart, position, axis, intersect, useFinalPosition) { return items; } +function getAxisItems(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const items = []; + const axis = options.axis; + const rangeMethod = axis === 'x' ? 'inXRange' : 'inYRange'; + let intersectsItem = false; + + evaluateAllVisibleItems(chart, (element, datasetIndex, index) => { + if (element[rangeMethod](position[axis], useFinalPosition)) { + items.push({element, datasetIndex, index}); + } + + if (element.inRange(position.x, position.y, useFinalPosition)) { + intersectsItem = true; + } + }); + + // If we want to trigger on an intersect and we don't have any items + // that intersect the position, return nothing + if (options.intersect && !intersectsItem) { + return []; + } + return items; +} + /** * Contains interaction related functions * @namespace Chart.Interaction @@ -294,26 +319,8 @@ export default { * @return {InteractionItem[]} - items that are found */ x(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const items = []; - let intersectsItem = false; - - evaluateAllVisibleItems(chart, (element, datasetIndex, index) => { - if (element.inXRange(position.x, useFinalPosition)) { - items.push({element, datasetIndex, index}); - } - - if (element.inRange(position.x, position.y, useFinalPosition)) { - intersectsItem = true; - } - }); - - // If we want to trigger on an intersect and we don't have any items - // that intersect the position, return nothing - if (options.intersect && !intersectsItem) { - return []; - } - return items; + options.axis = 'x'; + return getAxisItems(chart, e, options, useFinalPosition); }, /** @@ -326,26 +333,8 @@ export default { * @return {InteractionItem[]} - items that are found */ y(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const items = []; - let intersectsItem = false; - - evaluateAllVisibleItems(chart, (element, datasetIndex, index) => { - if (element.inYRange(position.y, useFinalPosition)) { - items.push({element, datasetIndex, index}); - } - - if (element.inRange(position.x, position.y, useFinalPosition)) { - intersectsItem = true; - } - }); - - // If we want to trigger on an intersect and we don't have any items - // that intersect the position, return nothing - if (options.intersect && !intersectsItem) { - return []; - } - return items; + options.axis = 'y'; + return getAxisItems(chart, e, options, useFinalPosition); } } };