UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

154 lines (152 loc) 4.5 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["xAxis", "yAxis", "series", "width", "height", "margin", "colors", "dataset", "sx", "axisHighlight", "grid", "children", "slots", "slotProps", "skipAnimation", "loading", "layout", "onItemClick", "highlightedItem", "onHighlightChange", "borderRadius", "barLabel", "className", "hideLegend", "showToolbar"]; import * as React from 'react'; import useId from '@mui/utils/useId'; import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from "../constants/index.js"; import { BAR_CHART_PLUGINS } from "./BarChart.plugins.js"; /** * A helper function that extracts BarChartProps from the input props * and returns an object with props for the children components of BarChart. * * @param props The input props for BarChart * @returns An object with props for the children components of BarChart */ export const useBarChartProps = props => { const { xAxis, yAxis, series, width, height, margin, colors, dataset, sx, axisHighlight, grid, children, slots, slotProps, skipAnimation, loading, layout, onItemClick, highlightedItem, onHighlightChange, borderRadius, barLabel, className } = props, rest = _objectWithoutPropertiesLoose(props, _excluded); const id = useId(); const clipPathId = `${id}-clip-path`; const hasHorizontalSeries = layout === 'horizontal' || layout === undefined && series.some(item => item.layout === 'horizontal'); const defaultBandXAxis = React.useMemo(() => [{ id: DEFAULT_X_AXIS_KEY, scaleType: 'band', data: Array.from({ length: Math.max(...series.map(s => (s.data ?? dataset ?? []).length)) }, (_, index) => index) }], [dataset, series]); const defaultBandYAxis = React.useMemo(() => [{ id: DEFAULT_Y_AXIS_KEY, scaleType: 'band', data: Array.from({ length: Math.max(...series.map(s => (s.data ?? dataset ?? []).length)) }, (_, index) => index) }], [dataset, series]); const seriesWithDefault = React.useMemo(() => series.map(s => _extends({ type: 'bar' }, s, { layout: hasHorizontalSeries ? 'horizontal' : 'vertical' })), [hasHorizontalSeries, series]); const defaultXAxis = hasHorizontalSeries ? undefined : defaultBandXAxis; const processedXAxis = React.useMemo(() => { if (!xAxis) { return defaultXAxis; } return hasHorizontalSeries ? xAxis : xAxis.map(axis => _extends({ scaleType: 'band' }, axis)); }, [defaultXAxis, hasHorizontalSeries, xAxis]); const defaultYAxis = hasHorizontalSeries ? defaultBandYAxis : undefined; const processedYAxis = React.useMemo(() => { if (!yAxis) { return defaultYAxis; } return hasHorizontalSeries ? yAxis.map(axis => _extends({ scaleType: 'band' }, axis)) : yAxis; }, [defaultYAxis, hasHorizontalSeries, yAxis]); const chartContainerProps = _extends({}, rest, { series: seriesWithDefault, width, height, margin, colors, dataset, xAxis: processedXAxis, yAxis: processedYAxis, highlightedItem, onHighlightChange, disableAxisListener: slotProps?.tooltip?.trigger !== 'axis' && axisHighlight?.x === 'none' && axisHighlight?.y === 'none', className, skipAnimation, plugins: BAR_CHART_PLUGINS }); const barPlotProps = { onItemClick, slots, slotProps, borderRadius, barLabel }; const gridProps = { vertical: grid?.vertical, horizontal: grid?.horizontal }; const clipPathGroupProps = { clipPath: `url(#${clipPathId})` }; const clipPathProps = { id: clipPathId }; const overlayProps = { slots, slotProps, loading }; const chartsAxisProps = { slots, slotProps }; const axisHighlightProps = _extends({}, hasHorizontalSeries ? { y: 'band' } : { x: 'band' }, axisHighlight); const legendProps = { slots, slotProps }; const chartsWrapperProps = { sx, legendPosition: props.slotProps?.legend?.position, legendDirection: props.slotProps?.legend?.direction }; return { chartsWrapperProps, chartContainerProps, barPlotProps, gridProps, clipPathProps, clipPathGroupProps, overlayProps, chartsAxisProps, axisHighlightProps, legendProps, children }; };