@mui/x-charts
Version:
The community edition of MUI X Charts components.
47 lines (43 loc) • 1.52 kB
JavaScript
'use client';
import { isBandScale } from "../internals/isBandScale.js";
import { useRadiusAxis, useRotationAxis, useXAxis, useYAxis } from "./useAxis.js";
/**
* 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 (isBandScale(scale)) {
return value => (scale(value) ?? 0) + scale.bandwidth() / 2;
}
return value => scale(value);
}
/**
* Get the X scale.
*
* @param {AxisId | undefined} axisId - If provided returns the scale for the x axis with axisId, else returns the values for the default x axis.
* @returns {AxisScaleConfig[S]['scale']} The scale for the specified X axis.
*/
export function useXScale(axisId) {
const axis = useXAxis(axisId);
return axis.scale;
}
/**
* Get the Y scale.
*
* @param {AxisId | undefined} axisId - If provided returns the scale for the y axis with axisId, else returns the values for the default y axis.
* @returns {AxisScaleConfig[S]['scale']} The scale for the specified Y axis.
*/
export function useYScale(axisId) {
const axis = useYAxis(axisId);
return axis.scale;
}
export function useRotationScale(identifier) {
const axis = useRotationAxis(identifier);
return axis?.scale;
}
export function useRadiusScale(identifier) {
const axis = useRadiusAxis(identifier);
return axis?.scale;
}