@mui/x-charts
Version:
The community edition of MUI X Charts components.
88 lines (86 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getRadiusAxisIndex = getRadiusAxisIndex;
exports.getRotationAxisIndex = getRotationAxisIndex;
var _scaleGuards = require("../../../scaleGuards");
var _getAsNumber = require("../../../getAsNumber");
var _findClosestIndex = require("../../../findClosestIndex");
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 getRotationAxisIndex(axisConfig, pointerValue) {
const {
scale,
data: axisData,
reverse,
isFullCircle
} = axisConfig;
const [startAngle, endAngle] = scale.range();
const angleGap = (0, _clampAngle.clampAngleRad)(pointerValue - startAngle);
const maxAngleGap = (0, _clampAngle.clampAngleRad)(endAngle - startAngle);
if (!isFullCircle && angleGap > maxAngleGap) {
// If not a full circle we only consider pointer inside the rotation range.
return -1;
}
if (!(0, _scaleGuards.isOrdinalScale)(scale)) {
if (axisData === undefined) {
return -1;
}
const angle = startAngle + (0, _clampAngle.clampAngleRad)(pointerValue - startAngle);
const valueAsNumber = (0, _getAsNumber.getAsNumber)(scale.invert(angle));
return (0, _findClosestIndex.findClosestIndex)(axisData, valueAsNumber);
}
if (!axisData) {
return -1;
}
let dataIndex;
if (scale.bandwidth() === 0) {
dataIndex = Math.floor((angleGap + scale.step() / 2) / scale.step());
if (isFullCircle) {
// To show dataIndex 0 when we are before the startAngle
dataIndex = dataIndex % axisData.length;
}
} else {
dataIndex = Math.floor(angleGap / scale.step());
}
if (dataIndex < 0 || dataIndex >= axisData.length) {
return -1;
}
return reverse ? axisData.length - 1 - dataIndex : dataIndex;
}
/**
* For a pointer coordinate, this function returns the value and dataIndex associated.
* Returns `-1` if the coordinate does not match a value.
*/
function getRadiusAxisIndex(axisConfig, pointerValue) {
const {
scale,
data: axisData,
reverse
} = axisConfig;
if (!(0, _scaleGuards.isOrdinalScale)(scale)) {
if (axisData === undefined) {
return -1;
}
const valueAsNumber = (0, _getAsNumber.getAsNumber)(scale.invert(pointerValue));
return (0, _findClosestIndex.findClosestIndex)(axisData, valueAsNumber);
}
if (!axisData) {
return -1;
}
let dataIndex;
const distFromStart = pointerValue - Math.min(...scale.range());
if (scale.bandwidth() === 0) {
dataIndex = Math.floor((distFromStart + scale.step() / 2) / scale.step());
} else {
dataIndex = Math.floor(distFromStart / scale.step());
}
if (dataIndex < 0 || dataIndex >= axisData.length) {
return -1;
}
return reverse ? axisData.length - 1 - dataIndex : dataIndex;
}