import { Datum } from '@elastic/charts' ; import { PrimitiveValue } from '@elastic/charts/src/chart_types/partition_chart/layout/utils/group_by_rollup' ; import { mocks } from '@elastic/charts/src/mocks/hierarchical' ; import { discreteColor } from './utils' ; const raw = mocks . observabilityTree ; interface Node { c ? : Node [ ] ; n : string ; v : number ; } type Row = { [ layerKey ] : unknown ; value : number ; depth : number ; } ; const flatTree = ( { c , n , v } , depth ) : Row [ ] => { if ( ! c ) { return [ { [ `layer_${ depth }` ] : n , value : v , depth } ] ; } const childrenRows = c . reduce < Row [ ] > ( ( a , child ) => [ ... a , ... flatTree ( child , depth + 1 ) ] , [ ] ) ; const childrenTotal = childrenRows . reduce ( ( p , { value } ) => p + value , 0 ) ; const missing = Math . max ( 0 , v - childrenTotal ) ; if ( missing > 0 ) { childrenRows . unshift ( { [ `layer_${ depth + 1 }` ] : undefined , value : missing / 2 , depth } ) ; childrenRows . push ( { [ `layer_${ depth + 1 }` ] : undefined , value : missing / 2 , depth } ) ; } childrenRows . forEach ( ( innerChild ) => { innerChild [ `layer_${ depth }` ] = n ; } ) ; return childrenRows ; } ; internal export const getFlatData = ( ) => flatTree ( raw , 0 ) ; internal export const maxDepth = getFlatData ( ) . reduce ( ( p , n ) => Math . max ( p , n . depth ) , 0 ) ; internal export const getLayerSpec = ( color ) => [ ... new Array ( maxDepth + 1 ) ] . map ( ( _ , depth ) => ( { groupByRollup : ( d ) => d [ `layer_${ depth }` ] , nodeLabel : ( d ) => String ( d ) , showAccessor : ( d ) => d !== undefined , shape : { fillColor : ( ) => discreteColor ( color , 0.8 ) ( depth ) , } , } ) ) ;