UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

21 lines (19 loc) 733 B
'use client'; import { isOrdinalScale } from "../internals/scaleGuards.mjs"; /** * 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 */ export function getValueToPositionMapper(scale) { if (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); }