import { ScaleBand } from '../../../../scales/scale_band' ; import { GlobalChartState } from '../../../../state/chart_state' ; import { createCustomCachedSelector } from '../../../../state/create_selector' ; import { Dimensions } from '../../../../utils/dimensions' ; import { Point } from '../../../../utils/point' ; import { PrimitiveValue } from '../../../partition_chart/layout/utils/group_by_rollup' ; import { computeChartDimensionsSelector } from './compute_chart_dimensions' ; import { computeSmallMultipleScalesSelector , SmallMultipleScales } from './compute_small_multiple_scales' ; const getCurrentPointerPosition = ( state ) => state . interactions . pointer . current . position ; internal export type PointerPosition = Point & { horizontalPanelValue : PrimitiveValue ; verticalPanelValue : PrimitiveValue ; } ; internal export const getProjectedPointerPositionSelector = createCustomCachedSelector ( [ getCurrentPointerPosition , computeChartDimensionsSelector , computeSmallMultipleScalesSelector ] , ( currentPointerPosition , { chartDimensions } , smallMultipleScales ) : PointerPosition => getProjectedPointerPosition ( currentPointerPosition , chartDimensions , smallMultipleScales ) ) ; param chartAreaPointerPosition param horizontal param vertical param chartAreaDimensions function getProjectedPointerPosition ( chartAreaPointerPosition , { left , top , width , height } , { horizontal , vertical } ) { const { x , y } = chartAreaPointerPosition ; let xPos = x - left ; let yPos = y - top ; if ( xPos < 0 || xPos >= width ) { xPos = - 1 ; } if ( yPos < 0 || yPos >= height ) { yPos = - 1 ; } const h = getPosRelativeToPanel ( horizontal , xPos ) ; const v = getPosRelativeToPanel ( vertical , yPos ) ; return { x : h . pos , y : v . pos , horizontalPanelValue : h . value , verticalPanelValue : v . value , } ; } function getPosRelativeToPanel ( panelScale , pos ) { const outerPadding = panelScale . outerPadding * panelScale . step ; const innerPadding = panelScale . innerPadding * panelScale . step ; const numOfDomainSteps = panelScale . domain . length ; const rangeWithoutOuterPaddings = numOfDomainSteps * panelScale . bandwidth + ( numOfDomainSteps - 1 ) * innerPadding ; if ( pos < outerPadding || pos > outerPadding + rangeWithoutOuterPaddings ) { return { pos : - 1 , value : null } ; } const posWOInitialOuterPadding = pos - outerPadding ; const minEqualSteps = ( numOfDomainSteps - 1 ) * panelScale . step ; if ( posWOInitialOuterPadding <= minEqualSteps ) { const relativePosIndex = Math . floor ( posWOInitialOuterPadding / panelScale . step ) ; const relativePos = posWOInitialOuterPadding - panelScale . step * relativePosIndex ; if ( relativePos > panelScale . bandwidth ) { return { pos : - 1 , value : null } ; } return { pos : relativePos , value : panelScale . domain [ relativePosIndex ] ?? null } ; } return { pos : posWOInitialOuterPadding - panelScale . step * ( numOfDomainSteps - 1 ) , value : panelScale . domain [ numOfDomainSteps - 1 ] ?? null , } ; }