|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
|
|
import { ErrorBarDirection } from '../cartesian/ErrorBar'; |
|
|
import { DataKey } from '../util/types'; |
|
|
import { GraphicalItemId } from './graphicalItemsSlice'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ErrorBarsSettings = { |
|
|
|
|
|
|
|
|
|
|
|
direction: ErrorBarDirection; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataKey: DataKey<any>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
export type ErrorBarsState = Record<GraphicalItemId, ReadonlyArray<ErrorBarsSettings>>; |
|
|
|
|
|
const initialState: ErrorBarsState = {}; |
|
|
|
|
|
const errorBarSlice = createSlice({ |
|
|
name: 'errorBars', |
|
|
initialState, |
|
|
reducers: { |
|
|
addErrorBar: (state, action: PayloadAction<{ itemId: GraphicalItemId; errorBar: ErrorBarsSettings }>) => { |
|
|
const { itemId, errorBar } = action.payload; |
|
|
if (!state[itemId]) { |
|
|
state[itemId] = []; |
|
|
} |
|
|
state[itemId].push(errorBar); |
|
|
}, |
|
|
removeErrorBar: (state, action: PayloadAction<{ itemId: GraphicalItemId; errorBar: ErrorBarsSettings }>) => { |
|
|
const { itemId, errorBar } = action.payload; |
|
|
if (state[itemId]) { |
|
|
state[itemId] = state[itemId].filter(e => e.dataKey !== errorBar.dataKey || e.direction !== errorBar.direction); |
|
|
} |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
|
|
|
export const { addErrorBar, removeErrorBar } = errorBarSlice.actions; |
|
|
|
|
|
export const errorBarReducer = errorBarSlice.reducer; |
|
|
|