UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

204 lines (201 loc) 7.65 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinePlot = LinePlot; var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _styles = require("@mui/material/styles"); var _warning = require("@mui/x-internals/warning"); var _d3Shape = require("@mui/x-charts-vendor/d3-shape"); var _LineElement = require("./LineElement"); var _useScale = require("../hooks/useScale"); var _getCurve = require("../internals/getCurve"); var _isBandScale = require("../internals/isBandScale"); var _constants = require("../constants"); var _useLineSeries = require("../hooks/useLineSeries"); var _useSkipAnimation = require("../hooks/useSkipAnimation"); var _useChartGradientId = require("../hooks/useChartGradientId"); var _hooks = require("../hooks"); var _useInternalIsZoomInteracting = require("../internals/plugins/featurePlugins/useChartCartesianAxis/useInternalIsZoomInteracting"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["slots", "slotProps", "skipAnimation", "onItemClick"]; const LinePlotRoot = (0, _styles.styled)('g', { name: 'MuiAreaPlot', slot: 'Root' })({ [`& .${_LineElement.lineElementClasses.root}`]: { transition: 'opacity 0.2s ease-in, fill 0.2s ease-in' } }); const useAggregatedData = () => { const seriesData = (0, _useLineSeries.useLineSeriesContext)(); const { xAxis, xAxisIds } = (0, _hooks.useXAxes)(); const { yAxis, yAxisIds } = (0, _hooks.useYAxes)(); const getGradientId = (0, _useChartGradientId.useChartGradientIdBuilder)(); // This memo prevents odd line chart behavior when hydrating. const allData = React.useMemo(() => { if (seriesData === undefined) { return []; } const { series, stackingGroups } = seriesData; const defaultXAxisId = xAxisIds[0]; const defaultYAxisId = yAxisIds[0]; return stackingGroups.flatMap(({ ids: groupIds }) => { return groupIds.flatMap(seriesId => { const { xAxisId = defaultXAxisId, yAxisId = defaultYAxisId, stackedData, data, connectNulls, curve, strictStepCurve } = series[seriesId]; const xScale = xAxis[xAxisId].scale; const xPosition = (0, _useScale.getValueToPositionMapper)(xScale); const yScale = yAxis[yAxisId].scale; const xData = xAxis[xAxisId].data; const gradientId = yAxis[yAxisId].colorScale && getGradientId(yAxisId) || xAxis[xAxisId].colorScale && getGradientId(xAxisId) || undefined; if (process.env.NODE_ENV !== 'production') { if (xData === undefined) { throw new Error(`MUI X Charts: ${xAxisId === _constants.DEFAULT_X_AXIS_KEY ? 'The first `xAxis`' : `The x-axis with id "${xAxisId}"`} should have data property to be able to display a line plot.`); } if (xData.length < stackedData.length) { (0, _warning.warnOnce)(`MUI X Charts: The data length of the x axis (${xData.length} items) is lower than the length of series (${stackedData.length} items).`, 'error'); } } const shouldExpand = curve?.includes('step') && !strictStepCurve && (0, _isBandScale.isBandScale)(xScale); const formattedData = xData?.flatMap((x, index) => { const nullData = data[index] == null; if (shouldExpand) { const rep = [{ x, y: stackedData[index], nullData, isExtension: false }]; if (!nullData && (index === 0 || data[index - 1] == null)) { rep.unshift({ x: (xScale(x) ?? 0) - (xScale.step() - xScale.bandwidth()) / 2, y: stackedData[index], nullData, isExtension: true }); } if (!nullData && (index === data.length - 1 || data[index + 1] == null)) { rep.push({ x: (xScale(x) ?? 0) + (xScale.step() + xScale.bandwidth()) / 2, y: stackedData[index], nullData, isExtension: true }); } return rep; } return { x, y: stackedData[index], nullData }; }) ?? []; const d3Data = connectNulls ? formattedData.filter(d => !d.nullData) : formattedData; const linePath = (0, _d3Shape.line)().x(d => d.isExtension ? d.x : xPosition(d.x)).defined(d => connectNulls || !d.nullData || !!d.isExtension).y(d => yScale(d.y[1])); const d = linePath.curve((0, _getCurve.getCurveFactory)(curve))(d3Data) || ''; return (0, _extends2.default)({}, series[seriesId], { gradientId, d, seriesId }); }); }); }, [seriesData, xAxisIds, yAxisIds, xAxis, yAxis, getGradientId]); return allData; }; /** * Demos: * * - [Lines](https://mui.com/x/react-charts/lines/) * - [Line demonstration](https://mui.com/x/react-charts/line-demo/) * * API: * * - [LinePlot API](https://mui.com/x/api/charts/line-plot/) */ function LinePlot(props) { const { slots, slotProps, skipAnimation: inSkipAnimation, onItemClick } = props, other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded); const isZoomInteracting = (0, _useInternalIsZoomInteracting.useInternalIsZoomInteracting)(); const skipAnimation = (0, _useSkipAnimation.useSkipAnimation)(isZoomInteracting || inSkipAnimation); const completedData = useAggregatedData(); return /*#__PURE__*/(0, _jsxRuntime.jsx)(LinePlotRoot, (0, _extends2.default)({}, other, { children: completedData.map(({ d, seriesId, color, gradientId }) => { return /*#__PURE__*/(0, _jsxRuntime.jsx)(_LineElement.LineElement, { id: seriesId, d: d, color: color, gradientId: gradientId, skipAnimation: skipAnimation, slots: slots, slotProps: slotProps, onClick: onItemClick && (event => onItemClick(event, { type: 'line', seriesId })) }, seriesId); }) })); } process.env.NODE_ENV !== "production" ? LinePlot.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "pnpm proptypes" | // ---------------------------------------------------------------------- /** * Callback fired when a line item is clicked. * @param {React.MouseEvent<SVGPathElement, MouseEvent>} event The event source of the callback. * @param {LineItemIdentifier} lineItemIdentifier The line item identifier. */ onItemClick: _propTypes.default.func, /** * If `true`, animations are skipped. * @default false */ skipAnimation: _propTypes.default.bool, /** * The props used for each component slot. * @default {} */ slotProps: _propTypes.default.object, /** * Overridable component slots. * @default {} */ slots: _propTypes.default.object } : void 0;