@mui/x-charts
Version:
The community edition of the Charts components (MUI X).
38 lines (37 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getValueToPositionMapper = getValueToPositionMapper;
exports.useXScale = useXScale;
exports.useYScale = useYScale;
var _CartesianProvider = require("../context/CartesianProvider");
var _isBandScale = require("../internals/isBandScale");
/**
* For a given scale return a function that map value to their position.
* Useful when dealing with specific scale such as band.
* @param scale The scale to use
* @returns (value: any) => number
*/
function getValueToPositionMapper(scale) {
if ((0, _isBandScale.isBandScale)(scale)) {
return value => (scale(value) ?? 0) + scale.bandwidth() / 2;
}
return value => scale(value);
}
function useXScale(identifier) {
const {
xAxis,
xAxisIds
} = (0, _CartesianProvider.useCartesianContext)();
const id = typeof identifier === 'string' ? identifier : xAxisIds[identifier ?? 0];
return xAxis[id].scale;
}
function useYScale(identifier) {
const {
yAxis,
yAxisIds
} = (0, _CartesianProvider.useCartesianContext)();
const id = typeof identifier === 'string' ? identifier : yAxisIds[identifier ?? 0];
return yAxis[id].scale;
}