@mui/x-charts
Version:
The community edition of MUI X Charts components.
32 lines (31 loc) • 2.04 kB
JavaScript
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage";
import * as React from 'react';
import { selectorChartPolarCenter } from "../../internals/plugins/featurePlugins/useChartPolarAxis/index.mjs";
import { getChartPoint } from "../../internals/getChartPoint.mjs";
import { generateSvg2rotation } from "../../internals/plugins/featurePlugins/useChartPolarAxis/coordinateTransformation.mjs";
import { getAxisIndex } from "../../internals/plugins/featurePlugins/useChartPolarAxis/getAxisIndex.mjs";
import { useStore } from "../../internals/store/useStore.mjs";
import { useChartsLayerContainerRef } from "../../hooks/useChartsLayerContainerRef.mjs";
import { useRotationAxis } from "../../hooks/useAxis.mjs";
/**
* This hook provides a function that from pointer event returns the rotation index.
* @return {(event: { clientX: number; clientY: number }) => number | null} rotationIndexGetter Returns the rotation data index.
*/
export function useRadarRotationIndex() {
const chartsLayerContainerRef = useChartsLayerContainerRef();
const store = useStore();
const rotationAxis = useRotationAxis();
const center = store.use(selectorChartPolarCenter);
const rotationIndexGetter = React.useCallback(function rotationIndexGetter(event) {
const element = chartsLayerContainerRef.current;
if (!element || !rotationAxis) {
// Should never append
throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Charts: The ${!element ? 'SVG element' : 'rotation axis'} was not found. ` + 'This is required to compute the radar chart dataIndex. ' + 'Ensure the radar chart is properly initialized with all required axes.' : _formatErrorMessage(28, !element ? 'SVG element' : 'rotation axis'));
}
const svgPoint = getChartPoint(element, event);
const rotation = generateSvg2rotation(center)(svgPoint.x, svgPoint.y);
const rotationIndex = getAxisIndex(rotationAxis, rotation);
return rotationIndex;
}, [center, rotationAxis, chartsLayerContainerRef]);
return rotationIndexGetter;
}