@mui/x-charts
Version:
The community edition of MUI X Charts components.
26 lines (24 loc) • 877 B
JavaScript
;
'use client';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getValueToPositionMapper = getValueToPositionMapper;
var _scaleGuards = require("../internals/scaleGuards");
/**
* 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);
}