import { groupBy , GroupKeysOrKeyFn } from '../../chart_types/xy_chart/utils/group_data_series' ; import { isFiniteNumber } from '../common' ; remarks public param data param groupAccessors param valueGetterSetters export function computeRatioByGroups < T extends Record < string , unknown > > ( data , groupAccessors , valueGetterSetters ) { return groupBy ( data , groupAccessors , true ) . map ( ( groupedData ) => { const groupSum = groupedData . reduce ( ( sum , datum ) => { return ( valueGetterSetters . reduce ( ( valueSum , [ getter ] ) => { const value = getter ( datum ) ; return valueSum + ( isFiniteNumber ( value ) ? Math . abs ( value ) : 0 ) ; } , 0 ) + sum ) ; } , 0 ) ; return groupedData . map ( ( datum ) => { return valueGetterSetters . reduce < T > ( ( acc , [ getter , setter ] ) => { const value = getter ( acc ) ; return isFiniteNumber ( value ) ? setter ( acc , groupSum === 0 ? 0 : Math . abs ( value ) / groupSum ) : acc ; } , datum ) ; } ) ; } ) . flat ( ) ; }