| 1 | import { useEffect } from 'react';
|
|---|
| 2 | import { setChartData, setComputedData } from '../state/chartDataSlice';
|
|---|
| 3 | import { useAppDispatch, useAppSelector } from '../state/hooks';
|
|---|
| 4 | import { useIsPanorama } from './PanoramaContext';
|
|---|
| 5 | export var ChartDataContextProvider = props => {
|
|---|
| 6 | var {
|
|---|
| 7 | chartData
|
|---|
| 8 | } = props;
|
|---|
| 9 | var dispatch = useAppDispatch();
|
|---|
| 10 | var isPanorama = useIsPanorama();
|
|---|
| 11 | useEffect(() => {
|
|---|
| 12 | if (isPanorama) {
|
|---|
| 13 | // Panorama mode reuses data from the main chart, so we must not overwrite it here.
|
|---|
| 14 | return () => {
|
|---|
| 15 | // there is nothing to clean up
|
|---|
| 16 | };
|
|---|
| 17 | }
|
|---|
| 18 | dispatch(setChartData(chartData));
|
|---|
| 19 | return () => {
|
|---|
| 20 | dispatch(setChartData(undefined));
|
|---|
| 21 | };
|
|---|
| 22 | }, [chartData, dispatch, isPanorama]);
|
|---|
| 23 | return null;
|
|---|
| 24 | };
|
|---|
| 25 | export var SetComputedData = props => {
|
|---|
| 26 | var {
|
|---|
| 27 | computedData
|
|---|
| 28 | } = props;
|
|---|
| 29 | var dispatch = useAppDispatch();
|
|---|
| 30 | useEffect(() => {
|
|---|
| 31 | dispatch(setComputedData(computedData));
|
|---|
| 32 | return () => {
|
|---|
| 33 | dispatch(setChartData(undefined));
|
|---|
| 34 | };
|
|---|
| 35 | }, [computedData, dispatch]);
|
|---|
| 36 | return null;
|
|---|
| 37 | };
|
|---|
| 38 | var selectChartData = state => state.chartData.chartData;
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * "data" is the data of the chart - it has no type because this part of recharts is very flexible.
|
|---|
| 42 | * Basically it's an array of "something" and then there's the dataKey property in various places
|
|---|
| 43 | * that's meant to pull other things away from the data.
|
|---|
| 44 | *
|
|---|
| 45 | * Some charts have `data` defined on the chart root, and they will return the array through this hook.
|
|---|
| 46 | * For example: <ComposedChart data={data} />.
|
|---|
| 47 | *
|
|---|
| 48 | * Other charts, such as Pie, have data defined on individual graphical elements.
|
|---|
| 49 | * These charts will return `undefined` through this hook, and you need to read the data from children.
|
|---|
| 50 | * For example: <PieChart><Pie data={data} />
|
|---|
| 51 | *
|
|---|
| 52 | * Some charts also allow setting both - data on the parent, and data on the children at the same time!
|
|---|
| 53 | * However, this particular selector will only return the ones defined on the parent.
|
|---|
| 54 | *
|
|---|
| 55 | * @deprecated use one of the other selectors instead - which one, depends on how do you identify the applicable graphical items.
|
|---|
| 56 | *
|
|---|
| 57 | * @return data array for some charts and undefined for other
|
|---|
| 58 | */
|
|---|
| 59 | export var useChartData = () => useAppSelector(selectChartData);
|
|---|
| 60 | var selectDataIndex = state => {
|
|---|
| 61 | var {
|
|---|
| 62 | dataStartIndex,
|
|---|
| 63 | dataEndIndex
|
|---|
| 64 | } = state.chartData;
|
|---|
| 65 | return {
|
|---|
| 66 | startIndex: dataStartIndex,
|
|---|
| 67 | endIndex: dataEndIndex
|
|---|
| 68 | };
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * startIndex and endIndex are data boundaries, set through Brush.
|
|---|
| 73 | *
|
|---|
| 74 | * @return object with startIndex and endIndex
|
|---|
| 75 | */
|
|---|
| 76 | export var useDataIndex = () => {
|
|---|
| 77 | return useAppSelector(selectDataIndex);
|
|---|
| 78 | }; |
|---|