recharts
Version:
React charts
824 lines (809 loc) • 38.3 kB
JavaScript
var _excluded = ["id"],
_excluded2 = ["onMouseEnter", "onClick", "onMouseLeave"],
_excluded3 = ["animationBegin", "animationDuration", "animationEasing", "hide", "isAnimationActive", "legendType", "lineJointType", "lineType", "shape", "xAxisId", "yAxisId", "zAxisId"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import * as React from 'react';
import { useMemo, useRef } from 'react';
import { Layer } from '../container/Layer';
import { CartesianLabelListContextProvider, LabelListFromLabelProp, SetLabelListClipArea } from '../component/LabelList';
import { clsx } from '../util/clsx';
import { findAllByType } from '../util/ReactUtils';
import { Curve } from '../shape/Curve';
import { Cell } from '../component/Cell';
import { getLinearRegression, interpolate, isNan, isNullish } from '../util/DataUtils';
import { getCateCoordinateOfLine, getTooltipNameProp, getValueByDataKey } from '../util/ChartUtils';
import { adaptEventsOfChild, isNonEmptyArray } from '../util/types';
import { ScatterSymbol } from '../util/ScatterUtils';
import { useMouseClickItemDispatch, useMouseEnterItemDispatch, useMouseLeaveItemDispatch } from '../context/tooltipContext';
import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
import { SetErrorBarContext } from '../context/ErrorBarContext';
import { GraphicalItemClipPath, useNeedsClip } from './GraphicalItemClipPath';
import { selectScatterPoints } from '../state/selectors/scatterSelectors';
import { useAppSelector } from '../state/hooks';
import { implicitZAxis } from '../state/selectors/axisSelectors';
import { useIsPanorama } from '../context/PanoramaContext';
import { selectActiveTooltipIndex } from '../state/selectors/tooltipSelectors';
import { SetLegendPayload } from '../state/SetLegendPayload';
import { DATA_ITEM_GRAPHICAL_ITEM_ID_ATTRIBUTE_NAME } from '../util/Constants';
import { resolveDefaultProps } from '../util/resolveDefaultProps';
import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
import { SetCartesianGraphicalItem } from '../state/SetGraphicalItem';
import { svgPropertiesNoEvents, svgPropertiesNoEventsFromUnknown } from '../util/svgPropertiesNoEvents';
import { useCartesianChartLayout, useViewBox } from '../context/chartLayoutContext';
import { AnimatedItems, useAnimationCallbacks } from '../animation/AnimatedItems';
import { matchAppend } from '../animation/matchBy';
import { ZIndexLayer } from '../zIndex/ZIndexLayer';
import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
import { propsAreEqual } from '../util/propsAreEqual';
import { useBackwardsCompatibleTheme } from '../theme/useBackwardsCompatibleTheme';
import { useGraphicalItemIdentity, useGraphicalItemThemeFill } from '../theme/useGraphicalItemIdentity';
import { useEffectiveDataKey } from '../util/useEffectiveDataKey';
import { getDataKeyOnAxis } from '../state/selectors/effectiveDataKeySelectors';
import { getEntryShapeColors, getEntryStyleOverrides, hasOwnColors, useUnthemedColors } from '../theme/dataEntryStyles';
import { getActiveStyleOverrides, useThemedActiveStyles } from '../theme/activeStyles';
import { EntryFillDefinitions, FillObjectDefinition, getEntryFillId, getFillUrl, getSeriesFillId, hasEntryFillObjects, readEntryFill, splitFill, withEntryFillColor } from '../fill/fillDefinitions';
import { DataLabelColorsContextProvider, DataLabelThemeContextProvider, useSeriesLabelThemeResolver } from '../theme/labelTheme';
/**
* @public
*/
/**
* Scatter coordinates are nullable because sometimes the point value is out of the domain,
* and we can't compute a valid coordinate for it.
*
* Scatter -> Symbol ignores points with null cx or cy so those won't render if using the default shapes.
* However: the points are exposed via various props and can be used in custom shapes so we keep them around.
* @public
*/
/**
* @public
*/
/**
* Internal props, combination of external props + defaultProps + private Recharts state
*/
/**
* External props, intended for end users to fill in
*
* The type is exported only so that TypeScript can name it in the declaration files of projects
* that wrap Recharts components. It is not part of the public API and may change in any release.
*
* @internal
*/
/**
* Because of naming conflict, we are forced to ignore certain (valid) SVG attributes.
*/
/**
* @public
*/
/**
* Props after the fill object is split off: `fill` is the base color.
*/
var computeLegendPayloadFromScatterProps = (props, explicitShape, fillObject) => {
var dataKey = props.dataKey,
name = props.name,
fill = props.fill,
stroke = props.stroke,
legendType = props.legendType,
hide = props.hide;
return [{
inactive: hide,
dataKey,
type: legendType,
/*
* Only an explicit shape describes the points to the legend, so that the legend of a Scatter
* with the default shape stays unchanged. The legend symbol gets the same fill and stroke as the points,
* so that a stroke-only shape (fill="none") stays visible in the legend.
*/
dot: typeof explicitShape === 'string' ? {
symbol: explicitShape,
fill,
stroke
} : undefined,
color: fill,
fill: fillObject,
value: getTooltipNameProp(name, dataKey),
payload: props
}];
};
var SetScatterTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
var dataKey = _ref.dataKey,
points = _ref.points,
stroke = _ref.stroke,
strokeWidth = _ref.strokeWidth,
fill = _ref.fill,
name = _ref.name,
hide = _ref.hide,
formatter = _ref.formatter,
tooltipType = _ref.tooltipType,
id = _ref.id;
var tooltipEntrySettings = {
dataDefinedOnItem: points === null || points === void 0 ? void 0 : points.map(p => p.tooltipPayload),
getPosition: index => {
var _points$Number;
return points === null || points === void 0 || (_points$Number = points[Number(index)]) === null || _points$Number === void 0 ? void 0 : _points$Number.tooltipPosition;
},
settings: {
stroke,
strokeWidth,
fill,
nameKey: undefined,
dataKey,
name: getTooltipNameProp(name, dataKey),
hide,
type: tooltipType,
color: fill,
unit: '',
// why doesn't Scatter support unit?
formatter,
graphicalItemId: id
}
};
return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
tooltipEntrySettings: tooltipEntrySettings
});
});
function ScatterLine(_ref2) {
var points = _ref2.points,
props = _ref2.props;
var line = props.line,
lineType = props.lineType,
lineJointType = props.lineJointType;
if (!line) {
return null;
}
var scatterProps = svgPropertiesNoEvents(props);
var customLineProps = svgPropertiesNoEventsFromUnknown(line);
var linePoints, lineItem;
if (lineType === 'joint') {
linePoints = points.map(entry => {
var _entry$cx, _entry$cy;
return {
x: (_entry$cx = entry.cx) !== null && _entry$cx !== void 0 ? _entry$cx : null,
y: (_entry$cy = entry.cy) !== null && _entry$cy !== void 0 ? _entry$cy : null
};
});
} else if (lineType === 'fitting') {
var _getLinearRegression = getLinearRegression(points),
xmin = _getLinearRegression.xmin,
xmax = _getLinearRegression.xmax,
a = _getLinearRegression.a,
b = _getLinearRegression.b;
var linearExp = x => a * x + b;
linePoints = [{
x: xmin,
y: linearExp(xmin)
}, {
x: xmax,
y: linearExp(xmax)
}];
}
var lineProps = _objectSpread(_objectSpread(_objectSpread({}, scatterProps), {}, {
// @ts-expect-error customLineProps is contributing unknown props
fill: 'none',
// @ts-expect-error customLineProps is contributing unknown props
stroke: scatterProps && scatterProps.fill
}, customLineProps), {}, {
// @ts-expect-error linePoints is used before it is assigned (???)
points: linePoints
});
if (/*#__PURE__*/React.isValidElement(line)) {
lineItem = /*#__PURE__*/React.cloneElement(line, lineProps);
} else if (typeof line === 'function') {
lineItem = line(lineProps);
} else {
lineItem = /*#__PURE__*/React.createElement(Curve, _extends({}, lineProps, {
type: lineJointType
}));
}
return /*#__PURE__*/React.createElement(Layer, {
className: "recharts-scatter-line",
key: "recharts-scatter-line"
}, lineItem);
}
/**
* Resolves the color a scatter point actually renders with, mirroring the render order in ScatterSymbols:
* data entry props win over the series props (theme already merged into those).
* Cell is deprecated and intentionally not part of this; its colors never reach the tooltip.
* Fill wins over stroke, same as Pie and Funnel entry colors.
* https://github.com/recharts/recharts/issues/7976
*/
export function getScatterEntryColor(entry, seriesFill, seriesStroke, unthemedColors) {
var _ref3, _ref4;
/*
* When the entry defines its own colors, the theme is suppressed for that entry at render time,
* and the explicit Scatter props fill in the rest (see getEntryStyleOverrides).
* Mirror that here so the tooltip color matches the rendered symbol.
*/
var seriesFillFallback = hasOwnColors(entry) && unthemedColors != null ? unthemedColors.fill : seriesFill;
var seriesStrokeFallback = hasOwnColors(entry) && unthemedColors != null ? unthemedColors.stroke : seriesStroke;
var fill = entry != null && typeof entry === 'object' && 'fill' in entry && typeof entry.fill === 'string' ? entry.fill : undefined;
var stroke = entry != null && typeof entry === 'object' && 'stroke' in entry && typeof entry.stroke === 'string' ? entry.stroke : undefined;
return (_ref3 = (_ref4 = fill !== null && fill !== void 0 ? fill : seriesFillFallback) !== null && _ref4 !== void 0 ? _ref4 : stroke) !== null && _ref3 !== void 0 ? _ref3 : seriesStrokeFallback;
}
function ScatterLabelListProvider(_ref5) {
var showLabels = _ref5.showLabels,
points = _ref5.points,
seriesFill = _ref5.seriesFill,
seriesStroke = _ref5.seriesStroke,
unthemedColors = _ref5.unthemedColors,
children = _ref5.children;
var chartViewBox = useViewBox();
var labelColors = useMemo(() => ({
hasFilledShapes: false,
getShapeColors: index => {
var _points$index;
return getEntryShapeColors((_points$index = points[index]) === null || _points$index === void 0 ? void 0 : _points$index.payload, seriesFill, seriesStroke, unthemedColors);
}
}), [points, seriesFill, seriesStroke, unthemedColors]);
var labelListEntries = useMemo(() => {
return points === null || points === void 0 ? void 0 : points.map(point => {
var _point$x, _point$y;
var viewBox = {
/*
* Scatter label uses x and y as the reference point for the label,
* not cx and cy.
*/
x: (_point$x = point.x) !== null && _point$x !== void 0 ? _point$x : 0,
/*
* Scatter label uses x and y as the reference point for the label,
* not cx and cy.
*/
y: (_point$y = point.y) !== null && _point$y !== void 0 ? _point$y : 0,
width: point.width,
height: point.height,
lowerWidth: point.width,
upperWidth: point.width
};
return _objectSpread(_objectSpread({}, viewBox), {}, {
/*
* Here we put undefined because Scatter shows two values usually, one for X and one for Y.
* LabelList will see this undefined and will use its own `dataKey` prop to determine which value to show,
* using the payload below.
*/
value: undefined,
payload: point.payload,
viewBox,
parentViewBox: chartViewBox,
fill: undefined
});
});
}, [chartViewBox, points]);
return /*#__PURE__*/React.createElement(CartesianLabelListContextProvider, {
value: showLabels ? labelListEntries : undefined
}, /*#__PURE__*/React.createElement(DataLabelColorsContextProvider, {
value: labelColors
}, children));
}
/**
* Individual scatter point component that subscribes to its own isActive state.
* This avoids re-rendering all points when the active index changes —
* only the point becoming active and the point becoming inactive re-render.
*
* @param entry The scatter point data including coordinates, size, and tooltip payload
* @param index The index of this point in the points array
* @param shape The default shape to render for inactive points
* @param activeShape The shape to render when this point is active, or undefined if no active shape
* @param baseProps SVG presentation attributes (fill, stroke, etc.) shared across all points
* @param entryStyleOverrides Removes theme styles from points that define their own colors in data, or undefined
* @param themedActiveStyles Theme styles of the active shape, or undefined
* @param id The graphical item ID of the parent Scatter component
* @param restOfAllOtherProps Remaining Scatter props for user-provided event handlers via adaptEventsOfChild
* @param onMouseEnterFromContext Curried mouse enter handler that dispatches tooltip activation
* @param onMouseLeaveFromContext Curried mouse leave handler that dispatches tooltip deactivation
* @param onClickFromContext Curried click handler that dispatches tooltip click activation
*/
function ScatterPoint(_ref6) {
var _useAppSelector;
var entry = _ref6.entry,
paintFill = _ref6.paintFill,
index = _ref6.index,
shape = _ref6.shape,
activeShape = _ref6.activeShape,
baseProps = _ref6.baseProps,
entryStyleOverrides = _ref6.entryStyleOverrides,
themedActiveStyles = _ref6.themedActiveStyles,
id = _ref6.id,
restOfAllOtherProps = _ref6.restOfAllOtherProps,
animationElapsedTime = _ref6.animationElapsedTime,
isAnimating = _ref6.isAnimating,
isEntrance = _ref6.isEntrance,
onMouseEnterFromContext = _ref6.onMouseEnterFromContext,
onMouseLeaveFromContext = _ref6.onMouseLeaveFromContext,
onClickFromContext = _ref6.onClickFromContext;
var hasActiveShape = activeShape != null && activeShape !== false;
var selectIsActive = useMemo(() => {
var strIndex = String(index);
return state => hasActiveShape && selectActiveTooltipIndex(state) === strIndex;
}, [hasActiveShape, index]);
var isActive = (_useAppSelector = useAppSelector(selectIsActive)) !== null && _useAppSelector !== void 0 ? _useAppSelector : false;
// isActive is only true when hasActiveShape is true, so activeShape is defined here
var option = isActive && activeShape != null && activeShape !== false ? activeShape : shape;
// Filter out undefined values from baseProps so data fill/stroke aren't clobbered
var filteredBaseProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, baseProps.fill !== undefined && {
fill: baseProps.fill
}), baseProps.stroke !== undefined && {
stroke: baseProps.stroke
}), baseProps.strokeWidth !== undefined && {
strokeWidth: baseProps.strokeWidth
}), baseProps.strokeOpacity !== undefined && {
strokeOpacity: baseProps.strokeOpacity
}), baseProps.fillOpacity !== undefined && {
fillOpacity: baseProps.fillOpacity
}), baseProps.strokeDasharray !== undefined && {
strokeDasharray: baseProps.strokeDasharray
});
var symbolProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, filteredBaseProps), entryStyleOverrides), entry), paintFill == null ? undefined : {
fill: paintFill
}), isActive ? getActiveStyleOverrides(activeShape, themedActiveStyles, entry) : undefined), {}, {
isActive,
index,
animationElapsedTime,
isAnimating,
isEntrance,
[DATA_ITEM_GRAPHICAL_ITEM_ID_ATTRIBUTE_NAME]: String(id)
});
return /*#__PURE__*/React.createElement(ZIndexLayer
/*
* inactive Scatters use the parent zIndex, which is represented by undefined here.
* ZIndexLayer will render undefined zIndex as-is, as regular children, without portals.
* Active Scatters use the activeDot zIndex so they render above other elements.
*/, {
zIndex: isActive ? DefaultZIndexes.activeDot : undefined
}, /*#__PURE__*/React.createElement(Layer, _extends({
className: "recharts-scatter-symbol"
}, adaptEventsOfChild(restOfAllOtherProps, entry, index), {
onMouseEnter: onMouseEnterFromContext(entry, index),
onMouseLeave: onMouseLeaveFromContext(entry, index),
onClick: onClickFromContext(entry, index)
}), /*#__PURE__*/React.createElement(ScatterSymbol, _extends({
option: option
}, symbolProps))));
}
function ScatterSymbols(props) {
var points = props.points,
allOtherScatterProps = props.allOtherScatterProps,
animationElapsedTime = props.animationElapsedTime,
isAnimating = props.isAnimating,
isEntrance = props.isEntrance;
var shape = allOtherScatterProps.shape,
activeShape = allOtherScatterProps.activeShape,
dataKey = allOtherScatterProps.dataKey,
unthemedColors = allOtherScatterProps.unthemedColors,
themedActiveStyles = allOtherScatterProps.themedActiveStyles;
var id = allOtherScatterProps.id,
allOtherPropsWithoutId = _objectWithoutProperties(allOtherScatterProps, _excluded);
var onMouseEnterFromProps = allOtherScatterProps.onMouseEnter,
onItemClickFromProps = allOtherScatterProps.onClick,
onMouseLeaveFromProps = allOtherScatterProps.onMouseLeave,
restOfAllOtherProps = _objectWithoutProperties(allOtherScatterProps, _excluded2);
var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, dataKey, id);
var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps);
var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, dataKey, id);
if (!isNonEmptyArray(points)) {
return null;
}
var svgProps = svgPropertiesNoEvents(allOtherPropsWithoutId);
var baseProps = allOtherScatterProps.fillObject == null ? svgProps : _objectSpread(_objectSpread({}, svgProps), {}, {
fill: getFillUrl(getSeriesFillId(id))
});
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ScatterLine, {
points: points,
props: allOtherPropsWithoutId
}), points.map((entry, i) => /*#__PURE__*/React.createElement(ScatterPoint, {
key: "symbol-".concat(entry === null || entry === void 0 ? void 0 : entry.cx, "-").concat(entry === null || entry === void 0 ? void 0 : entry.cy, "-").concat(entry === null || entry === void 0 ? void 0 : entry.size, "-").concat(i),
entry: entry
// a data entry with its own fill object paints with its own definition
,
paintFill: readEntryFill(entry.payload).fillObject == null ? undefined : getFillUrl(getEntryFillId(id, i)),
index: i,
shape: shape,
activeShape: activeShape,
baseProps: baseProps,
entryStyleOverrides: getEntryStyleOverrides(entry, unthemedColors),
themedActiveStyles: themedActiveStyles,
id: id,
restOfAllOtherProps: restOfAllOtherProps,
animationElapsedTime: animationElapsedTime,
isAnimating: isAnimating,
isEntrance: isEntrance,
onMouseEnterFromContext: onMouseEnterFromContext,
onMouseLeaveFromContext: onMouseLeaveFromContext,
onClickFromContext: onClickFromContext
})));
}
var defaultScatterAnimateItems = (items, animationElapsedTime) => {
if (items == null) return [];
if (animationElapsedTime === 1) {
return items.flatMap(item => item.status === 'removed' ? [] : [item.next]);
}
return items.flatMap(item => {
if (item.status === 'removed') return [];
if (item.status === 'matched') {
return [_objectSpread(_objectSpread({}, item.next), {}, {
cx: item.next.cx == null ? undefined : interpolate(item.prev.cx, item.next.cx, animationElapsedTime),
cy: item.next.cy == null ? undefined : interpolate(item.prev.cy, item.next.cy, animationElapsedTime),
size: interpolate(item.prev.size, item.next.size, animationElapsedTime)
})];
}
// added
return [_objectSpread(_objectSpread({}, item.next), {}, {
size: interpolate(0, item.next.size, animationElapsedTime)
})];
});
};
function SymbolsWithAnimation(_ref7) {
var previousPointsRef = _ref7.previousPointsRef,
props = _ref7.props;
var points = props.points,
isAnimationActive = props.isAnimationActive,
animationBegin = props.animationBegin,
animationDuration = props.animationDuration,
animationEasing = props.animationEasing,
animationInterpolateFn = props.animationInterpolateFn;
var _useAnimationCallback = useAnimationCallbacks(),
isAnimating = _useAnimationCallback.isAnimating,
handleAnimationStart = _useAnimationCallback.handleAnimationStart,
handleAnimationEnd = _useAnimationCallback.handleAnimationEnd;
var layout = useCartesianChartLayout();
if (layout == null) return null;
return /*#__PURE__*/React.createElement(ScatterLabelListProvider, {
showLabels: !isAnimating,
points: points,
seriesFill: props.fill,
seriesStroke: props.stroke,
unthemedColors: props.unthemedColors
}, /*#__PURE__*/React.createElement(AnimatedItems, {
animationInput: props,
animationIdPrefix: "recharts-scatter-",
items: points,
previousItemsRef: previousPointsRef,
isAnimationActive: isAnimationActive,
animationBegin: animationBegin,
animationDuration: animationDuration,
animationEasing: animationEasing,
onAnimationStart: handleAnimationStart,
onAnimationEnd: handleAnimationEnd,
animationInterpolateFn: animationInterpolateFn,
animationMatchBy: props.animationMatchBy,
layout: layout
}, (stepData, animationElapsedTime, isEntrance) => /*#__PURE__*/React.createElement(Layer, null, /*#__PURE__*/React.createElement(ScatterSymbols, {
points: stepData,
allOtherScatterProps: props,
showLabels: !isAnimating,
animationElapsedTime: animationElapsedTime,
isAnimating: isAnimating || animationElapsedTime < 1,
isEntrance: isEntrance
}))), props.children, /*#__PURE__*/React.createElement(LabelListFromLabelProp, {
label: props.label
}));
}
export function computeScatterPoints(_ref8) {
var displayedData = _ref8.displayedData,
xAxis = _ref8.xAxis,
yAxis = _ref8.yAxis,
zAxis = _ref8.zAxis,
scatterSettings = _ref8.scatterSettings,
xAxisTicks = _ref8.xAxisTicks,
yAxisTicks = _ref8.yAxisTicks,
cells = _ref8.cells,
layout = _ref8.layout,
seriesFill = _ref8.seriesFill,
seriesStroke = _ref8.seriesStroke,
unthemedColors = _ref8.unthemedColors;
/*
* Scatter reads from two axes so it has two dataKeys.
* On the value axis, the Scatter dataKey wins and the axis is the fallback - same as all other graphical items.
* On the other axis, the axis dataKey wins.
*/
var xAxisDataKey = getDataKeyOnAxis(scatterSettings.dataKey, xAxis.dataKey, layout, 'xAxis');
var yAxisDataKey = getDataKeyOnAxis(scatterSettings.dataKey, yAxis.dataKey, layout, 'yAxis');
var zAxisDataKey = zAxis && zAxis.dataKey;
var defaultRangeZ = zAxis ? zAxis.range : implicitZAxis.range;
var defaultZ = defaultRangeZ && defaultRangeZ[0];
var xBandSize = xAxis.scale.bandwidth ? xAxis.scale.bandwidth() : 0;
var yBandSize = yAxis.scale.bandwidth ? yAxis.scale.bandwidth() : 0;
return displayedData.map((entry, index) => {
var x = getValueByDataKey(entry, xAxisDataKey);
var y = getValueByDataKey(entry, yAxisDataKey);
var rawZ = getValueByDataKey(entry, zAxisDataKey);
var z = isNullish(rawZ) || isNan(rawZ) ? '-' : rawZ;
var pointColor = getScatterEntryColor(entry, seriesFill, seriesStroke, unthemedColors);
var tooltipPayload = [{
name: isNullish(xAxis.dataKey) ? scatterSettings.name : xAxis.name || String(xAxisDataKey),
unit: xAxis.unit || '',
// @ts-expect-error getValueByDataKey does not validate the output type
value: x,
payload: entry,
dataKey: xAxisDataKey,
type: scatterSettings.tooltipType,
graphicalItemId: scatterSettings.id,
color: pointColor,
fill: pointColor
}, {
name: isNullish(yAxis.dataKey) ? scatterSettings.name : yAxis.name || String(yAxisDataKey),
unit: yAxis.unit || '',
// @ts-expect-error getValueByDataKey does not validate the output type
value: y,
payload: entry,
dataKey: yAxisDataKey,
type: scatterSettings.tooltipType,
graphicalItemId: scatterSettings.id,
color: pointColor,
fill: pointColor
}];
if (z !== '-' && zAxis != null) {
tooltipPayload.push({
// @ts-expect-error name prop should not have dataKey in it
name: zAxis.name || zAxis.dataKey,
unit: zAxis.unit || '',
// @ts-expect-error getValueByDataKey does not validate the output type
value: z,
payload: entry,
dataKey: zAxisDataKey,
type: scatterSettings.tooltipType,
graphicalItemId: scatterSettings.id,
color: pointColor,
fill: pointColor
});
}
var cx = getCateCoordinateOfLine({
axis: xAxis,
ticks: xAxisTicks,
bandSize: xBandSize,
entry,
index,
dataKey: xAxisDataKey
});
var cy = getCateCoordinateOfLine({
axis: yAxis,
ticks: yAxisTicks,
bandSize: yBandSize,
entry,
index,
dataKey: yAxisDataKey
});
var size = z !== '-' && zAxis != null ? zAxis.scale.map(z) : defaultZ;
var radius = size == null ? 0 : Math.sqrt(Math.max(size, 0) / Math.PI);
/*
* `id` from a data entry is never spread on rendered elements to avoid duplicate DOM ids.
* The raw entry, id included, stays available through `payload`.
*/
var entryWithoutId = typeof entry === 'object' && entry !== null ? _objectSpread({}, entry) : {};
delete entryWithoutId.id;
return withEntryFillColor(_objectSpread(_objectSpread({}, entryWithoutId), {}, {
cx,
cy,
x: cx == null ? undefined : cx - radius,
y: cy == null ? undefined : cy - radius,
width: 2 * radius,
height: 2 * radius,
size,
node: {
x,
y,
z
},
tooltipPayload,
tooltipPosition: {
x: cx,
y: cy
},
payload: entry
}, cells && cells[index] && cells[index].props), entry);
});
}
var errorBarDataPointFormatter = (dataPoint, dataKey, direction) => {
return {
x: dataPoint.cx,
y: dataPoint.cy,
value: direction === 'x' ? Number(dataPoint.node.x) : Number(dataPoint.node.y),
// @ts-expect-error getValueByDataKey does not validate the output type
errorVal: getValueByDataKey(dataPoint, dataKey)
};
};
function ScatterWithId(props) {
var hide = props.hide,
points = props.points,
className = props.className,
needClip = props.needClip,
xAxisId = props.xAxisId,
yAxisId = props.yAxisId,
id = props.id,
fill = props.fill,
stroke = props.stroke,
unthemedColors = props.unthemedColors;
var previousPointsRef = useRef(null);
if (hide) {
return null;
}
var layerClass = clsx('recharts-scatter', className);
var clipPathId = id;
var clipPath = needClip ? "url(#clipPath-".concat(clipPathId, ")") : undefined;
var hasEntryFills = hasEntryFillObjects(points.map(point => point.payload));
return /*#__PURE__*/React.createElement(ZIndexLayer, {
zIndex: props.zIndex
}, /*#__PURE__*/React.createElement(Layer, {
className: layerClass,
clipPath: clipPath,
id: id
}, (needClip || props.fillObject != null || hasEntryFills) && /*#__PURE__*/React.createElement("defs", null, needClip && /*#__PURE__*/React.createElement(GraphicalItemClipPath, {
clipPathId: clipPathId,
xAxisId: xAxisId,
yAxisId: yAxisId
}), props.fillObject != null && /*#__PURE__*/React.createElement(FillObjectDefinition, {
id: getSeriesFillId(id),
fillObject: props.fillObject,
color: fill
}), hasEntryFills && /*#__PURE__*/React.createElement(EntryFillDefinitions, {
id: id,
entries: points.map((point, index) => ({
index,
payload: point.payload,
color: getEntryShapeColors(point.payload, fill, stroke, unthemedColors).fill
}))
})), /*#__PURE__*/React.createElement(SetErrorBarContext, {
xAxisId: xAxisId,
yAxisId: yAxisId,
data: points,
dataPointFormatter: errorBarDataPointFormatter,
errorBarOffset: 0,
clipPath: clipPath
}, /*#__PURE__*/React.createElement(Layer, {
key: "recharts-scatter-symbols"
}, /*#__PURE__*/React.createElement(SetLabelListClipArea, {
xAxisId: xAxisId,
yAxisId: yAxisId
}, /*#__PURE__*/React.createElement(SymbolsWithAnimation, {
props: props,
previousPointsRef: previousPointsRef
}))))));
}
export var defaultScatterProps = {
xAxisId: 0,
yAxisId: 0,
zAxisId: 0,
label: false,
line: false,
legendType: 'circle',
lineType: 'joint',
lineJointType: 'linear',
shape: 'circle',
hide: false,
isAnimationActive: 'auto',
animationBegin: 0,
animationDuration: 400,
animationEasing: 'linear',
animationMatchBy: matchAppend,
animationInterpolateFn: defaultScatterAnimateItems,
zIndex: DefaultZIndexes.scatter
};
function ScatterImpl(props) {
var _resolveDefaultProps = resolveDefaultProps(props, defaultScatterProps),
animationBegin = _resolveDefaultProps.animationBegin,
animationDuration = _resolveDefaultProps.animationDuration,
animationEasing = _resolveDefaultProps.animationEasing,
hide = _resolveDefaultProps.hide,
isAnimationActive = _resolveDefaultProps.isAnimationActive,
legendType = _resolveDefaultProps.legendType,
lineJointType = _resolveDefaultProps.lineJointType,
lineType = _resolveDefaultProps.lineType,
shape = _resolveDefaultProps.shape,
xAxisId = _resolveDefaultProps.xAxisId,
yAxisId = _resolveDefaultProps.yAxisId,
zAxisId = _resolveDefaultProps.zAxisId,
everythingElse = _objectWithoutProperties(_resolveDefaultProps, _excluded3);
var _useNeedsClip = useNeedsClip(xAxisId, yAxisId),
needClip = _useNeedsClip.needClip;
var cells = useMemo(() => findAllByType(props.children, Cell), [props.children]);
var isPanorama = useIsPanorama();
var points = useAppSelector(state => {
return selectScatterPoints(state, xAxisId, yAxisId, zAxisId, props.id, cells, isPanorama, props.fill, props.stroke, props.unthemedColors);
});
if (needClip == null) {
return null;
}
if (points == null) {
return null;
}
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetScatterTooltipEntrySettings, {
dataKey: props.dataKey,
points: points,
stroke: props.stroke,
strokeWidth: props.strokeWidth,
fill: props.fill,
name: props.name,
hide: props.hide,
formatter: props.formatter,
tooltipType: props.tooltipType,
id: props.id
}), /*#__PURE__*/React.createElement(ScatterWithId, _extends({}, everythingElse, {
xAxisId: xAxisId,
yAxisId: yAxisId,
zAxisId: zAxisId,
lineType: lineType,
lineJointType: lineJointType,
legendType: legendType,
shape: shape,
hide: hide,
isAnimationActive: isAnimationActive,
animationBegin: animationBegin,
animationDuration: animationDuration,
animationEasing: animationEasing,
points: points,
needClip: needClip
})));
}
function ScatterFn(propsWithFillObject) {
var _outsideProps$xAxisId, _outsideProps$yAxisId, _outsideProps$legendT, _outsideProps$fill, _outsideProps$fillOpa, _outsideProps$stroke, _outsideProps$strokeO, _outsideProps$strokeW, _outsideProps$strokeD;
var _splitFill = splitFill(propsWithFillObject.fill),
fillColor = _splitFill.color,
fillObject = _splitFill.fillObject;
// From here on, `fill` is the base color
var outsideProps = _objectSpread(_objectSpread({}, propsWithFillObject), {}, {
fill: fillColor
});
var effectiveDataKey = useEffectiveDataKey({
dataKey: outsideProps.dataKey,
xAxisId: (_outsideProps$xAxisId = outsideProps.xAxisId) !== null && _outsideProps$xAxisId !== void 0 ? _outsideProps$xAxisId : defaultScatterProps.xAxisId,
yAxisId: (_outsideProps$yAxisId = outsideProps.yAxisId) !== null && _outsideProps$yAxisId !== void 0 ? _outsideProps$yAxisId : defaultScatterProps.yAxisId
});
var graphicalItemThemeSelector = useGraphicalItemIdentity(effectiveDataKey, outsideProps);
var themeFillObject = useGraphicalItemThemeFill(effectiveDataKey, outsideProps);
var effectiveFillObject = fillObject !== null && fillObject !== void 0 ? fillObject : themeFillObject;
var resolveLabelTheme = useSeriesLabelThemeResolver(graphicalItemThemeSelector);
var unthemedColors = useUnthemedColors(outsideProps);
var themedActiveStyles = useThemedActiveStyles(graphicalItemThemeSelector, outsideProps);
var graphicalItemTheme = useBackwardsCompatibleTheme(graphicalItemThemeSelector, outsideProps, undefined);
var props = resolveDefaultProps(_objectSpread(_objectSpread({}, outsideProps), {}, {
// the legend shows the same symbol as the points, unless legendType says otherwise
legendType: (_outsideProps$legendT = outsideProps.legendType) !== null && _outsideProps$legendT !== void 0 ? _outsideProps$legendT : typeof outsideProps.shape === 'string' ? outsideProps.shape : undefined,
fill: (_outsideProps$fill = outsideProps.fill) !== null && _outsideProps$fill !== void 0 ? _outsideProps$fill : graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.fill,
fillOpacity: (_outsideProps$fillOpa = outsideProps.fillOpacity) !== null && _outsideProps$fillOpa !== void 0 ? _outsideProps$fillOpa : graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.fillOpacity,
stroke: (_outsideProps$stroke = outsideProps.stroke) !== null && _outsideProps$stroke !== void 0 ? _outsideProps$stroke : graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.stroke,
strokeOpacity: (_outsideProps$strokeO = outsideProps.strokeOpacity) !== null && _outsideProps$strokeO !== void 0 ? _outsideProps$strokeO : graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.strokeOpacity,
strokeWidth: (_outsideProps$strokeW = outsideProps.strokeWidth) !== null && _outsideProps$strokeW !== void 0 ? _outsideProps$strokeW : graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.strokeWidth,
strokeDasharray: (_outsideProps$strokeD = outsideProps.strokeDasharray) !== null && _outsideProps$strokeD !== void 0 ? _outsideProps$strokeD : Array.isArray(graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.strokeDasharray) ? graphicalItemTheme.strokeDasharray.join(',') : graphicalItemTheme === null || graphicalItemTheme === void 0 ? void 0 : graphicalItemTheme.strokeDasharray
}), defaultScatterProps);
var isPanorama = useIsPanorama();
return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
id: props.id,
type: "scatter"
}, id => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetLegendPayload, {
legendPayload: computeLegendPayloadFromScatterProps(props, outsideProps.shape, effectiveFillObject)
}), /*#__PURE__*/React.createElement(SetCartesianGraphicalItem, {
type: "scatter",
id: id,
data: props.data,
xAxisId: props.xAxisId,
yAxisId: props.yAxisId,
zAxisId: props.zAxisId,
dataKey: props.dataKey,
hide: props.hide,
name: props.name,
tooltipType: props.tooltipType,
isPanorama: isPanorama
}), /*#__PURE__*/React.createElement(DataLabelThemeContextProvider, {
value: resolveLabelTheme
}, /*#__PURE__*/React.createElement(ScatterImpl, _extends({}, props, {
id: id,
fillObject: effectiveFillObject,
unthemedColors: unthemedColors,
themedActiveStyles: themedActiveStyles
})))));
}
/**
* @provides LabelListContext
* @provides ErrorBarContext
* @provides CellReader
* @consumes CartesianChartContext
* @public
*/
export var Scatter = /*#__PURE__*/React.memo(ScatterFn, propsAreEqual);
// @ts-expect-error we need to set the displayName for debugging purposes
Scatter.displayName = 'Scatter';