import { LegendItemExtraValues } from '../../../../common/legend' ; import { SeriesKey } from '../../../../common/series_id' ; import { Relation } from '../../../../common/text_utils' ; import { LegendPath } from '../../../../state/actions/legend' ; import { IndexedAccessorFn } from '../../../../utils/accessor' ; import { Datum , ValueAccessor , ValueFormatter } from '../../../../utils/common' ; import { Layer } from '../../specs' ; import { PartitionLayout } from '../types/config_types' ; import { aggregators , CHILDREN_KEY , groupByRollup , HIERARCHY_ROOT_KEY , HierarchyOfArrays , mapEntryValue , mapsToArrays , NodeSorter , Sorter , } from '../utils/group_by_rollup' ; import { isMosaic , isSunburst , isTreemap , isWaffle } from './viewmodel' ; function aggregateComparator ( accessor , sorter ) { return ( a , b ) => sorter ( accessor ( a ) , accessor ( b ) ) ; } const ascending = ( a , b ) => a - b ; const descending = ( a , b ) => b - a ; const childOrders = { ascending , descending , } ; const descendingValueNodes = aggregateComparator ( mapEntryValue , childOrders . descending ) ; const ascendingValueNodes = aggregateComparator ( mapEntryValue , childOrders . ascending ) ; internal export function getHierarchyOfArrays ( rawFacts , valueAccessor , groupByRollupAccessors , sortSpecs , innerGroups ) { const aggregator = aggregators . sum ; const facts = rawFacts . filter ( ( n ) => { const value = valueAccessor ( n ) ; return Number . isFinite ( value ) && value > 0 ; } ) ; if ( facts . reduce ( ( p , n ) => aggregator . reducer ( p , valueAccessor ( n ) ) , aggregator . identity ( ) ) <= 0 ) { return [ ] ; } return mapsToArrays ( groupByRollup ( groupByRollupAccessors , valueAccessor , aggregator , facts ) , sortSpecs , innerGroups ) ; } const sorter = ( layout ) => ( { sortPredicate } , i ) => sortPredicate || ( isTreemap ( layout ) || isSunburst ( layout ) || isWaffle ( layout ) ? descendingValueNodes : isMosaic ( layout ) ? i === 2 ? ascendingValueNodes : descendingValueNodes : null ) ; internal export function partitionTree ( data , valueAccessor , layers , layout , innerGroups ) { return getHierarchyOfArrays ( data , valueAccessor , [ ( ) => HIERARCHY_ROOT_KEY , ... layers . map ( ( { groupByRollup } ) => groupByRollup ) ] , [ null , ... layers . map ( sorter ( layout ) ) ] , innerGroups ) ; } internal export function getExtraValueMap ( layers , valueFormatter , tree , maxDepth , depth = 0 , keys = new Map ( ) ) { for ( let i = 0 ; i < tree . length ; i ++ ) { const branch = tree [ i ] ; const [ key , arrayNode ] = branch ; const { value , path , [ CHILDREN_KEY ] : children } = arrayNode ; const values = new Map ( ) ; const formattedValue = valueFormatter ? valueFormatter ( value ) : value ; values . set ( key , formattedValue ) ; keys . set ( path . map ( ( { index } ) => index ) . join ( '__' ) , values ) ; if ( depth < maxDepth ) getExtraValueMap ( layers , valueFormatter , children , maxDepth , depth + 1 , keys ) ; } return keys ; }