UNPKG

recharts

Version:
931 lines (909 loc) • 43.4 kB
var _excluded = ["key"], _excluded2 = ["onMouseEnter", "onClick", "onMouseLeave"], _excluded3 = ["id", "graphicalItems", "explicitStyleProps", "pieFillObject"], _excluded4 = ["id"]; 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 _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 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 get from 'es-toolkit/compat/get'; import { selectPieLegend, selectPieSectors } from '../state/selectors/pieSelectors'; import { useAppSelector } from '../state/hooks'; import { Layer } from '../container/Layer'; import { Curve } from '../shape/Curve'; import { Sector } from '../shape/Sector'; import { Text } from '../component/Text'; import { Cell } from '../component/Cell'; import { clsx } from '../util/clsx'; import { findAllByType } from '../util/ReactUtils'; import { getMaxRadius, polarToCartesian } from '../util/PolarUtils'; import { getPercentValue, interpolate, isNumber, mathSign } from '../util/DataUtils'; import { getTooltipNameProp, getValueByDataKey } from '../util/ChartUtils'; import { adaptEventsOfChild } from '../util/types'; import { Shape } from '../util/ActiveShapeUtils'; import { useMouseClickItemDispatch, useMouseEnterItemDispatch, useMouseLeaveItemDispatch } from '../context/tooltipContext'; import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings'; import { selectActiveTooltipDataKey, selectActiveTooltipGraphicalItemId, selectActiveTooltipIndex } from '../state/selectors/tooltipSelectors'; import { SetPolarLegendPayload } from '../state/SetLegendPayload'; import { DATA_ITEM_GRAPHICAL_ITEM_ID_ATTRIBUTE_NAME, DATA_ITEM_INDEX_ATTRIBUTE_NAME } from '../util/Constants'; import { AnimatedItems, useAnimationCallbacks } from '../animation/AnimatedItems'; import { matchAppend } from '../animation/matchBy'; import { resolveDefaultProps } from '../util/resolveDefaultProps'; import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId'; import { SetPolarGraphicalItem } from '../state/SetGraphicalItem'; import { isDataAttribute, isSvgElementPropKey, svgPropertiesNoEvents, svgPropertiesNoEventsFromUnknown } from '../util/svgPropertiesNoEvents'; import { LabelListFromLabelProp, PolarLabelListContextProvider } from '../component/LabelList'; import { PolarLabelContextProvider } from '../component/Label'; import { ZIndexLayer } from '../zIndex/ZIndexLayer'; import { DefaultZIndexes } from '../zIndex/DefaultZIndexes'; import { getClassNameFromUnknown } from '../util/getClassNameFromUnknown'; import { usePolarChartLayout } from '../context/chartLayoutContext'; import { DataLabelColorsContextProvider, DataLabelThemeContextProvider, applyLabelTheme, filledShapeLabelColors, useDataLabelThemeResolver, useIndexedLabelThemeResolver } from '../theme/labelTheme'; import { getColorGraphicalItems } from '../theme/themeFill'; import { FillObjectDefinition, getEntryFillId, getFillUrl, getIndexedEntryFillObject, readEntryFill, splitFill, useThemeFillObjects } from '../fill/fillDefinitions'; import { useBackwardsCompatibleTheme } from '../theme/useBackwardsCompatibleTheme'; import { getOwnStyles, hasOwnColors, withoutColors } from '../theme/dataEntryStyles'; import { getActiveStyleOverrides } from '../theme/activeStyles'; /** * Properties needed by custom label renderers that happen to share their names with SVG * attributes. Everything else that is a valid SVG property must not leak from a data entry * onto the label line or the label text: labels keep the entry color only. * https://github.com/recharts/recharts/issues/7974 */ var LABEL_GEOMETRY_PROP_KEYS = new Set(['cx', 'cy', 'r', 'name']); function filterEntryPropsForLabels(obj) { var result = {}; for (var key in obj) { if (!Object.prototype.hasOwnProperty.call(obj, key)) { continue; } if (key === 'id' || isDataAttribute(key)) { continue; } if (isSvgElementPropKey(key) && !LABEL_GEOMETRY_PROP_KEYS.has(key)) { continue; } result[key] = obj[key]; } return result; } /** * @public */ /** * @public */ /** * @public */ /** * The `label` prop in Pie accepts a variety of alternatives. * @public */ /** * We spread the data object into the sector data item, * so we can't really know what is going to be inside. * * This type represents our best effort, but it all depends on the input data * and what is inside of it. * * https://github.com/recharts/recharts/issues/6380 * https://github.com/recharts/recharts/discussions/6375 * @public */ /** * @public */ /** * @public */ /** * Internal props, combination of external props + defaultProps + private Recharts state */ /** * 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 */ /** * @public */ var defaultPieSectorShape = Sector; /** * Props after the fill object is split off: `fill` is the base color. */ /** * Resolves the styles of a single sector when a theme is present. * Returns undefined without a theme, in which case the legacy styles apply. * * The legacy default fill and stroke of the Pie are never part of the result, * because they never apply together with a theme. * * @param payload the data entry, merged with the props of its Cell * @param index index of the sector * @param graphicalItems graphical item styles from the theme * @param explicitStyleProps style props that the user set on the Pie, before applying defaults */ function getThemedPieStyles(payload, index, graphicalItems, explicitStyleProps) { var _ownStyles$fillOpacit, _ownStyles$strokeOpac, _ownStyles$strokeWidt, _ownStyles$strokeDash; var graphicalItemStyle = graphicalItems[index % graphicalItems.length]; if (graphicalItemStyle == null) { return undefined; } // A fill object in the data entry counts by its color only, so one without a color keeps the theme colors var entryStyles = payload != null && typeof payload === 'object' ? _objectSpread(_objectSpread({}, payload), {}, { fill: readEntryFill(payload).color }) : {}; // The data entry wins over the Pie props, and both win over the theme. var ownStyles = _objectSpread(_objectSpread({}, getOwnStyles(explicitStyleProps)), getOwnStyles(entryStyles)); /* * If the data entry (or its Cell), or the Pie itself, brings its own colors, * the sector drops the theme fill and stroke together, so that the colors never mix. * Styles without a color of their own merge with the theme field by field. */ var hasExplicitColors = hasOwnColors(entryStyles) || hasOwnColors(explicitStyleProps); return { fill: hasExplicitColors ? ownStyles.fill : graphicalItemStyle.fill, fillOpacity: (_ownStyles$fillOpacit = ownStyles.fillOpacity) !== null && _ownStyles$fillOpacit !== void 0 ? _ownStyles$fillOpacit : graphicalItemStyle.fillOpacity, stroke: hasExplicitColors ? ownStyles.stroke : graphicalItemStyle.stroke, strokeOpacity: (_ownStyles$strokeOpac = ownStyles.strokeOpacity) !== null && _ownStyles$strokeOpac !== void 0 ? _ownStyles$strokeOpac : graphicalItemStyle.strokeOpacity, strokeWidth: (_ownStyles$strokeWidt = ownStyles.strokeWidth) !== null && _ownStyles$strokeWidt !== void 0 ? _ownStyles$strokeWidt : graphicalItemStyle.strokeWidth, strokeDasharray: (_ownStyles$strokeDash = ownStyles.strokeDasharray) !== null && _ownStyles$strokeDash !== void 0 ? _ownStyles$strokeDash : graphicalItemStyle.strokeDasharray }; } /** * Legend and Tooltip show a single color for each sector. * Pie sectors are primarily filled, so the fill wins. A sector that has no fill shows its stroke instead. */ function getPieEntryColor(styles) { return styles.fill && styles.fill !== 'none' ? styles.fill : styles.stroke; } function getThemedPieSector(sector, index, graphicalItems, explicitStyleProps) { var _sector$tooltipPayloa; var styles = getThemedPieStyles(sector.payload, index, graphicalItems, explicitStyleProps); if (styles == null) { return sector; } return _objectSpread(_objectSpread(_objectSpread({}, sector), styles), {}, { tooltipPayload: (_sector$tooltipPayloa = sector.tooltipPayload) === null || _sector$tooltipPayloa === void 0 ? void 0 : _sector$tooltipPayloa.map(item => _objectSpread(_objectSpread({}, item), {}, { color: getPieEntryColor(styles), fill: styles.fill })) }); } function SetPiePayloadLegend(props) { var cells = useMemo(() => findAllByType(props.children, Cell), [props.children]); var themeFillObjects = useThemeFillObjects(); var legendPayload = useAppSelector(state => selectPieLegend(state, props.id, cells)); if (legendPayload == null) { return null; } var themedLegendPayload = legendPayload.map((entry, index) => { var _cells$index; var payloadWithCell = _objectSpread(_objectSpread({}, entry.payload), cells === null || cells === void 0 || (_cells$index = cells[index]) === null || _cells$index === void 0 ? void 0 : _cells$index.props); var fill = getIndexedEntryFillObject(payloadWithCell, index, props.pieFillObject, themeFillObjects, hasOwnColors(props.explicitStyleProps)); var styles = getThemedPieStyles(payloadWithCell, index, props.graphicalItems, props.explicitStyleProps); if (styles == null) { return fill == null ? entry : _objectSpread(_objectSpread({}, entry), {}, { fill }); } return _objectSpread(_objectSpread({}, entry), {}, { color: getPieEntryColor(styles), fill }); }); return /*#__PURE__*/React.createElement(SetPolarLegendPayload, { legendPayload: themedLegendPayload }); } function getActiveShapeFill(activeShape) { // activeShape can be boolean/function/element/object; only element/object can carry a static fill value. if (activeShape == null || typeof activeShape === 'boolean' || typeof activeShape === 'function') { return undefined; } if (/*#__PURE__*/React.isValidElement(activeShape)) { var _activeShape$props; // React element form: <Sector fill="..."/> or custom element with fill prop. var _fill = (_activeShape$props = activeShape.props) === null || _activeShape$props === void 0 ? void 0 : _activeShape$props.fill; return typeof _fill === 'string' ? _fill : undefined; } var fill = activeShape.fill; return typeof fill === 'string' ? fill : undefined; } var SetPieTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => { var dataKey = _ref.dataKey, nameKey = _ref.nameKey, sectors = _ref.sectors, stroke = _ref.stroke, strokeWidth = _ref.strokeWidth, fill = _ref.fill, name = _ref.name, hide = _ref.hide, tooltipType = _ref.tooltipType, formatter = _ref.formatter, id = _ref.id, activeShape = _ref.activeShape; var activeShapeFill = getActiveShapeFill(activeShape); var tooltipDataDefinedOnItem = sectors.map(sector => { var sectorTooltipPayload = sector.tooltipPayload; if (activeShapeFill == null || sectorTooltipPayload == null) { return sectorTooltipPayload; } return sectorTooltipPayload.map(item => _objectSpread(_objectSpread({}, item), {}, { color: activeShapeFill, fill: activeShapeFill })); }); var tooltipEntrySettings = { dataDefinedOnItem: tooltipDataDefinedOnItem, getPosition: index => { var _sectors$Number; return (_sectors$Number = sectors[Number(index)]) === null || _sectors$Number === void 0 ? void 0 : _sectors$Number.tooltipPosition; }, settings: { stroke, strokeWidth, fill, dataKey, nameKey, name: getTooltipNameProp(name, dataKey), hide, type: tooltipType, color: fill, unit: '', // why doesn't Pie support unit? formatter, graphicalItemId: id } }; return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, { tooltipEntrySettings: tooltipEntrySettings }); }); var getTextAnchor = (x, cx) => { if (x > cx) { return 'start'; } if (x < cx) { return 'end'; } return 'middle'; }; var getOuterRadius = (dataPoint, outerRadius, maxPieRadius) => { if (typeof outerRadius === 'function') { return getPercentValue(outerRadius(dataPoint), maxPieRadius, maxPieRadius * 0.8); } return getPercentValue(outerRadius, maxPieRadius, maxPieRadius * 0.8); }; var parseCoordinateOfPie = (pieSettings, offset, dataPoint) => { var _pieSettings$maxRadiu, _pieSettings$maxRadiu2; var top = offset.top, left = offset.left, width = offset.width, height = offset.height; var maxPieRadius = (_pieSettings$maxRadiu = pieSettings.maxRadius) !== null && _pieSettings$maxRadiu !== void 0 ? _pieSettings$maxRadiu : getMaxRadius(width, height); var cx = left + getPercentValue(pieSettings.cx, width, width / 2); var cy = top + getPercentValue(pieSettings.cy, height, height / 2); var innerRadius = getPercentValue(pieSettings.innerRadius, maxPieRadius, 0); var outerRadius = getOuterRadius(dataPoint, pieSettings.outerRadius, maxPieRadius); var maxRadius = (_pieSettings$maxRadiu2 = pieSettings.maxRadius) !== null && _pieSettings$maxRadiu2 !== void 0 ? _pieSettings$maxRadiu2 : Math.sqrt(width * width + height * height) / 2; return { cx, cy, innerRadius, outerRadius, maxRadius }; }; var parseDeltaAngle = (startAngle, endAngle) => { var sign = mathSign(endAngle - startAngle); var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360); return sign * deltaAngle; }; var renderLabelLineItem = (option, props) => { if (/*#__PURE__*/React.isValidElement(option)) { // @ts-expect-error we can't know if the type of props matches the element return /*#__PURE__*/React.cloneElement(option, props); } if (typeof option === 'function') { return option(props); } var className = clsx('recharts-pie-label-line', typeof option !== 'boolean' ? option.className : ''); // React doesn't like it when we spread a key property onto an element var key = props.key, otherProps = _objectWithoutProperties(props, _excluded); return /*#__PURE__*/React.createElement(Curve, _extends({}, otherProps, { type: "linear", className: className })); }; var renderLabelItem = (option, props, value, background) => { if (/*#__PURE__*/React.isValidElement(option)) { // @ts-expect-error element cloning is not typed return /*#__PURE__*/React.cloneElement(option, props); } var label = value; if (typeof option === 'function') { label = option(props); if (/*#__PURE__*/React.isValidElement(label)) { return label; } } var className = clsx('recharts-pie-label-text', getClassNameFromUnknown(option)); return /*#__PURE__*/React.createElement(Text, _extends({}, props, { alignmentBaseline: "middle", className: className, background: background }), label); }; /** * Returns the props of the default Pie label, if the `label` prop sets them as an object. */ function getPieLabelObject(label) { if (label == null || typeof label !== 'object' || /*#__PURE__*/React.isValidElement(label) || 'position' in label) { return undefined; } return label; } function PieLabels(_ref2) { var sectors = _ref2.sectors, props = _ref2.props, showLabels = _ref2.showLabels; var resolveLabelTheme = useDataLabelThemeResolver(); var label = props.label, labelLine = props.labelLine, dataKey = props.dataKey; if (!showLabels || !label || !sectors) { return null; } var pieProps = svgPropertiesNoEvents(props); var customLabelProps = svgPropertiesNoEventsFromUnknown(label); var labelObject = getPieLabelObject(label); var customLabelLineProps = svgPropertiesNoEventsFromUnknown(labelLine); var offsetRadius = typeof label === 'object' && 'offsetRadius' in label && typeof label.offsetRadius === 'number' && label.offsetRadius || 20; var labels = sectors.map((entry, i) => { var _labelObject$style; var midAngle = (entry.startAngle + entry.endAngle) / 2; var endPoint = polarToCartesian(entry.cx, entry.cy, entry.outerRadius + offsetRadius, midAngle); var entryLabelProps = filterEntryPropsForLabels(entry); var labelProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieProps), entryLabelProps), {}, { // @ts-expect-error entry fill is a valid label color fill: entry.fill, // @ts-expect-error customLabelProps is contributing unknown props stroke: 'none' }, customLabelProps), {}, { index: i, textAnchor: getTextAnchor(endPoint.x, entry.cx) }, endPoint); var themedLabelProps = applyLabelTheme(resolveLabelTheme === null || resolveLabelTheme === void 0 ? void 0 : resolveLabelTheme(i), { fill: labelObject === null || labelObject === void 0 ? void 0 : labelObject.fill, style: (_labelObject$style = labelObject === null || labelObject === void 0 ? void 0 : labelObject.style) !== null && _labelObject$style !== void 0 ? _labelObject$style : labelProps.style, background: labelObject === null || labelObject === void 0 ? void 0 : labelObject.background }, labelProps.fill); var finalLabelProps = _objectSpread(_objectSpread({}, labelProps), themedLabelProps); var lineProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieProps), entryLabelProps), {}, { // @ts-expect-error customLabelLineProps is contributing unknown props fill: 'none', // @ts-expect-error customLabelLineProps is contributing unknown props stroke: entry.fill }, customLabelLineProps), {}, { index: i, points: [polarToCartesian(entry.cx, entry.cy, entry.outerRadius, midAngle), endPoint], key: 'line' }); return /*#__PURE__*/React.createElement(ZIndexLayer, { zIndex: DefaultZIndexes.label, key: "label-".concat(entry.startAngle, "-").concat(entry.endAngle, "-").concat(entry.midAngle, "-").concat(i) }, /*#__PURE__*/React.createElement(Layer, null, labelLine && renderLabelLineItem(labelLine, lineProps), renderLabelItem(label, finalLabelProps, getValueByDataKey(entry.payload, dataKey, entry.value), themedLabelProps.background))); }); return /*#__PURE__*/React.createElement(Layer, { className: "recharts-pie-labels" }, labels); } function PieLabelList(_ref3) { var sectors = _ref3.sectors, props = _ref3.props, showLabels = _ref3.showLabels; var label = props.label; if (typeof label === 'object' && label != null && 'position' in label) { return /*#__PURE__*/React.createElement(LabelListFromLabelProp, { label: label }); } return /*#__PURE__*/React.createElement(PieLabels, { sectors: sectors, props: props, showLabels: showLabels }); } function PieSectors(props) { var sectors = props.sectors, activeShape = props.activeShape, inactiveShapeProp = props.inactiveShape, allOtherPieProps = props.allOtherPieProps, shape = props.shape, id = props.id, animationElapsedTime = props.animationElapsedTime, isAnimating = props.isAnimating, isEntrance = props.isEntrance, indexedActiveStyles = props.indexedActiveStyles; var activeIndex = useAppSelector(selectActiveTooltipIndex); var activeDataKey = useAppSelector(selectActiveTooltipDataKey); var activeGraphicalItemId = useAppSelector(selectActiveTooltipGraphicalItemId); var onMouseEnterFromProps = allOtherPieProps.onMouseEnter, onItemClickFromProps = allOtherPieProps.onClick, onMouseLeaveFromProps = allOtherPieProps.onMouseLeave, restOfAllOtherProps = _objectWithoutProperties(allOtherPieProps, _excluded2); var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, allOtherPieProps.dataKey, id); var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps); var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, allOtherPieProps.dataKey, id); if (sectors == null || sectors.length === 0) { return null; } return /*#__PURE__*/React.createElement(React.Fragment, null, sectors.map((entry, i) => { var _allOtherPieProps$sec, _allOtherPieProps$sec2; if ((entry === null || entry === void 0 ? void 0 : entry.startAngle) === 0 && (entry === null || entry === void 0 ? void 0 : entry.endAngle) === 0 && sectors.length !== 1) return null; // For Pie charts, when multiple Pies share the same dataKey, we need to ensure only the hovered Pie's sector is active. // We do this by checking if the active graphical item ID matches this Pie's ID. var graphicalItemMatches = activeGraphicalItemId == null || activeGraphicalItemId === id; var isActive = String(i) === activeIndex && (activeDataKey == null || allOtherPieProps.dataKey === activeDataKey) && graphicalItemMatches; var inactiveShape = activeIndex ? inactiveShapeProp : null; /* * Same as `activeBar` in Bar: `activeShape={true}` renders the `shape`, * so a custom `shape` renders the active sector too, and it decides on its own how the active state looks like. */ var option; var usesInactiveShape = false; if (isActive && activeShape) { option = activeShape === true ? shape : activeShape; } else if (inactiveShape != null) { option = inactiveShape; usesInactiveShape = true; } else { option = shape; } var sectorProps = _objectSpread(_objectSpread({}, entry), {}, { fill: (_allOtherPieProps$sec = (_allOtherPieProps$sec2 = allOtherPieProps.sectorPaintFills) === null || _allOtherPieProps$sec2 === void 0 ? void 0 : _allOtherPieProps$sec2[i]) !== null && _allOtherPieProps$sec !== void 0 ? _allOtherPieProps$sec : entry.fill, stroke: entry.stroke, tabIndex: -1, index: i, isActive, animationElapsedTime, isAnimating, isEntrance, [DATA_ITEM_INDEX_ATTRIBUTE_NAME]: i, [DATA_ITEM_GRAPHICAL_ITEM_ID_ATTRIBUTE_NAME]: id }); /* * The theme active styles only apply to the default sector, or to an `activeShape` object on top of it. * A custom `shape` or `activeShape` (function or element) receives none of them, * and neither does the deprecated `inactiveShape` when it renders the active sector. */ var activeStyleOverrides = isActive && !usesInactiveShape ? getActiveStyleOverrides(option === defaultPieSectorShape ? true : option, indexedActiveStyles[i % indexedActiveStyles.length], entry.payload) : undefined; var activeSectorProps = activeStyleOverrides == null ? sectorProps : _objectSpread(_objectSpread({}, sectorProps), activeStyleOverrides); return /*#__PURE__*/React.createElement(Layer, _extends({ key: "sector-".concat(entry === null || entry === void 0 ? void 0 : entry.startAngle, "-").concat(entry === null || entry === void 0 ? void 0 : entry.endAngle, "-").concat(entry.midAngle, "-").concat(i), tabIndex: -1, className: "recharts-pie-sector" }, adaptEventsOfChild(restOfAllOtherProps, entry, i), { onMouseEnter: onMouseEnterFromContext(entry, i), onMouseLeave: onMouseLeaveFromContext(entry, i), onClick: onClickFromContext(entry, i) }), /*#__PURE__*/React.createElement(Shape, { option: option, DefaultShape: defaultPieSectorShape, shapeProps: activeSectorProps })); })); } // getValueByDataKey returns null as-is (the default applies only to undefined), so treat it like 0. // See: https://github.com/recharts/recharts/issues/7965 function isNonZeroNumber(value) { return isNumber(value) && value !== 0; } export function computePieSectors(_ref4) { var _pieSettings$paddingA; var pieSettings = _ref4.pieSettings, displayedData = _ref4.displayedData, cells = _ref4.cells, offset = _ref4.offset; var cornerRadius = pieSettings.cornerRadius, startAngle = pieSettings.startAngle, endAngle = pieSettings.endAngle, dataKey = pieSettings.dataKey, nameKey = pieSettings.nameKey, tooltipType = pieSettings.tooltipType; var minAngle = Math.abs(pieSettings.minAngle); var deltaAngle = parseDeltaAngle(startAngle, endAngle); var absDeltaAngle = Math.abs(deltaAngle); var notZeroItemCount = displayedData.filter(entry => isNonZeroNumber(getValueByDataKey(entry, dataKey, 0))).length; var paddingAngle = notZeroItemCount <= 1 ? 0 : (_pieSettings$paddingA = pieSettings.paddingAngle) !== null && _pieSettings$paddingA !== void 0 ? _pieSettings$paddingA : 0; var firstNonZeroIndex = displayedData.findIndex(entry => isNonZeroNumber(getValueByDataKey(entry, dataKey, 0))); var totalPaddingAngle = (absDeltaAngle >= 360 ? notZeroItemCount : notZeroItemCount - 1) * paddingAngle; var sum = displayedData.reduce((result, entry) => { var val = getValueByDataKey(entry, dataKey, 0); return result + (isNumber(val) ? val : 0); }, 0); // Only apply minAngle redistribution when at least one non-zero segment's // natural angle falls below the minAngle threshold. Otherwise, minAngle // unnecessarily shifts all segments even when none need the boost. // See: https://github.com/recharts/recharts/issues/6814 var needsMinAngleAdjustment = minAngle > 0 && sum > 0 && displayedData.some(entry => { var val = getValueByDataKey(entry, dataKey, 0); var percent = (isNumber(val) ? val : 0) / sum; return isNonZeroNumber(val) && percent * absDeltaAngle < minAngle; }); var effectiveMinAngle = needsMinAngleAdjustment ? minAngle : 0; var realTotalAngle = absDeltaAngle - notZeroItemCount * effectiveMinAngle - totalPaddingAngle; var sectors; if (sum > 0) { var prev; sectors = displayedData.map((entry, i) => { var _entryFill$color; var val = getValueByDataKey(entry, dataKey, 0); var name = getValueByDataKey(entry, nameKey, i); var coordinate = parseCoordinateOfPie(pieSettings, offset, entry); var percent = (isNumber(val) ? val : 0) / sum; var tempStartAngle; // @ts-expect-error can't spread unknown var entryWithCellInfo = _objectSpread(_objectSpread({}, entry), cells && cells[i] && cells[i].props); // `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 = _objectSpread({}, entryWithCellInfo); delete entryWithoutId.id; var entryFill = readEntryFill(entryWithCellInfo); var sectorColor = (_entryFill$color = entryFill.color) !== null && _entryFill$color !== void 0 ? _entryFill$color : pieSettings.fill; // Leading zero values take no space, so the first visible sector gets no padding in front of it. var isFirstAfterLeadingZeros = i > 0 && i === firstNonZeroIndex; var hasPadding = isNonZeroNumber(val) && !isFirstAfterLeadingZeros; if (i) { tempStartAngle = prev.endAngle + mathSign(deltaAngle) * paddingAngle * (hasPadding ? 1 : 0); } else { tempStartAngle = startAngle; } var tempEndAngle = tempStartAngle + mathSign(deltaAngle) * ((isNonZeroNumber(val) ? effectiveMinAngle : 0) + percent * realTotalAngle); var midAngle = (tempStartAngle + tempEndAngle) / 2; var middleRadius = (coordinate.innerRadius + coordinate.outerRadius) / 2; var tooltipPayload = [{ name, value: val, payload: entryWithCellInfo, dataKey, type: tooltipType, color: sectorColor, fill: sectorColor, graphicalItemId: pieSettings.id }]; var tooltipPosition = polarToCartesian(coordinate.cx, coordinate.cy, middleRadius, midAngle); prev = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieSettings.presentationProps), {}, { percent, cornerRadius: typeof cornerRadius === 'string' ? parseFloat(cornerRadius) : cornerRadius, name, tooltipPayload, midAngle, middleRadius, tooltipPosition }, entryWithoutId), entryFill.fillObject == null ? undefined : { fill: sectorColor }), coordinate), {}, { value: val, dataKey, startAngle: tempStartAngle, endAngle: tempEndAngle, payload: entryWithCellInfo, paddingAngle: hasPadding ? mathSign(deltaAngle) * paddingAngle : 0 }); return prev; }); } return sectors; } function PieLabelListProvider(_ref5) { var showLabels = _ref5.showLabels, sectors = _ref5.sectors, children = _ref5.children; var labelListEntries = useMemo(() => { if (!showLabels || !sectors) { return []; } return sectors.map(entry => ({ value: entry.value, payload: entry.payload, clockWise: false, parentViewBox: undefined, viewBox: { cx: entry.cx, cy: entry.cy, innerRadius: entry.innerRadius, outerRadius: entry.outerRadius, startAngle: entry.startAngle, endAngle: entry.endAngle, clockWise: false }, fill: entry.fill })); }, [sectors, showLabels]); return /*#__PURE__*/React.createElement(PolarLabelListContextProvider, { value: showLabels ? labelListEntries : undefined }, children); } var defaultPieAnimateItems = (items, animationElapsedTime) => { if (items == null) return []; var stepData = []; var firstNonRemoved = items.find(item => item.status !== 'removed'); var curAngle = firstNonRemoved ? firstNonRemoved.next.startAngle : 0; items.forEach((item, index) => { if (item.status === 'removed') return; if (item.status === 'matched') { /* * The padding angle can itself change between prev and next (for example when a segment's * value crosses zero, its padding drops to 0). Interpolate it the same way as the arc angle * itself, otherwise the gap snaps to its final value on the very first animation frame, * throwing off every angle computed afterwards, including the closing gap of the last sector. */ var paddingAngle = index > 0 ? interpolate(get(item.prev, 'paddingAngle', 0), get(item.next, 'paddingAngle', 0), animationElapsedTime) : 0; var angle = interpolate(item.prev.endAngle - item.prev.startAngle, item.next.endAngle - item.next.startAngle, animationElapsedTime); var latest = _objectSpread(_objectSpread({}, item.next), {}, { startAngle: curAngle + paddingAngle, endAngle: curAngle + angle + paddingAngle }); stepData.push(latest); curAngle = latest.endAngle; } else { // added var _paddingAngle = index > 0 ? get(item.next, 'paddingAngle', 0) : 0; var deltaAngle = interpolate(0, item.next.endAngle - item.next.startAngle, animationElapsedTime); var _latest = _objectSpread(_objectSpread({}, item.next), {}, { startAngle: curAngle + _paddingAngle, endAngle: curAngle + deltaAngle + _paddingAngle }); stepData.push(_latest); curAngle = _latest.endAngle; } }); return stepData; }; function SectorsWithAnimation(_ref6) { var _firstSector$cx, _firstSector$cy, _firstSector$innerRad, _firstSector$outerRad; var props = _ref6.props, previousSectorsRef = _ref6.previousSectorsRef, id = _ref6.id, graphicalItems = _ref6.graphicalItems, indexedActiveStyles = _ref6.indexedActiveStyles; var sectors = props.sectors, activeShape = props.activeShape, inactiveShape = props.inactiveShape, animationInterpolateFn = props.animationInterpolateFn; var resolveLabelTheme = useIndexedLabelThemeResolver(graphicalItems); var _useAnimationCallback = useAnimationCallbacks(props.onAnimationStart, props.onAnimationEnd), isAnimating = _useAnimationCallback.isAnimating, handleAnimationStart = _useAnimationCallback.handleAnimationStart, handleAnimationEnd = _useAnimationCallback.handleAnimationEnd; var layout = usePolarChartLayout(); if (layout == null) return null; var firstSector = sectors[0]; return /*#__PURE__*/React.createElement(DataLabelThemeContextProvider, { value: resolveLabelTheme }, /*#__PURE__*/React.createElement(DataLabelColorsContextProvider, { value: filledShapeLabelColors }, /*#__PURE__*/React.createElement(PieLabelListProvider, { showLabels: !isAnimating, sectors: sectors }, /*#__PURE__*/React.createElement(AnimatedItems, { animationInput: props, animationIdPrefix: "recharts-pie-", items: sectors, previousItemsRef: previousSectorsRef, isAnimationActive: props.isAnimationActive, animationBegin: props.animationBegin, animationDuration: props.animationDuration, animationEasing: props.animationEasing, onAnimationStart: handleAnimationStart, onAnimationEnd: handleAnimationEnd, animationInterpolateFn: animationInterpolateFn, animationMatchBy: props.animationMatchBy, layout: layout }, (stepData, animationElapsedTime, isEntrance) => /*#__PURE__*/React.createElement(Layer, null, /*#__PURE__*/React.createElement(PieSectors, { sectors: stepData, activeShape: activeShape, inactiveShape: inactiveShape, allOtherPieProps: props, shape: props.shape, id: id, indexedActiveStyles: indexedActiveStyles, animationElapsedTime: animationElapsedTime, isAnimating: isAnimating || animationElapsedTime < 1, isEntrance: isEntrance }))), /*#__PURE__*/React.createElement(PieLabelList, { showLabels: !isAnimating, sectors: sectors, props: props }), /*#__PURE__*/React.createElement(PolarLabelContextProvider, { cx: (_firstSector$cx = firstSector === null || firstSector === void 0 ? void 0 : firstSector.cx) !== null && _firstSector$cx !== void 0 ? _firstSector$cx : 0, cy: (_firstSector$cy = firstSector === null || firstSector === void 0 ? void 0 : firstSector.cy) !== null && _firstSector$cy !== void 0 ? _firstSector$cy : 0, innerRadius: (_firstSector$innerRad = firstSector === null || firstSector === void 0 ? void 0 : firstSector.innerRadius) !== null && _firstSector$innerRad !== void 0 ? _firstSector$innerRad : 0, outerRadius: (_firstSector$outerRad = firstSector === null || firstSector === void 0 ? void 0 : firstSector.outerRadius) !== null && _firstSector$outerRad !== void 0 ? _firstSector$outerRad : 0, startAngle: props.startAngle, endAngle: props.endAngle, clockWise: false }, props.children)))); } export var defaultPieProps = { animationBegin: 400, animationDuration: 1500, animationEasing: 'ease', animationInterpolateFn: defaultPieAnimateItems, animationMatchBy: matchAppend, cx: '50%', cy: '50%', dataKey: 'value', endAngle: 360, fill: '#808080', hide: false, innerRadius: 0, isAnimationActive: 'auto', label: false, labelLine: true, legendType: 'rect', minAngle: 0, nameKey: 'name', outerRadius: '80%', paddingAngle: 0, rootTabIndex: 0, shape: defaultPieSectorShape, startAngle: 0, stroke: '#fff', zIndex: DefaultZIndexes.area }; function PieImpl(props) { var id = props.id, graphicalItems = props.graphicalItems, explicitStyleProps = props.explicitStyleProps, pieFillObject = props.pieFillObject, propsWithoutId = _objectWithoutProperties(props, _excluded3); var hide = props.hide, className = props.className, rootTabIndex = props.rootTabIndex; var themeFillObjects = useThemeFillObjects(); var cells = useMemo(() => findAllByType(props.children, Cell), [props.children]); // The theme active colors go with the theme colors, so a Pie with its own colors drops them in the active state too. var hasExplicitColors = hasOwnColors(explicitStyleProps); var indexedActiveStyles = useMemo(() => graphicalItems.map(style => hasExplicitColors ? withoutColors(style.active) : style.active), [graphicalItems, hasExplicitColors]); var sectors = useAppSelector(state => selectPieSectors(state, id, cells)); var previousSectorsRef = useRef(null); var layerClass = clsx('recharts-pie', className); if (hide || sectors == null) { previousSectorsRef.current = null; return /*#__PURE__*/React.createElement(Layer, { tabIndex: rootTabIndex, className: layerClass }); } var themedSectors = sectors.map((sector, index) => getThemedPieSector(sector, index, graphicalItems, explicitStyleProps)); var sectorFillObjects = themedSectors.map((sector, index) => getIndexedEntryFillObject(sector.payload, index, pieFillObject, themeFillObjects, hasExplicitColors)); var sectorPaintFills = sectorFillObjects.map((fillObject, index) => fillObject == null ? undefined : getFillUrl(getEntryFillId(id, index))); // The legacy default fill and stroke never apply together with a theme, and that is true in the Tooltip too. var isThemed = graphicalItems.length > 0; return /*#__PURE__*/React.createElement(ZIndexLayer, { zIndex: props.zIndex }, /*#__PURE__*/React.createElement(SetPieTooltipEntrySettings, { dataKey: props.dataKey, nameKey: props.nameKey, sectors: themedSectors, stroke: isThemed ? explicitStyleProps.stroke : props.stroke, strokeWidth: isThemed ? explicitStyleProps.strokeWidth : props.strokeWidth, fill: isThemed ? explicitStyleProps.fill : props.fill, name: props.name, hide: props.hide, tooltipType: props.tooltipType, formatter: props.formatter, id: id, activeShape: props.activeShape }), /*#__PURE__*/React.createElement(Layer, { tabIndex: rootTabIndex, className: layerClass }, sectorFillObjects.some(fillObject => fillObject != null) && /*#__PURE__*/React.createElement("defs", null, sectorFillObjects.map((fillObject, index) => { var _themedSectors$index; return fillObject == null ? null : /*#__PURE__*/React.createElement(FillObjectDefinition // the sector index is the identity of its definition , { key: index, id: getEntryFillId(id, index), fillObject: fillObject, color: (_themedSectors$index = themedSectors[index]) === null || _themedSectors$index === void 0 ? void 0 : _themedSectors$index.fill }); })), /*#__PURE__*/React.createElement(SectorsWithAnimation, { props: _objectSpread(_objectSpread({}, propsWithoutId), {}, { sectors: themedSectors, sectorPaintFills }), previousSectorsRef: previousSectorsRef, id: id, graphicalItems: graphicalItems, indexedActiveStyles: indexedActiveStyles }))); } /** * @consumes PolarChartContext * @provides LabelListContext * @provides CellReader */ function PieFn(propsWithFillObject) { var _splitFill = splitFill(propsWithFillObject.fill), fillColor = _splitFill.color, pieFillObject = _splitFill.fillObject; // From here on, `fill` is the base color var outsideProps = _objectSpread(_objectSpread({}, propsWithFillObject), {}, { fill: fillColor }); var props = resolveDefaultProps(outsideProps, defaultPieProps); var theme = useBackwardsCompatibleTheme(rechartsTheme => ({ graphicalItems: rechartsTheme.graphicalItems }), {}, undefined); var explicitStyleProps = { fill: outsideProps.fill, fillOpacity: outsideProps.fillOpacity, stroke: outsideProps.stroke, strokeOpacity: outsideProps.strokeOpacity, strokeWidth: outsideProps.strokeWidth, strokeDasharray: outsideProps.strokeDasharray }; var externalId = props.id, propsWithoutId = _objectWithoutProperties(props, _excluded4); var presentationProps = svgPropertiesNoEvents(propsWithoutId); return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, { id: externalId, type: "pie" }, id => { var _getColorGraphicalIte, _getColorGraphicalIte2; return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetPolarGraphicalItem, { type: "pie", id: id, data: propsWithoutId.data, dataKey: propsWithoutId.dataKey, hide: propsWithoutId.hide, angleAxisId: 0, radiusAxisId: 0, name: propsWithoutId.name, nameKey: propsWithoutId.nameKey, tooltipType: propsWithoutId.tooltipType, legendType: propsWithoutId.legendType, fill: propsWithoutId.fill, cx: propsWithoutId.cx, cy: propsWithoutId.cy, startAngle: propsWithoutId.startAngle, endAngle: propsWithoutId.endAngle, paddingAngle: propsWithoutId.paddingAngle, minAngle: propsWithoutId.minAngle, innerRadius: propsWithoutId.innerRadius, outerRadius: propsWithoutId.outerRadius, cornerRadius: propsWithoutId.cornerRadius, presentationProps: presentationProps, maxRadius: props.maxRadius }), /*#__PURE__*/React.createElement(SetPiePayloadLegend, _extends({}, propsWithoutId, { id: id, graphicalItems: (_getColorGraphicalIte = getColorGraphicalItems(theme === null || theme === void 0 ? void 0 : theme.graphicalItems)) !== null && _getColorGraphicalIte !== void 0 ? _getColorGraphicalIte : [], explicitStyleProps: explicitStyleProps, pieFillObject: pieFillObject })), /*#__PURE__*/React.createElement(PieImpl, _extends({}, propsWithoutId, { id: id, graphicalItems: (_getColorGraphicalIte2 = getColorGraphicalItems(theme === null || theme === void 0 ? void 0 : theme.graphicalItems)) !== null && _getColorGraphicalIte2 !== void 0 ? _getColorGraphicalIte2 : [], explicitStyleProps: explicitStyleProps, pieFillObject: pieFillObject }))); }); } /** * @public */ export var Pie = PieFn; // @ts-expect-error we need to set the displayName for debugging purposes Pie.displayName = 'Pie';