@mui/x-charts
Version:
The community edition of MUI X Charts components.
16 lines (15 loc) • 618 B
JavaScript
import { isOrdinalScale } from "./scaleGuards.js";
export function invertScale(scale, data, value) {
if (isOrdinalScale(scale)) {
const dataIndex = getDataIndexForOrdinalScaleValue(scale, value);
return data[dataIndex];
}
return scale.invert(value);
}
/**
* Get the data index for a given value on an ordinal scale.
*/
export function getDataIndexForOrdinalScaleValue(scale, value) {
const dataIndex = scale.bandwidth() === 0 ? Math.floor((value - Math.min(...scale.range()) + scale.step() / 2) / scale.step()) : Math.floor((value - Math.min(...scale.range())) / scale.step());
return dataIndex;
}