214 lines
7.5 KiB
JavaScript
214 lines
7.5 KiB
JavaScript
"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.MarkPlot = MarkPlot;
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
var React = _interopRequireWildcard(require("react"));
|
|
var _constants = require("../constants");
|
|
var _useSkipAnimation = require("../hooks/useSkipAnimation");
|
|
var _useChartId = require("../hooks/useChartId");
|
|
var _useScale = require("../hooks/useScale");
|
|
var _useLineSeries = require("../hooks/useLineSeries");
|
|
var _cleanId = require("../internals/cleanId");
|
|
var _CircleMarkElement = require("./CircleMarkElement");
|
|
var _getColor = _interopRequireDefault(require("./seriesConfig/getColor"));
|
|
var _MarkElement = require("./MarkElement");
|
|
var _ChartProvider = require("../context/ChartProvider");
|
|
var _hooks = require("../hooks");
|
|
var _useInternalIsZoomInteracting = require("../internals/plugins/featurePlugins/useChartCartesianAxis/useInternalIsZoomInteracting");
|
|
var _useChartCartesianAxis = require("../internals/plugins/featurePlugins/useChartCartesianAxis");
|
|
var _useSelector = require("../internals/store/useSelector");
|
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
const _excluded = ["slots", "slotProps", "skipAnimation", "onItemClick"];
|
|
/**
|
|
* Demos:
|
|
*
|
|
* - [Lines](https://mui.com/x/react-charts/lines/)
|
|
* - [Line demonstration](https://mui.com/x/react-charts/line-demo/)
|
|
*
|
|
* API:
|
|
*
|
|
* - [MarkPlot API](https://mui.com/x/api/charts/mark-plot/)
|
|
*/
|
|
function MarkPlot(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 seriesData = (0, _useLineSeries.useLineSeriesContext)();
|
|
const {
|
|
xAxis,
|
|
xAxisIds
|
|
} = (0, _hooks.useXAxes)();
|
|
const {
|
|
yAxis,
|
|
yAxisIds
|
|
} = (0, _hooks.useYAxes)();
|
|
const chartId = (0, _useChartId.useChartId)();
|
|
const {
|
|
instance,
|
|
store
|
|
} = (0, _ChartProvider.useChartContext)();
|
|
const {
|
|
isFaded,
|
|
isHighlighted
|
|
} = (0, _hooks.useItemHighlightedGetter)();
|
|
const xAxisHighlightIndexes = (0, _useSelector.useSelector)(store, _useChartCartesianAxis.selectorChartsHighlightXAxisIndex);
|
|
const highlightedItems = React.useMemo(() => {
|
|
const rep = {};
|
|
for (const {
|
|
dataIndex,
|
|
axisId
|
|
} of xAxisHighlightIndexes) {
|
|
if (rep[axisId] === undefined) {
|
|
rep[axisId] = new Set([dataIndex]);
|
|
} else {
|
|
rep[axisId].add(dataIndex);
|
|
}
|
|
}
|
|
return rep;
|
|
}, [xAxisHighlightIndexes]);
|
|
if (seriesData === undefined) {
|
|
return null;
|
|
}
|
|
const {
|
|
series,
|
|
stackingGroups
|
|
} = seriesData;
|
|
const defaultXAxisId = xAxisIds[0];
|
|
const defaultYAxisId = yAxisIds[0];
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)("g", (0, _extends2.default)({}, other, {
|
|
children: stackingGroups.flatMap(({
|
|
ids: groupIds
|
|
}) => {
|
|
return groupIds.map(seriesId => {
|
|
const {
|
|
xAxisId = defaultXAxisId,
|
|
yAxisId = defaultYAxisId,
|
|
stackedData,
|
|
data,
|
|
showMark = true,
|
|
shape = 'circle'
|
|
} = series[seriesId];
|
|
if (showMark === false) {
|
|
return null;
|
|
}
|
|
const xScale = (0, _useScale.getValueToPositionMapper)(xAxis[xAxisId].scale);
|
|
const yScale = yAxis[yAxisId].scale;
|
|
const xData = xAxis[xAxisId].data;
|
|
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.`);
|
|
}
|
|
const clipId = (0, _cleanId.cleanId)(`${chartId}-${seriesId}-line-clip`); // We assume that if displaying line mark, the line will also be rendered
|
|
|
|
const colorGetter = (0, _getColor.default)(series[seriesId], xAxis[xAxisId], yAxis[yAxisId]);
|
|
const Mark = slots?.mark ?? (shape === 'circle' ? _CircleMarkElement.CircleMarkElement : _MarkElement.MarkElement);
|
|
const isSeriesHighlighted = isHighlighted({
|
|
seriesId
|
|
});
|
|
const isSeriesFaded = !isSeriesHighlighted && isFaded({
|
|
seriesId
|
|
});
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)("g", {
|
|
clipPath: `url(#${clipId})`,
|
|
"data-series": seriesId,
|
|
children: xData?.map((x, index) => {
|
|
const value = data[index] == null ? null : stackedData[index][1];
|
|
return {
|
|
x: xScale(x),
|
|
y: value === null ? null : yScale(value),
|
|
position: x,
|
|
value,
|
|
index
|
|
};
|
|
}).filter(({
|
|
x,
|
|
y,
|
|
index,
|
|
position,
|
|
value
|
|
}) => {
|
|
if (value === null || y === null) {
|
|
// Remove missing data point
|
|
return false;
|
|
}
|
|
if (!instance.isPointInside(x, y)) {
|
|
// Remove out of range
|
|
return false;
|
|
}
|
|
if (showMark === true) {
|
|
return true;
|
|
}
|
|
return showMark({
|
|
x,
|
|
y,
|
|
index,
|
|
position,
|
|
value
|
|
});
|
|
}).map(({
|
|
x,
|
|
y,
|
|
index
|
|
}) => {
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Mark, (0, _extends2.default)({
|
|
id: seriesId,
|
|
dataIndex: index,
|
|
shape: shape,
|
|
color: colorGetter(index),
|
|
x: x,
|
|
y: y // Don't know why TS doesn't get from the filter that y can't be null
|
|
,
|
|
skipAnimation: skipAnimation,
|
|
onClick: onItemClick && (event => onItemClick(event, {
|
|
type: 'line',
|
|
seriesId,
|
|
dataIndex: index
|
|
})),
|
|
isHighlighted: highlightedItems[xAxisId]?.has(index) || isSeriesHighlighted,
|
|
isFaded: isSeriesFaded
|
|
}, slotProps?.mark), `${seriesId}-${index}`);
|
|
})
|
|
}, seriesId);
|
|
});
|
|
})
|
|
}));
|
|
}
|
|
process.env.NODE_ENV !== "production" ? MarkPlot.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 mark item is clicked.
|
|
* @param {React.MouseEvent<SVGPathElement, MouseEvent>} event The event source of the callback.
|
|
* @param {LineItemIdentifier} lineItemIdentifier The line mark item identifier.
|
|
*/
|
|
onItemClick: _propTypes.default.func,
|
|
/**
|
|
* If `true`, animations are skipped.
|
|
*/
|
|
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; |