| import { createSelector } from 'reselect'; |
| import { Series } from 'victory-vendor/d3-shape'; |
| import { DataKey, NullableCoordinate } from '../../util/types'; |
| import { BaseValue, computeArea } from '../../cartesian/Area'; |
| import { |
| selectAxisWithScale, |
| selectStackGroups, |
| selectTicksOfGraphicalItem, |
| selectUnfilteredCartesianItems, |
| } from './axisSelectors'; |
| import { RechartsRootState } from '../store'; |
| import { AxisId } from '../cartesianAxisSlice'; |
| import { selectChartLayout } from '../../context/chartLayoutContext'; |
| import { selectChartDataWithIndexesIfNotInPanorama } from './dataSelectors'; |
| import { getBandSizeOfAxis, getNormalizedStackId, isCategoricalAxis, StackId } from '../../util/ChartUtils'; |
| import { ChartData } from '../chartDataSlice'; |
| import { NullablePoint } from '../../shape/Curve'; |
| import { getStackSeriesIdentifier } from '../../util/stacks/getStackSeriesIdentifier'; |
| import { MaybeStackedGraphicalItem } from './barSelectors'; |
| import { StackDataPoint, StackGroup, StackSeriesIdentifier } from '../../util/stacks/stackTypes'; |
|
|
| export interface AreaPointItem extends NullablePoint { |
| x: number | null; |
| y: number | null; |
| value?: number | number[]; |
| payload?: any; |
| } |
| export type AreaSettings = MaybeStackedGraphicalItem & { |
| connectNulls: boolean; |
| baseValue: BaseValue | undefined; |
| dataKey: DataKey<any>; |
| stackId: StackId | undefined; |
| data: ChartData | undefined; |
| }; |
|
|
| export type ComputedArea = { |
| points: ReadonlyArray<AreaPointItem>; |
| baseLine: number | NullableCoordinate[]; |
| isRange: boolean; |
| }; |
|
|
| const selectXAxisWithScale = (state: RechartsRootState, xAxisId: AxisId, _yAxisId: AxisId, isPanorama: boolean) => |
| selectAxisWithScale(state, 'xAxis', xAxisId, isPanorama); |
|
|
| const selectXAxisTicks = (state: RechartsRootState, xAxisId: AxisId, _yAxisId: AxisId, isPanorama: boolean) => |
| selectTicksOfGraphicalItem(state, 'xAxis', xAxisId, isPanorama); |
|
|
| const selectYAxisWithScale = (state: RechartsRootState, _xAxisId: AxisId, yAxisId: AxisId, isPanorama: boolean) => |
| selectAxisWithScale(state, 'yAxis', yAxisId, isPanorama); |
|
|
| const selectYAxisTicks = (state: RechartsRootState, _xAxisId: AxisId, yAxisId: AxisId, isPanorama: boolean) => |
| selectTicksOfGraphicalItem(state, 'yAxis', yAxisId, isPanorama); |
|
|
| const selectBandSize = createSelector( |
| [selectChartLayout, selectXAxisWithScale, selectYAxisWithScale, selectXAxisTicks, selectYAxisTicks], |
| (layout, xAxis, yAxis, xAxisTicks, yAxisTicks) => { |
| if (isCategoricalAxis(layout, 'xAxis')) { |
| return getBandSizeOfAxis(xAxis, xAxisTicks, false); |
| } |
| return getBandSizeOfAxis(yAxis, yAxisTicks, false); |
| }, |
| ); |
|
|
| export const selectGraphicalItemStackedData = ( |
| state: RechartsRootState, |
| xAxisId: AxisId, |
| yAxisId: AxisId, |
| isPanorama: boolean, |
| areaSettings: AreaSettings, |
| ) => { |
| const layout = selectChartLayout(state); |
| const isXAxisCategorical = isCategoricalAxis(layout, 'xAxis'); |
| let stackGroups: Record<StackId, StackGroup> | undefined; |
| if (isXAxisCategorical) { |
| stackGroups = selectStackGroups(state, 'yAxis', yAxisId, isPanorama); |
| } else { |
| stackGroups = selectStackGroups(state, 'xAxis', xAxisId, isPanorama); |
| } |
| if (stackGroups == null) { |
| return undefined; |
| } |
| const { stackId } = areaSettings; |
| const stackSeriesIdentifier: StackSeriesIdentifier | undefined = getStackSeriesIdentifier(areaSettings); |
| if (stackId == null || stackSeriesIdentifier == null) { |
| return undefined; |
| } |
| const groups: ReadonlyArray<Series<StackDataPoint, StackSeriesIdentifier>> = stackGroups[stackId]?.stackedData; |
| return groups?.find(v => v.key === stackSeriesIdentifier); |
| }; |
|
|
| const pickAreaSettings = ( |
| _state: RechartsRootState, |
| _xAxisId: AxisId, |
| _yAxisId: AxisId, |
| _isPanorama: boolean, |
| areaSettings: AreaSettings, |
| ) => areaSettings; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const selectSynchronisedAreaSettings: ( |
| state: RechartsRootState, |
| xAxisId: AxisId, |
| yAxisId: AxisId, |
| isPanorama: boolean, |
| areaSettings: AreaSettings, |
| ) => AreaSettings | undefined = createSelector( |
| [selectUnfilteredCartesianItems, pickAreaSettings], |
| (graphicalItems, areaSettingsFromProps) => { |
| if ( |
| graphicalItems.some( |
| cgis => |
| cgis.type === 'area' && |
| areaSettingsFromProps.dataKey === cgis.dataKey && |
| getNormalizedStackId(areaSettingsFromProps.stackId) === cgis.stackId && |
| areaSettingsFromProps.data === cgis.data, |
| ) |
| ) { |
| |
| |
| |
| |
| |
| |
| |
| |
| return areaSettingsFromProps; |
| } |
| return undefined; |
| }, |
| ); |
|
|
| export const selectArea: ( |
| state: RechartsRootState, |
| xAxisId: AxisId, |
| yAxisId: AxisId, |
| isPanorama: boolean, |
| areaSettings: AreaSettings, |
| ) => ComputedArea | undefined = createSelector( |
| [ |
| selectChartLayout, |
| selectXAxisWithScale, |
| selectYAxisWithScale, |
| selectXAxisTicks, |
| selectYAxisTicks, |
| selectGraphicalItemStackedData, |
| selectChartDataWithIndexesIfNotInPanorama, |
| selectBandSize, |
| selectSynchronisedAreaSettings, |
| ], |
| ( |
| layout, |
| xAxis, |
| yAxis, |
| xAxisTicks, |
| yAxisTicks, |
| stackedData, |
| { chartData, dataStartIndex, dataEndIndex }, |
| bandSize, |
| areaSettings, |
| ) => { |
| if ( |
| areaSettings == null || |
| (layout !== 'horizontal' && layout !== 'vertical') || |
| xAxis == null || |
| yAxis == null || |
| xAxisTicks == null || |
| yAxisTicks == null || |
| xAxisTicks.length === 0 || |
| yAxisTicks.length === 0 || |
| bandSize == null |
| ) { |
| return undefined; |
| } |
| const { data } = areaSettings; |
|
|
| let displayedData: ChartData | undefined; |
| if (data && data.length > 0) { |
| displayedData = data; |
| } else { |
| displayedData = chartData?.slice(dataStartIndex, dataEndIndex + 1); |
| } |
|
|
| if (displayedData == null) { |
| return undefined; |
| } |
|
|
| |
| const chartBaseValue: undefined = undefined; |
|
|
| return computeArea({ |
| layout, |
| xAxis, |
| yAxis, |
| xAxisTicks, |
| yAxisTicks, |
| dataStartIndex, |
| areaSettings, |
| stackedData, |
| displayedData, |
| chartBaseValue, |
| bandSize, |
| }); |
| }, |
| ); |
|
|