|
|
import { useEffect } from 'react'; |
|
|
import { CartesianViewBoxRequired, ChartOffsetInternal, LayoutType, Margin, Size } from '../util/types'; |
|
|
import { useAppDispatch, useAppSelector } from '../state/hooks'; |
|
|
import { RechartsRootState } from '../state/store'; |
|
|
import { setChartSize, setMargin } from '../state/layoutSlice'; |
|
|
import { selectChartOffsetInternal, selectChartViewBox } from '../state/selectors/selectChartOffsetInternal'; |
|
|
import { selectChartHeight, selectChartWidth } from '../state/selectors/containerSelectors'; |
|
|
import { useIsPanorama } from './PanoramaContext'; |
|
|
import { selectBrushDimensions, selectBrushSettings } from '../state/selectors/brushSelectors'; |
|
|
|
|
|
export const useViewBox = (): CartesianViewBoxRequired | undefined => { |
|
|
const panorama = useIsPanorama(); |
|
|
const rootViewBox = useAppSelector(selectChartViewBox); |
|
|
const brushDimensions = useAppSelector(selectBrushDimensions); |
|
|
const brushPadding = useAppSelector(selectBrushSettings)?.padding; |
|
|
if (!panorama || !brushDimensions || !brushPadding) { |
|
|
return rootViewBox; |
|
|
} |
|
|
return { |
|
|
width: brushDimensions.width - brushPadding.left - brushPadding.right, |
|
|
height: brushDimensions.height - brushPadding.top - brushPadding.bottom, |
|
|
x: brushPadding.left, |
|
|
y: brushPadding.top, |
|
|
}; |
|
|
}; |
|
|
|
|
|
const manyComponentsThrowErrorsIfOffsetIsUndefined: ChartOffsetInternal = { |
|
|
top: 0, |
|
|
bottom: 0, |
|
|
left: 0, |
|
|
right: 0, |
|
|
width: 0, |
|
|
height: 0, |
|
|
brushBottom: 0, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useOffsetInternal = (): ChartOffsetInternal => { |
|
|
return useAppSelector(selectChartOffsetInternal) ?? manyComponentsThrowErrorsIfOffsetIsUndefined; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useChartWidth = (): number | undefined => { |
|
|
return useAppSelector(selectChartWidth); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useChartHeight = (): number | undefined => { |
|
|
return useAppSelector(selectChartHeight); |
|
|
}; |
|
|
|
|
|
const manyComponentsThrowErrorsIfMarginIsUndefined: Margin = { |
|
|
top: 0, |
|
|
right: 0, |
|
|
bottom: 0, |
|
|
left: 0, |
|
|
}; |
|
|
export const useMargin = (): Margin => { |
|
|
return useAppSelector(state => state.layout.margin) ?? manyComponentsThrowErrorsIfMarginIsUndefined; |
|
|
}; |
|
|
|
|
|
export const selectChartLayout = (state: RechartsRootState): LayoutType => state.layout.layoutType; |
|
|
|
|
|
export const useChartLayout = () => useAppSelector(selectChartLayout); |
|
|
|
|
|
export const ReportChartSize = (props: Size): null => { |
|
|
const dispatch = useAppDispatch(); |
|
|
|
|
|
useEffect(() => { |
|
|
dispatch(setChartSize(props)); |
|
|
}, [dispatch, props]); |
|
|
|
|
|
return null; |
|
|
}; |
|
|
|
|
|
export const ReportChartMargin = ({ margin }: { margin: Margin }): null => { |
|
|
const dispatch = useAppDispatch(); |
|
|
useEffect(() => { |
|
|
dispatch(setMargin(margin)); |
|
|
}, [dispatch, margin]); |
|
|
return null; |
|
|
}; |
|
|
|