@mui/x-charts
Version:
The community edition of MUI X Charts components.
140 lines (138 loc) • 3.65 kB
JavaScript
'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", "onAreaClick", "onLineClick", "onMarkClick", "axisHighlight", "disableLineItemHighlight", "hideLegend", "grid", "children", "slots", "slotProps", "skipAnimation", "loading", "highlightedItem", "onHighlightChange", "className", "showToolbar"];
import * as React from 'react';
import useId from '@mui/utils/useId';
import { DEFAULT_X_AXIS_KEY } from "../constants/index.js";
import { LINE_CHART_PLUGINS } from "./LineChart.plugins.js";
/**
* A helper function that extracts LineChartProps from the input props
* and returns an object with props for the children components of LineChart.
*
* @param props The input props for LineChart
* @returns An object with props for the children components of LineChart
*/
export const useLineChartProps = props => {
const {
xAxis,
yAxis,
series,
width,
height,
margin,
colors,
dataset,
sx,
onAreaClick,
onLineClick,
onMarkClick,
axisHighlight,
disableLineItemHighlight,
grid,
children,
slots,
slotProps,
skipAnimation,
loading,
highlightedItem,
onHighlightChange,
className
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const id = useId();
const clipPathId = `${id}-clip-path`;
const seriesWithDefault = React.useMemo(() => series.map(s => _extends({
disableHighlight: !!disableLineItemHighlight,
type: 'line'
}, s)), [disableLineItemHighlight, series]);
const chartContainerProps = _extends({}, other, {
series: seriesWithDefault,
width,
height,
margin,
colors,
dataset,
xAxis: xAxis ?? [{
id: DEFAULT_X_AXIS_KEY,
scaleType: 'point',
data: Array.from({
length: Math.max(...series.map(s => (s.data ?? dataset ?? []).length))
}, (_, index) => index)
}],
yAxis,
highlightedItem,
onHighlightChange,
disableAxisListener: slotProps?.tooltip?.trigger !== 'axis' && axisHighlight?.x === 'none' && axisHighlight?.y === 'none',
className,
skipAnimation,
plugins: LINE_CHART_PLUGINS
});
const gridProps = {
vertical: grid?.vertical,
horizontal: grid?.horizontal
};
const clipPathGroupProps = {
clipPath: `url(#${clipPathId})`
};
const clipPathProps = {
id: clipPathId
};
const areaPlotProps = {
slots,
slotProps,
onItemClick: onAreaClick
};
const linePlotProps = {
slots,
slotProps,
onItemClick: onLineClick
};
const markPlotProps = {
slots,
slotProps,
onItemClick: onMarkClick,
skipAnimation
};
const overlayProps = {
slots,
slotProps,
loading
};
const chartsAxisProps = {
slots,
slotProps
};
const axisHighlightProps = _extends({
x: 'line'
}, axisHighlight);
const lineHighlightPlotProps = {
slots,
slotProps
};
const legendProps = {
slots,
slotProps
};
const chartsWrapperProps = {
sx,
legendPosition: props.slotProps?.legend?.position,
legendDirection: props.slotProps?.legend?.direction
};
return {
chartsWrapperProps,
chartContainerProps,
gridProps,
clipPathProps,
clipPathGroupProps,
areaPlotProps,
linePlotProps,
markPlotProps,
overlayProps,
chartsAxisProps,
axisHighlightProps,
lineHighlightPlotProps,
legendProps,
children
};
};