UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

33 lines (32 loc) 1.41 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAxisIndex = getAxisIndex; var _formatErrorMessage2 = _interopRequireDefault(require("@mui/x-internals/formatErrorMessage")); var _scaleGuards = require("../../../scaleGuards"); var _clampAngle = require("../../../clampAngle"); /** * For a pointer coordinate, this function returns the value and dataIndex associated. * Returns `-1` if the coordinate does not match a value. */ function getAxisIndex(axisConfig, pointerValue) { const { scale, data: axisData, reverse } = axisConfig; if (!(0, _scaleGuards.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.' : (0, _formatErrorMessage2.default)(40)); } if (!axisData) { return -1; } const angleGap = (0, _clampAngle.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; }