@mui/x-charts
Version:
The community edition of MUI X Charts components.
82 lines (76 loc) • 2.54 kB
JavaScript
;
'use client';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getValueToPositionMapper = getValueToPositionMapper;
exports.useRadiusScale = useRadiusScale;
exports.useRotationScale = useRotationScale;
exports.useXScale = useXScale;
exports.useYScale = useYScale;
var _scaleGuards = require("../internals/scaleGuards");
var _useAxis = require("./useAxis");
/**
* For a given scale return a function that map value to their position.
* Useful when dealing with specific scale such as band.
* @param {D3Scale} scale The scale to use
* @returns {(value: any) => number} A function that map value to their position
*/
function getValueToPositionMapper(scale) {
if ((0, _scaleGuards.isOrdinalScale)(scale)) {
return value => (scale(value) ?? 0) + scale.bandwidth() / 2;
}
const domain = scale.domain();
// Fixes https://github.com/mui/mui-x/issues/18999#issuecomment-3173787401
if (domain[0] === domain[1]) {
return value => value === domain[0] ? scale(value) : NaN;
}
return value => scale(value);
}
/**
* Get the X scale.
*
* @param axisId - The axis identifier. Can be:
* - A string or number matching the axis ID defined in the chart's `xAxis` prop
* - Undefined to get the default (first) X axis
* @returns The X axis scale
*/
function useXScale(axisId) {
const axis = (0, _useAxis.useXAxis)(axisId);
return axis.scale;
}
/**
* Get the Y scale.
*
* @param axisId - The axis identifier. Can be:
* - A string or number matching the axis ID defined in the chart's `yAxis` prop
* - Undefined to get the default (first) Y axis
* @returns The Y axis scale
*/
function useYScale(axisId) {
const axis = (0, _useAxis.useYAxis)(axisId);
return axis.scale;
}
/**
* Get the rotation scale.
*
* @param axisId - The axis identifier. Can be:
* - A string or number matching the axis ID defined in the chart's `rotationAxis` prop
* - Undefined to get the default rotation axis
* @returns The rotation axis scale, or undefined if not found
*/
function useRotationScale(axisId) {
const axis = (0, _useAxis.useRotationAxis)(axisId);
return axis?.scale;
}
/**
* Get the radius scale.
* @param axisId - The axis identifier. Can be:
* - A string or number matching the axis ID defined in the chart's `radiusAxis` prop
* - Undefined to get the default radius axis
* @returns The radius axis scale, or undefined if not found
*/
function useRadiusScale(axisId) {
const axis = (0, _useAxis.useRadiusAxis)(axisId);
return axis?.scale;
}