UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

50 lines (49 loc) 1.83 kB
import { interpolateNumber } from '@mui/x-charts-vendor/d3-interpolate'; import { useAnimate } from "./useAnimate.js"; function barPropsInterpolator(from, to) { const interpolateX = interpolateNumber(from.x, to.x); const interpolateY = interpolateNumber(from.y, to.y); const interpolateWidth = interpolateNumber(from.width, to.width); const interpolateHeight = interpolateNumber(from.height, to.height); return t => { return { x: interpolateX(t), y: interpolateY(t), width: interpolateWidth(t), height: interpolateHeight(t) }; }; } /** * Animates a bar from the start of the axis (x-axis for vertical layout, y-axis for horizontal layout) to its * final position. * * The props object also accepts a `ref` which will be merged with the ref returned from this hook. This means you can * pass the ref returned by this hook to the `path` element and the `ref` provided as argument will also be called. */ export function useAnimateBar(props) { const initialProps = { x: props.layout === 'vertical' ? props.x : props.xOrigin, y: props.layout === 'vertical' ? props.yOrigin : props.y, width: props.layout === 'vertical' ? props.width : 0, height: props.layout === 'vertical' ? 0 : props.height }; return useAnimate({ x: props.x, y: props.y, width: props.width, height: props.height }, { createInterpolator: barPropsInterpolator, applyProps(element, animatedProps) { element.setAttribute('x', animatedProps.x.toString()); element.setAttribute('y', animatedProps.y.toString()); element.setAttribute('width', animatedProps.width.toString()); element.setAttribute('height', animatedProps.height.toString()); }, transformProps: p => p, initialProps, skip: props.skipAnimation, ref: props.ref }); }