@mui/x-charts
Version:
The community edition of MUI X Charts components.
27 lines (26 loc) • 1.14 kB
JavaScript
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage";
import { isOrdinalScale } from "../../../scaleGuards.mjs";
import { clampAngleRad } from "../../../clampAngle.mjs";
/**
* For a pointer coordinate, this function returns the value and dataIndex associated.
* Returns `-1` if the coordinate does not match a value.
*/
export function getAxisIndex(axisConfig, pointerValue) {
const {
scale,
data: axisData,
reverse
} = axisConfig;
if (!isOrdinalScale(scale)) {
throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Charts: getAxisValue is not implemented for polar continuous axes. ' + 'This function only supports ordinal (band/point) scales.' : _formatErrorMessage(40));
}
if (!axisData) {
return -1;
}
const angleGap = clampAngleRad(pointerValue - Math.min(...scale.range()));
const dataIndex = scale.bandwidth() === 0 ? Math.floor((angleGap + scale.step() / 2) / scale.step()) % axisData.length : Math.floor(angleGap / scale.step());
if (dataIndex < 0 || dataIndex >= axisData.length) {
return -1;
}
return reverse ? axisData.length - 1 - dataIndex : dataIndex;
}