| import { GraphicalItemSettings } from '../../graphicalItemsSlice'; |
| import { ChartDataState } from '../../chartDataSlice'; |
| import { BaseCartesianAxis } from '../../cartesianAxisSlice'; |
| import { getStackSeriesIdentifier } from '../../../util/stacks/getStackSeriesIdentifier'; |
| import { getValueByDataKey } from '../../../util/ChartUtils'; |
| import { StackSeriesIdentifier } from '../../../util/stacks/stackTypes'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export type DisplayedStackedDataPoint = Record<StackSeriesIdentifier, number>; |
|
|
| export type DisplayedStackedData = ReadonlyArray<DisplayedStackedDataPoint>; |
|
|
| export function combineDisplayedStackedData( |
| cartesianItems: ReadonlyArray<GraphicalItemSettings>, |
| { chartData = [] }: ChartDataState, |
| tooltipAxisSettings: BaseCartesianAxis, |
| ): DisplayedStackedData { |
| const tooltipDataKey = tooltipAxisSettings?.dataKey; |
|
|
| |
| const knownItemsByDataKey = new Map<string | number, DisplayedStackedDataPoint>(); |
|
|
| cartesianItems.forEach(item => { |
| |
| const resolvedData = item.data ?? chartData; |
| if (resolvedData == null || resolvedData.length === 0) { |
| |
| return; |
| } |
| const stackIdentifier = getStackSeriesIdentifier(item); |
| resolvedData.forEach((entry, index) => { |
| const tooltipValue = tooltipDataKey == null ? index : String(getValueByDataKey(entry, tooltipDataKey, null)); |
| const numericValue = getValueByDataKey(entry, item.dataKey, 0); |
| let curr; |
| if (knownItemsByDataKey.has(tooltipValue)) { |
| curr = knownItemsByDataKey.get(tooltipValue); |
| } else { |
| curr = {}; |
| } |
| Object.assign(curr, { [stackIdentifier]: numericValue }); |
| knownItemsByDataKey.set(tooltipValue, curr); |
| }); |
| }); |
| const merged = Array.from(knownItemsByDataKey.values()); |
| return merged; |
| } |
|
|