UNPKG

recharts

Version:
97 lines (93 loc) • 3.65 kB
import { createSlice } from '@reduxjs/toolkit'; import { castDraft } from 'immer'; /** * This is the data that's coming through main chart `data` prop * Recharts is very flexible in what it accepts so the type is very flexible too. * This will typically be an object, and various components will provide various `dataKey` * that dictates how to pull data from that object. * * TL;DR: before dataKey * * @inline */ /** * So this is the same unknown type as ChartData but this is after the dataKey has been applied. * We still don't know what the type is - that depends on what exactly it was before the dataKey application, * and the dataKey can return whatever anyway - but let's keep it separate as a form of documentation. * * TL;DR: ChartData after dataKey. */ export var initialChartDataState = { chartData: undefined, computedData: undefined, dataStartIndex: 0, dataEndIndex: 0 }; /** * Brush start and end indexes address the chart-level data array. Their initial value is * `0` and `0`, which is also a valid selection of the first data item, so they can only be * applied when the chart has data of its own. Charts where every graphical item brings its * own data leave the indexes at the initial value, and slicing with them would cut every * item array down to its first entry. * * @param chartDataState the chart data slice state * @returns a `[startIndex, endIndex]` pair, inclusive on both ends */ export function getBrushSliceIndexes(_ref) { var chartData = _ref.chartData, dataStartIndex = _ref.dataStartIndex, dataEndIndex = _ref.dataEndIndex; if (chartData == null || chartData.length === 0) { return [0, Infinity]; } return [dataStartIndex, dataEndIndex]; } var chartDataSlice = createSlice({ name: 'chartData', initialState: initialChartDataState, reducers: { setChartData(state, action) { state.chartData = castDraft(action.payload); if (action.payload == null) { state.dataStartIndex = 0; state.dataEndIndex = 0; return; } if (action.payload.length > 0 && state.dataEndIndex !== action.payload.length - 1) { state.dataEndIndex = action.payload.length - 1; } // A start index from the previous data can point outside the new data. if (action.payload.length > 0) { state.dataStartIndex = Math.min(Math.max(state.dataStartIndex, 0), action.payload.length - 1); } }, setComputedData(state, action) { state.computedData = action.payload; }, setDataStartEndIndexes(state, action) { var _action$payload = action.payload, startIndex = _action$payload.startIndex, endIndex = _action$payload.endIndex; // Keep both indexes inside the data, so a brush range that is out of bounds cannot produce NaN positions. var lastIndex = state.chartData != null && state.chartData.length > 0 ? state.chartData.length - 1 : undefined; var clamp = index => { if (lastIndex == null) { return index; } return Math.min(Math.max(index, 0), lastIndex); }; if (startIndex != null) { state.dataStartIndex = clamp(startIndex); } if (endIndex != null) { state.dataEndIndex = clamp(endIndex); } } } }); var _chartDataSlice$actio = chartDataSlice.actions, setChartData = _chartDataSlice$actio.setChartData, setDataStartEndIndexes = _chartDataSlice$actio.setDataStartEndIndexes, setComputedData = _chartDataSlice$actio.setComputedData; export { setChartData, setDataStartEndIndexes, setComputedData }; export var chartDataReducer = chartDataSlice.reducer;