import { extent } from 'd3-array' ; import { getPredicateFn } from '../../../../common/predicate' ; import { ScaleType } from '../../../../scales/constants' ; import { createCustomCachedSelector } from '../../../../state/create_selector' ; import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs' ; import { getAccessorValue } from '../../../../utils/accessor' ; import { addIntervalToTime , timeRange } from '../../../../utils/chrono/elasticsearch' ; import { isFiniteNumber } from '../../../../utils/common' ; import { HeatmapTable } from './compute_chart_dimensions' ; import { getHeatmapSpecSelector } from './get_heatmap_spec' ; internal export const getHeatmapTableSelector = createCustomCachedSelector ( [ getHeatmapSpecSelector , getSettingsSpecSelector ] , ( { data , valueAccessor , xAccessor , yAccessor , xSortPredicate , ySortPredicate , xScale , timeZone } , { xDomain } ) : HeatmapTable => { const resultData = data . reduce < HeatmapTable > ( ( acc , curr , index ) => { const x = getAccessorValue ( curr , xAccessor ) ; const y = getAccessorValue ( curr , yAccessor ) ; const value = getAccessorValue ( curr , valueAccessor ) ; const [ min , max ] = acc . extent ; acc . extent = [ Math . min ( min , value ) , Math . max ( max , value ) ] ; acc . table . push ( { x , y , value , originalIndex : index , } ) ; if ( ! acc . xValues . includes ( x ) ) { acc . xValues . push ( x ) ; } if ( ! acc . yValues . includes ( y ) ) { acc . yValues . push ( y ) ; } return acc ; } , { table : [ ] , xValues : [ ] , yValues : [ ] , extent : [ + Infinity , - Infinity ] , xNumericExtent : [ + Infinity , - Infinity ] , } ) ; if ( xScale . type === ScaleType . Time ) { const [ xDataMin = NaN , xDataMax = NaN ] = extent ( resultData . xValues as number [ ] ) ; const dataMaxExtended = xDataMax ? addIntervalToTime ( xDataMax , xScale . interval , timeZone ) : NaN ; const [ customMin , customMax ] = ! Array . isArray ( xDomain ) ? [ xDomain ?. min ?? NaN , xDomain ?. max ?? NaN ] : [ NaN , NaN ] ; const [ min , max ] = extent ( [ xDataMin , customMin , customMax , dataMaxExtended ] ) ; resultData . xNumericExtent = [ min ?? NaN , max ?? NaN ] ; resultData . xValues = isFiniteNumber ( min ) && isFiniteNumber ( max ) ? timeRange ( min , max , xScale . interval , timeZone ) : [ ] ; } else if ( xScale . type === ScaleType . Ordinal ) { resultData . xValues . sort ( getPredicateFn ( xSortPredicate ) ) ; } resultData . yValues . sort ( getPredicateFn ( ySortPredicate ) ) ; return resultData ; } ) ;