| 1 | var _excluded = ["key"],
|
|---|
| 2 | _excluded2 = ["onMouseEnter", "onClick", "onMouseLeave"],
|
|---|
| 3 | _excluded3 = ["id"],
|
|---|
| 4 | _excluded4 = ["id"];
|
|---|
| 5 | 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; }
|
|---|
| 6 | 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; }
|
|---|
| 7 | 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; }
|
|---|
| 8 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 9 | 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); }
|
|---|
| 10 | 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); }
|
|---|
| 11 | 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; }
|
|---|
| 12 | 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; }
|
|---|
| 13 | import * as React from 'react';
|
|---|
| 14 | import { useCallback, useMemo, useRef, useState } from 'react';
|
|---|
| 15 | import get from 'es-toolkit/compat/get';
|
|---|
| 16 | import { clsx } from 'clsx';
|
|---|
| 17 | import { selectPieLegend, selectPieSectors } from '../state/selectors/pieSelectors';
|
|---|
| 18 | import { useAppSelector } from '../state/hooks';
|
|---|
| 19 | import { Layer } from '../container/Layer';
|
|---|
| 20 | import { Curve } from '../shape/Curve';
|
|---|
| 21 | import { Text } from '../component/Text';
|
|---|
| 22 | import { Cell } from '../component/Cell';
|
|---|
| 23 | import { findAllByType } from '../util/ReactUtils';
|
|---|
| 24 | import { getMaxRadius, polarToCartesian } from '../util/PolarUtils';
|
|---|
| 25 | import { getPercentValue, interpolate, isNumber, mathSign } from '../util/DataUtils';
|
|---|
| 26 | import { getTooltipNameProp, getValueByDataKey } from '../util/ChartUtils';
|
|---|
| 27 | import { adaptEventsOfChild } from '../util/types';
|
|---|
| 28 | import { Shape } from '../util/ActiveShapeUtils';
|
|---|
| 29 | import { useMouseClickItemDispatch, useMouseEnterItemDispatch, useMouseLeaveItemDispatch } from '../context/tooltipContext';
|
|---|
| 30 | import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
|
|---|
| 31 | import { selectActiveTooltipDataKey, selectActiveTooltipGraphicalItemId, selectActiveTooltipIndex } from '../state/selectors/tooltipSelectors';
|
|---|
| 32 | import { SetPolarLegendPayload } from '../state/SetLegendPayload';
|
|---|
| 33 | import { DATA_ITEM_GRAPHICAL_ITEM_ID_ATTRIBUTE_NAME, DATA_ITEM_INDEX_ATTRIBUTE_NAME } from '../util/Constants';
|
|---|
| 34 | import { useAnimationId } from '../util/useAnimationId';
|
|---|
| 35 | import { resolveDefaultProps } from '../util/resolveDefaultProps';
|
|---|
| 36 | import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
|
|---|
| 37 | import { SetPolarGraphicalItem } from '../state/SetGraphicalItem';
|
|---|
| 38 | import { svgPropertiesNoEvents, svgPropertiesNoEventsFromUnknown } from '../util/svgPropertiesNoEvents';
|
|---|
| 39 | import { JavascriptAnimate } from '../animation/JavascriptAnimate';
|
|---|
| 40 | import { LabelListFromLabelProp, PolarLabelListContextProvider } from '../component/LabelList';
|
|---|
| 41 | import { ZIndexLayer } from '../zIndex/ZIndexLayer';
|
|---|
| 42 | import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|---|
| 43 | import { getClassNameFromUnknown } from '../util/getClassNameFromUnknown';
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * The `label` prop in Pie accepts a variety of alternatives.
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * We spread the data object into the sector data item,
|
|---|
| 51 | * so we can't really know what is going to be inside.
|
|---|
| 52 | *
|
|---|
| 53 | * This type represents our best effort, but it all depends on the input data
|
|---|
| 54 | * and what is inside of it.
|
|---|
| 55 | *
|
|---|
| 56 | * https://github.com/recharts/recharts/issues/6380
|
|---|
| 57 | * https://github.com/recharts/recharts/discussions/6375
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Internal props, combination of external props + defaultProps + private Recharts state
|
|---|
| 62 | */
|
|---|
| 63 |
|
|---|
| 64 | function SetPiePayloadLegend(props) {
|
|---|
| 65 | var cells = useMemo(() => findAllByType(props.children, Cell), [props.children]);
|
|---|
| 66 | var legendPayload = useAppSelector(state => selectPieLegend(state, props.id, cells));
|
|---|
| 67 | if (legendPayload == null) {
|
|---|
| 68 | return null;
|
|---|
| 69 | }
|
|---|
| 70 | return /*#__PURE__*/React.createElement(SetPolarLegendPayload, {
|
|---|
| 71 | legendPayload: legendPayload
|
|---|
| 72 | });
|
|---|
| 73 | }
|
|---|
| 74 | var SetPieTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
|
|---|
| 75 | var {
|
|---|
| 76 | dataKey,
|
|---|
| 77 | nameKey,
|
|---|
| 78 | sectors,
|
|---|
| 79 | stroke,
|
|---|
| 80 | strokeWidth,
|
|---|
| 81 | fill,
|
|---|
| 82 | name,
|
|---|
| 83 | hide,
|
|---|
| 84 | tooltipType,
|
|---|
| 85 | id
|
|---|
| 86 | } = _ref;
|
|---|
| 87 | var tooltipEntrySettings = {
|
|---|
| 88 | dataDefinedOnItem: sectors.map(p => p.tooltipPayload),
|
|---|
| 89 | getPosition: index => {
|
|---|
| 90 | var _sectors$Number;
|
|---|
| 91 | return (_sectors$Number = sectors[Number(index)]) === null || _sectors$Number === void 0 ? void 0 : _sectors$Number.tooltipPosition;
|
|---|
| 92 | },
|
|---|
| 93 | settings: {
|
|---|
| 94 | stroke,
|
|---|
| 95 | strokeWidth,
|
|---|
| 96 | fill,
|
|---|
| 97 | dataKey,
|
|---|
| 98 | nameKey,
|
|---|
| 99 | name: getTooltipNameProp(name, dataKey),
|
|---|
| 100 | hide,
|
|---|
| 101 | type: tooltipType,
|
|---|
| 102 | color: fill,
|
|---|
| 103 | unit: '',
|
|---|
| 104 | // why doesn't Pie support unit?
|
|---|
| 105 | graphicalItemId: id
|
|---|
| 106 | }
|
|---|
| 107 | };
|
|---|
| 108 | return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
|
|---|
| 109 | tooltipEntrySettings: tooltipEntrySettings
|
|---|
| 110 | });
|
|---|
| 111 | });
|
|---|
| 112 | var getTextAnchor = (x, cx) => {
|
|---|
| 113 | if (x > cx) {
|
|---|
| 114 | return 'start';
|
|---|
| 115 | }
|
|---|
| 116 | if (x < cx) {
|
|---|
| 117 | return 'end';
|
|---|
| 118 | }
|
|---|
| 119 | return 'middle';
|
|---|
| 120 | };
|
|---|
| 121 | var getOuterRadius = (dataPoint, outerRadius, maxPieRadius) => {
|
|---|
| 122 | if (typeof outerRadius === 'function') {
|
|---|
| 123 | return getPercentValue(outerRadius(dataPoint), maxPieRadius, maxPieRadius * 0.8);
|
|---|
| 124 | }
|
|---|
| 125 | return getPercentValue(outerRadius, maxPieRadius, maxPieRadius * 0.8);
|
|---|
| 126 | };
|
|---|
| 127 | var parseCoordinateOfPie = (pieSettings, offset, dataPoint) => {
|
|---|
| 128 | var {
|
|---|
| 129 | top,
|
|---|
| 130 | left,
|
|---|
| 131 | width,
|
|---|
| 132 | height
|
|---|
| 133 | } = offset;
|
|---|
| 134 | var maxPieRadius = getMaxRadius(width, height);
|
|---|
| 135 | var cx = left + getPercentValue(pieSettings.cx, width, width / 2);
|
|---|
| 136 | var cy = top + getPercentValue(pieSettings.cy, height, height / 2);
|
|---|
| 137 | var innerRadius = getPercentValue(pieSettings.innerRadius, maxPieRadius, 0);
|
|---|
| 138 | var outerRadius = getOuterRadius(dataPoint, pieSettings.outerRadius, maxPieRadius);
|
|---|
| 139 | var maxRadius = pieSettings.maxRadius || Math.sqrt(width * width + height * height) / 2;
|
|---|
| 140 | return {
|
|---|
| 141 | cx,
|
|---|
| 142 | cy,
|
|---|
| 143 | innerRadius,
|
|---|
| 144 | outerRadius,
|
|---|
| 145 | maxRadius
|
|---|
| 146 | };
|
|---|
| 147 | };
|
|---|
| 148 | var parseDeltaAngle = (startAngle, endAngle) => {
|
|---|
| 149 | var sign = mathSign(endAngle - startAngle);
|
|---|
| 150 | var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360);
|
|---|
| 151 | return sign * deltaAngle;
|
|---|
| 152 | };
|
|---|
| 153 | var renderLabelLineItem = (option, props) => {
|
|---|
| 154 | if (/*#__PURE__*/React.isValidElement(option)) {
|
|---|
| 155 | // @ts-expect-error we can't know if the type of props matches the element
|
|---|
| 156 | return /*#__PURE__*/React.cloneElement(option, props);
|
|---|
| 157 | }
|
|---|
| 158 | if (typeof option === 'function') {
|
|---|
| 159 | return option(props);
|
|---|
| 160 | }
|
|---|
| 161 | var className = clsx('recharts-pie-label-line', typeof option !== 'boolean' ? option.className : '');
|
|---|
| 162 | // React doesn't like it when we spread a key property onto an element
|
|---|
| 163 | var {
|
|---|
| 164 | key
|
|---|
| 165 | } = props,
|
|---|
| 166 | otherProps = _objectWithoutProperties(props, _excluded);
|
|---|
| 167 | return /*#__PURE__*/React.createElement(Curve, _extends({}, otherProps, {
|
|---|
| 168 | type: "linear",
|
|---|
| 169 | className: className
|
|---|
| 170 | }));
|
|---|
| 171 | };
|
|---|
| 172 | var renderLabelItem = (option, props, value) => {
|
|---|
| 173 | if (/*#__PURE__*/React.isValidElement(option)) {
|
|---|
| 174 | // @ts-expect-error element cloning is not typed
|
|---|
| 175 | return /*#__PURE__*/React.cloneElement(option, props);
|
|---|
| 176 | }
|
|---|
| 177 | var label = value;
|
|---|
| 178 | if (typeof option === 'function') {
|
|---|
| 179 | label = option(props);
|
|---|
| 180 | if (/*#__PURE__*/React.isValidElement(label)) {
|
|---|
| 181 | return label;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | var className = clsx('recharts-pie-label-text', getClassNameFromUnknown(option));
|
|---|
| 185 | return /*#__PURE__*/React.createElement(Text, _extends({}, props, {
|
|---|
| 186 | alignmentBaseline: "middle",
|
|---|
| 187 | className: className
|
|---|
| 188 | }), label);
|
|---|
| 189 | };
|
|---|
| 190 | function PieLabels(_ref2) {
|
|---|
| 191 | var {
|
|---|
| 192 | sectors,
|
|---|
| 193 | props,
|
|---|
| 194 | showLabels
|
|---|
| 195 | } = _ref2;
|
|---|
| 196 | var {
|
|---|
| 197 | label,
|
|---|
| 198 | labelLine,
|
|---|
| 199 | dataKey
|
|---|
| 200 | } = props;
|
|---|
| 201 | if (!showLabels || !label || !sectors) {
|
|---|
| 202 | return null;
|
|---|
| 203 | }
|
|---|
| 204 | var pieProps = svgPropertiesNoEvents(props);
|
|---|
| 205 | var customLabelProps = svgPropertiesNoEventsFromUnknown(label);
|
|---|
| 206 | var customLabelLineProps = svgPropertiesNoEventsFromUnknown(labelLine);
|
|---|
| 207 | var offsetRadius = typeof label === 'object' && 'offsetRadius' in label && typeof label.offsetRadius === 'number' && label.offsetRadius || 20;
|
|---|
| 208 | var labels = sectors.map((entry, i) => {
|
|---|
| 209 | var midAngle = (entry.startAngle + entry.endAngle) / 2;
|
|---|
| 210 | var endPoint = polarToCartesian(entry.cx, entry.cy, entry.outerRadius + offsetRadius, midAngle);
|
|---|
| 211 | var labelProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieProps), entry), {}, {
|
|---|
| 212 | // @ts-expect-error customLabelProps is contributing unknown props
|
|---|
| 213 | stroke: 'none'
|
|---|
| 214 | }, customLabelProps), {}, {
|
|---|
| 215 | index: i,
|
|---|
| 216 | textAnchor: getTextAnchor(endPoint.x, entry.cx)
|
|---|
| 217 | }, endPoint);
|
|---|
| 218 | var lineProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieProps), entry), {}, {
|
|---|
| 219 | // @ts-expect-error customLabelLineProps is contributing unknown props
|
|---|
| 220 | fill: 'none',
|
|---|
| 221 | // @ts-expect-error customLabelLineProps is contributing unknown props
|
|---|
| 222 | stroke: entry.fill
|
|---|
| 223 | }, customLabelLineProps), {}, {
|
|---|
| 224 | index: i,
|
|---|
| 225 | points: [polarToCartesian(entry.cx, entry.cy, entry.outerRadius, midAngle), endPoint],
|
|---|
| 226 | key: 'line'
|
|---|
| 227 | });
|
|---|
| 228 | return /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 229 | zIndex: DefaultZIndexes.label,
|
|---|
| 230 | key: "label-".concat(entry.startAngle, "-").concat(entry.endAngle, "-").concat(entry.midAngle, "-").concat(i)
|
|---|
| 231 | }, /*#__PURE__*/React.createElement(Layer, null, labelLine && renderLabelLineItem(labelLine, lineProps), renderLabelItem(label, labelProps, getValueByDataKey(entry, dataKey))));
|
|---|
| 232 | });
|
|---|
| 233 | return /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 234 | className: "recharts-pie-labels"
|
|---|
| 235 | }, labels);
|
|---|
| 236 | }
|
|---|
| 237 | function PieLabelList(_ref3) {
|
|---|
| 238 | var {
|
|---|
| 239 | sectors,
|
|---|
| 240 | props,
|
|---|
| 241 | showLabels
|
|---|
| 242 | } = _ref3;
|
|---|
| 243 | var {
|
|---|
| 244 | label
|
|---|
| 245 | } = props;
|
|---|
| 246 | if (typeof label === 'object' && label != null && 'position' in label) {
|
|---|
| 247 | return /*#__PURE__*/React.createElement(LabelListFromLabelProp, {
|
|---|
| 248 | label: label
|
|---|
| 249 | });
|
|---|
| 250 | }
|
|---|
| 251 | return /*#__PURE__*/React.createElement(PieLabels, {
|
|---|
| 252 | sectors: sectors,
|
|---|
| 253 | props: props,
|
|---|
| 254 | showLabels: showLabels
|
|---|
| 255 | });
|
|---|
| 256 | }
|
|---|
| 257 | function PieSectors(props) {
|
|---|
| 258 | var {
|
|---|
| 259 | sectors,
|
|---|
| 260 | activeShape,
|
|---|
| 261 | inactiveShape: inactiveShapeProp,
|
|---|
| 262 | allOtherPieProps,
|
|---|
| 263 | shape,
|
|---|
| 264 | id
|
|---|
| 265 | } = props;
|
|---|
| 266 | var activeIndex = useAppSelector(selectActiveTooltipIndex);
|
|---|
| 267 | var activeDataKey = useAppSelector(selectActiveTooltipDataKey);
|
|---|
| 268 | var activeGraphicalItemId = useAppSelector(selectActiveTooltipGraphicalItemId);
|
|---|
| 269 | var {
|
|---|
| 270 | onMouseEnter: onMouseEnterFromProps,
|
|---|
| 271 | onClick: onItemClickFromProps,
|
|---|
| 272 | onMouseLeave: onMouseLeaveFromProps
|
|---|
| 273 | } = allOtherPieProps,
|
|---|
| 274 | restOfAllOtherProps = _objectWithoutProperties(allOtherPieProps, _excluded2);
|
|---|
| 275 | var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, allOtherPieProps.dataKey, id);
|
|---|
| 276 | var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps);
|
|---|
| 277 | var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, allOtherPieProps.dataKey, id);
|
|---|
| 278 | if (sectors == null || sectors.length === 0) {
|
|---|
| 279 | return null;
|
|---|
| 280 | }
|
|---|
| 281 | return /*#__PURE__*/React.createElement(React.Fragment, null, sectors.map((entry, i) => {
|
|---|
| 282 | 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;
|
|---|
| 283 |
|
|---|
| 284 | // For Pie charts, when multiple Pies share the same dataKey, we need to ensure only the hovered Pie's sector is active.
|
|---|
| 285 | // We do this by checking if the active graphical item ID matches this Pie's ID.
|
|---|
| 286 | var graphicalItemMatches = activeGraphicalItemId == null || activeGraphicalItemId === id;
|
|---|
| 287 | var isActive = String(i) === activeIndex && (activeDataKey == null || allOtherPieProps.dataKey === activeDataKey) && graphicalItemMatches;
|
|---|
| 288 | var inactiveShape = activeIndex ? inactiveShapeProp : null;
|
|---|
| 289 | var sectorOptions = activeShape && isActive ? activeShape : inactiveShape;
|
|---|
| 290 | var sectorProps = _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 291 | stroke: entry.stroke,
|
|---|
| 292 | tabIndex: -1,
|
|---|
| 293 | [DATA_ITEM_INDEX_ATTRIBUTE_NAME]: i,
|
|---|
| 294 | [DATA_ITEM_GRAPHICAL_ITEM_ID_ATTRIBUTE_NAME]: id
|
|---|
| 295 | });
|
|---|
| 296 | return /*#__PURE__*/React.createElement(Layer, _extends({
|
|---|
| 297 | 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),
|
|---|
| 298 | tabIndex: -1,
|
|---|
| 299 | className: "recharts-pie-sector"
|
|---|
| 300 | }, adaptEventsOfChild(restOfAllOtherProps, entry, i), {
|
|---|
| 301 | // @ts-expect-error the types need a bit of attention
|
|---|
| 302 | onMouseEnter: onMouseEnterFromContext(entry, i)
|
|---|
| 303 | // @ts-expect-error the types need a bit of attention
|
|---|
| 304 | ,
|
|---|
| 305 | onMouseLeave: onMouseLeaveFromContext(entry, i)
|
|---|
| 306 | // @ts-expect-error the types need a bit of attention
|
|---|
| 307 | ,
|
|---|
| 308 | onClick: onClickFromContext(entry, i)
|
|---|
| 309 | }), /*#__PURE__*/React.createElement(Shape, _extends({
|
|---|
| 310 | option: shape !== null && shape !== void 0 ? shape : sectorOptions,
|
|---|
| 311 | index: i,
|
|---|
| 312 | shapeType: "sector",
|
|---|
| 313 | isActive: isActive
|
|---|
| 314 | }, sectorProps)));
|
|---|
| 315 | }));
|
|---|
| 316 | }
|
|---|
| 317 | export function computePieSectors(_ref4) {
|
|---|
| 318 | var _pieSettings$paddingA;
|
|---|
| 319 | var {
|
|---|
| 320 | pieSettings,
|
|---|
| 321 | displayedData,
|
|---|
| 322 | cells,
|
|---|
| 323 | offset
|
|---|
| 324 | } = _ref4;
|
|---|
| 325 | var {
|
|---|
| 326 | cornerRadius,
|
|---|
| 327 | startAngle,
|
|---|
| 328 | endAngle,
|
|---|
| 329 | dataKey,
|
|---|
| 330 | nameKey,
|
|---|
| 331 | tooltipType
|
|---|
| 332 | } = pieSettings;
|
|---|
| 333 | var minAngle = Math.abs(pieSettings.minAngle);
|
|---|
| 334 | var deltaAngle = parseDeltaAngle(startAngle, endAngle);
|
|---|
| 335 | var absDeltaAngle = Math.abs(deltaAngle);
|
|---|
| 336 | var paddingAngle = displayedData.length <= 1 ? 0 : (_pieSettings$paddingA = pieSettings.paddingAngle) !== null && _pieSettings$paddingA !== void 0 ? _pieSettings$paddingA : 0;
|
|---|
| 337 | var notZeroItemCount = displayedData.filter(entry => getValueByDataKey(entry, dataKey, 0) !== 0).length;
|
|---|
| 338 | var totalPaddingAngle = (absDeltaAngle >= 360 ? notZeroItemCount : notZeroItemCount - 1) * paddingAngle;
|
|---|
| 339 | var realTotalAngle = absDeltaAngle - notZeroItemCount * minAngle - totalPaddingAngle;
|
|---|
| 340 | var sum = displayedData.reduce((result, entry) => {
|
|---|
| 341 | var val = getValueByDataKey(entry, dataKey, 0);
|
|---|
| 342 | return result + (isNumber(val) ? val : 0);
|
|---|
| 343 | }, 0);
|
|---|
| 344 | var sectors;
|
|---|
| 345 | if (sum > 0) {
|
|---|
| 346 | var prev;
|
|---|
| 347 | sectors = displayedData.map((entry, i) => {
|
|---|
| 348 | // @ts-expect-error getValueByDataKey does not validate the output type
|
|---|
| 349 | var val = getValueByDataKey(entry, dataKey, 0);
|
|---|
| 350 | // @ts-expect-error getValueByDataKey does not validate the output type
|
|---|
| 351 | var name = getValueByDataKey(entry, nameKey, i);
|
|---|
| 352 | var coordinate = parseCoordinateOfPie(pieSettings, offset, entry);
|
|---|
| 353 | var percent = (isNumber(val) ? val : 0) / sum;
|
|---|
| 354 | var tempStartAngle;
|
|---|
| 355 |
|
|---|
| 356 | // @ts-expect-error can't spread unknown
|
|---|
| 357 | var entryWithCellInfo = _objectSpread(_objectSpread({}, entry), cells && cells[i] && cells[i].props);
|
|---|
| 358 | if (i) {
|
|---|
| 359 | tempStartAngle = prev.endAngle + mathSign(deltaAngle) * paddingAngle * (val !== 0 ? 1 : 0);
|
|---|
| 360 | } else {
|
|---|
| 361 | tempStartAngle = startAngle;
|
|---|
| 362 | }
|
|---|
| 363 | var tempEndAngle = tempStartAngle + mathSign(deltaAngle) * ((val !== 0 ? minAngle : 0) + percent * realTotalAngle);
|
|---|
| 364 | var midAngle = (tempStartAngle + tempEndAngle) / 2;
|
|---|
| 365 | var middleRadius = (coordinate.innerRadius + coordinate.outerRadius) / 2;
|
|---|
| 366 | var tooltipPayload = [{
|
|---|
| 367 | name,
|
|---|
| 368 | value: val,
|
|---|
| 369 | payload: entryWithCellInfo,
|
|---|
| 370 | dataKey,
|
|---|
| 371 | type: tooltipType,
|
|---|
| 372 | graphicalItemId: pieSettings.id
|
|---|
| 373 | }];
|
|---|
| 374 | var tooltipPosition = polarToCartesian(coordinate.cx, coordinate.cy, middleRadius, midAngle);
|
|---|
| 375 | prev = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieSettings.presentationProps), {}, {
|
|---|
| 376 | percent,
|
|---|
| 377 | cornerRadius: typeof cornerRadius === 'string' ? parseFloat(cornerRadius) : cornerRadius,
|
|---|
| 378 | name,
|
|---|
| 379 | tooltipPayload,
|
|---|
| 380 | midAngle,
|
|---|
| 381 | middleRadius,
|
|---|
| 382 | tooltipPosition
|
|---|
| 383 | }, entryWithCellInfo), coordinate), {}, {
|
|---|
| 384 | value: val,
|
|---|
| 385 | dataKey,
|
|---|
| 386 | startAngle: tempStartAngle,
|
|---|
| 387 | endAngle: tempEndAngle,
|
|---|
| 388 | payload: entryWithCellInfo,
|
|---|
| 389 | paddingAngle: mathSign(deltaAngle) * paddingAngle
|
|---|
| 390 | });
|
|---|
| 391 | return prev;
|
|---|
| 392 | });
|
|---|
| 393 | }
|
|---|
| 394 | return sectors;
|
|---|
| 395 | }
|
|---|
| 396 | function PieLabelListProvider(_ref5) {
|
|---|
| 397 | var {
|
|---|
| 398 | showLabels,
|
|---|
| 399 | sectors,
|
|---|
| 400 | children
|
|---|
| 401 | } = _ref5;
|
|---|
| 402 | var labelListEntries = useMemo(() => {
|
|---|
| 403 | if (!showLabels || !sectors) {
|
|---|
| 404 | return [];
|
|---|
| 405 | }
|
|---|
| 406 | return sectors.map(entry => ({
|
|---|
| 407 | value: entry.value,
|
|---|
| 408 | payload: entry.payload,
|
|---|
| 409 | clockWise: false,
|
|---|
| 410 | parentViewBox: undefined,
|
|---|
| 411 | viewBox: {
|
|---|
| 412 | cx: entry.cx,
|
|---|
| 413 | cy: entry.cy,
|
|---|
| 414 | innerRadius: entry.innerRadius,
|
|---|
| 415 | outerRadius: entry.outerRadius,
|
|---|
| 416 | startAngle: entry.startAngle,
|
|---|
| 417 | endAngle: entry.endAngle,
|
|---|
| 418 | clockWise: false
|
|---|
| 419 | },
|
|---|
| 420 | fill: entry.fill
|
|---|
| 421 | }));
|
|---|
| 422 | }, [sectors, showLabels]);
|
|---|
| 423 | return /*#__PURE__*/React.createElement(PolarLabelListContextProvider, {
|
|---|
| 424 | value: showLabels ? labelListEntries : undefined
|
|---|
| 425 | }, children);
|
|---|
| 426 | }
|
|---|
| 427 | function SectorsWithAnimation(_ref6) {
|
|---|
| 428 | var {
|
|---|
| 429 | props,
|
|---|
| 430 | previousSectorsRef,
|
|---|
| 431 | id
|
|---|
| 432 | } = _ref6;
|
|---|
| 433 | var {
|
|---|
| 434 | sectors,
|
|---|
| 435 | isAnimationActive,
|
|---|
| 436 | animationBegin,
|
|---|
| 437 | animationDuration,
|
|---|
| 438 | animationEasing,
|
|---|
| 439 | activeShape,
|
|---|
| 440 | inactiveShape,
|
|---|
| 441 | onAnimationStart,
|
|---|
| 442 | onAnimationEnd
|
|---|
| 443 | } = props;
|
|---|
| 444 | var animationId = useAnimationId(props, 'recharts-pie-');
|
|---|
| 445 | var prevSectors = previousSectorsRef.current;
|
|---|
| 446 | var [isAnimating, setIsAnimating] = useState(false);
|
|---|
| 447 | var handleAnimationEnd = useCallback(() => {
|
|---|
| 448 | if (typeof onAnimationEnd === 'function') {
|
|---|
| 449 | onAnimationEnd();
|
|---|
| 450 | }
|
|---|
| 451 | setIsAnimating(false);
|
|---|
| 452 | }, [onAnimationEnd]);
|
|---|
| 453 | var handleAnimationStart = useCallback(() => {
|
|---|
| 454 | if (typeof onAnimationStart === 'function') {
|
|---|
| 455 | onAnimationStart();
|
|---|
| 456 | }
|
|---|
| 457 | setIsAnimating(true);
|
|---|
| 458 | }, [onAnimationStart]);
|
|---|
| 459 | return /*#__PURE__*/React.createElement(PieLabelListProvider, {
|
|---|
| 460 | showLabels: !isAnimating,
|
|---|
| 461 | sectors: sectors
|
|---|
| 462 | }, /*#__PURE__*/React.createElement(JavascriptAnimate, {
|
|---|
| 463 | animationId: animationId,
|
|---|
| 464 | begin: animationBegin,
|
|---|
| 465 | duration: animationDuration,
|
|---|
| 466 | isActive: isAnimationActive,
|
|---|
| 467 | easing: animationEasing,
|
|---|
| 468 | onAnimationStart: handleAnimationStart,
|
|---|
| 469 | onAnimationEnd: handleAnimationEnd,
|
|---|
| 470 | key: animationId
|
|---|
| 471 | }, t => {
|
|---|
| 472 | var _first$startAngle;
|
|---|
| 473 | var stepData = [];
|
|---|
| 474 | var first = sectors && sectors[0];
|
|---|
| 475 | var curAngle = (_first$startAngle = first === null || first === void 0 ? void 0 : first.startAngle) !== null && _first$startAngle !== void 0 ? _first$startAngle : 0;
|
|---|
| 476 | sectors === null || sectors === void 0 || sectors.forEach((entry, index) => {
|
|---|
| 477 | var prev = prevSectors && prevSectors[index];
|
|---|
| 478 | var paddingAngle = index > 0 ? get(entry, 'paddingAngle', 0) : 0;
|
|---|
| 479 | if (prev) {
|
|---|
| 480 | var angle = interpolate(prev.endAngle - prev.startAngle, entry.endAngle - entry.startAngle, t);
|
|---|
| 481 | var latest = _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 482 | startAngle: curAngle + paddingAngle,
|
|---|
| 483 | endAngle: curAngle + angle + paddingAngle
|
|---|
| 484 | });
|
|---|
| 485 | stepData.push(latest);
|
|---|
| 486 | curAngle = latest.endAngle;
|
|---|
| 487 | } else {
|
|---|
| 488 | var {
|
|---|
| 489 | endAngle,
|
|---|
| 490 | startAngle
|
|---|
| 491 | } = entry;
|
|---|
| 492 | var deltaAngle = interpolate(0, endAngle - startAngle, t);
|
|---|
| 493 | var _latest = _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 494 | startAngle: curAngle + paddingAngle,
|
|---|
| 495 | endAngle: curAngle + deltaAngle + paddingAngle
|
|---|
| 496 | });
|
|---|
| 497 | stepData.push(_latest);
|
|---|
| 498 | curAngle = _latest.endAngle;
|
|---|
| 499 | }
|
|---|
| 500 | });
|
|---|
| 501 |
|
|---|
| 502 | // eslint-disable-next-line no-param-reassign
|
|---|
| 503 | previousSectorsRef.current = stepData;
|
|---|
| 504 | return /*#__PURE__*/React.createElement(Layer, null, /*#__PURE__*/React.createElement(PieSectors, {
|
|---|
| 505 | sectors: stepData,
|
|---|
| 506 | activeShape: activeShape,
|
|---|
| 507 | inactiveShape: inactiveShape,
|
|---|
| 508 | allOtherPieProps: props,
|
|---|
| 509 | shape: props.shape,
|
|---|
| 510 | id: id
|
|---|
| 511 | }));
|
|---|
| 512 | }), /*#__PURE__*/React.createElement(PieLabelList, {
|
|---|
| 513 | showLabels: !isAnimating,
|
|---|
| 514 | sectors: sectors,
|
|---|
| 515 | props: props
|
|---|
| 516 | }), props.children);
|
|---|
| 517 | }
|
|---|
| 518 | export var defaultPieProps = {
|
|---|
| 519 | animationBegin: 400,
|
|---|
| 520 | animationDuration: 1500,
|
|---|
| 521 | animationEasing: 'ease',
|
|---|
| 522 | cx: '50%',
|
|---|
| 523 | cy: '50%',
|
|---|
| 524 | dataKey: 'value',
|
|---|
| 525 | endAngle: 360,
|
|---|
| 526 | fill: '#808080',
|
|---|
| 527 | hide: false,
|
|---|
| 528 | innerRadius: 0,
|
|---|
| 529 | isAnimationActive: 'auto',
|
|---|
| 530 | label: false,
|
|---|
| 531 | labelLine: true,
|
|---|
| 532 | legendType: 'rect',
|
|---|
| 533 | minAngle: 0,
|
|---|
| 534 | nameKey: 'name',
|
|---|
| 535 | outerRadius: '80%',
|
|---|
| 536 | paddingAngle: 0,
|
|---|
| 537 | rootTabIndex: 0,
|
|---|
| 538 | startAngle: 0,
|
|---|
| 539 | stroke: '#fff',
|
|---|
| 540 | zIndex: DefaultZIndexes.area
|
|---|
| 541 | };
|
|---|
| 542 | function PieImpl(props) {
|
|---|
| 543 | var {
|
|---|
| 544 | id
|
|---|
| 545 | } = props,
|
|---|
| 546 | propsWithoutId = _objectWithoutProperties(props, _excluded3);
|
|---|
| 547 | var {
|
|---|
| 548 | hide,
|
|---|
| 549 | className,
|
|---|
| 550 | rootTabIndex
|
|---|
| 551 | } = props;
|
|---|
| 552 | var cells = useMemo(() => findAllByType(props.children, Cell), [props.children]);
|
|---|
| 553 | var sectors = useAppSelector(state => selectPieSectors(state, id, cells));
|
|---|
| 554 | var previousSectorsRef = useRef(null);
|
|---|
| 555 | var layerClass = clsx('recharts-pie', className);
|
|---|
| 556 | if (hide || sectors == null) {
|
|---|
| 557 | previousSectorsRef.current = null;
|
|---|
| 558 | return /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 559 | tabIndex: rootTabIndex,
|
|---|
| 560 | className: layerClass
|
|---|
| 561 | });
|
|---|
| 562 | }
|
|---|
| 563 | return /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 564 | zIndex: props.zIndex
|
|---|
| 565 | }, /*#__PURE__*/React.createElement(SetPieTooltipEntrySettings, {
|
|---|
| 566 | dataKey: props.dataKey,
|
|---|
| 567 | nameKey: props.nameKey,
|
|---|
| 568 | sectors: sectors,
|
|---|
| 569 | stroke: props.stroke,
|
|---|
| 570 | strokeWidth: props.strokeWidth,
|
|---|
| 571 | fill: props.fill,
|
|---|
| 572 | name: props.name,
|
|---|
| 573 | hide: props.hide,
|
|---|
| 574 | tooltipType: props.tooltipType,
|
|---|
| 575 | id: id
|
|---|
| 576 | }), /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 577 | tabIndex: rootTabIndex,
|
|---|
| 578 | className: layerClass
|
|---|
| 579 | }, /*#__PURE__*/React.createElement(SectorsWithAnimation, {
|
|---|
| 580 | props: _objectSpread(_objectSpread({}, propsWithoutId), {}, {
|
|---|
| 581 | sectors
|
|---|
| 582 | }),
|
|---|
| 583 | previousSectorsRef: previousSectorsRef,
|
|---|
| 584 | id: id
|
|---|
| 585 | })));
|
|---|
| 586 | }
|
|---|
| 587 | /**
|
|---|
| 588 | * @consumes PolarChartContext
|
|---|
| 589 | * @provides LabelListContext
|
|---|
| 590 | * @provides CellReader
|
|---|
| 591 | */
|
|---|
| 592 | export function Pie(outsideProps) {
|
|---|
| 593 | var props = resolveDefaultProps(outsideProps, defaultPieProps);
|
|---|
| 594 | var {
|
|---|
| 595 | id: externalId
|
|---|
| 596 | } = props,
|
|---|
| 597 | propsWithoutId = _objectWithoutProperties(props, _excluded4);
|
|---|
| 598 | var presentationProps = svgPropertiesNoEvents(propsWithoutId);
|
|---|
| 599 | return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
|
|---|
| 600 | id: externalId,
|
|---|
| 601 | type: "pie"
|
|---|
| 602 | }, id => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetPolarGraphicalItem, {
|
|---|
| 603 | type: "pie",
|
|---|
| 604 | id: id,
|
|---|
| 605 | data: propsWithoutId.data,
|
|---|
| 606 | dataKey: propsWithoutId.dataKey,
|
|---|
| 607 | hide: propsWithoutId.hide,
|
|---|
| 608 | angleAxisId: 0,
|
|---|
| 609 | radiusAxisId: 0,
|
|---|
| 610 | name: propsWithoutId.name,
|
|---|
| 611 | nameKey: propsWithoutId.nameKey,
|
|---|
| 612 | tooltipType: propsWithoutId.tooltipType,
|
|---|
| 613 | legendType: propsWithoutId.legendType,
|
|---|
| 614 | fill: propsWithoutId.fill,
|
|---|
| 615 | cx: propsWithoutId.cx,
|
|---|
| 616 | cy: propsWithoutId.cy,
|
|---|
| 617 | startAngle: propsWithoutId.startAngle,
|
|---|
| 618 | endAngle: propsWithoutId.endAngle,
|
|---|
| 619 | paddingAngle: propsWithoutId.paddingAngle,
|
|---|
| 620 | minAngle: propsWithoutId.minAngle,
|
|---|
| 621 | innerRadius: propsWithoutId.innerRadius,
|
|---|
| 622 | outerRadius: propsWithoutId.outerRadius,
|
|---|
| 623 | cornerRadius: propsWithoutId.cornerRadius,
|
|---|
| 624 | presentationProps: presentationProps,
|
|---|
| 625 | maxRadius: props.maxRadius
|
|---|
| 626 | }), /*#__PURE__*/React.createElement(SetPiePayloadLegend, _extends({}, propsWithoutId, {
|
|---|
| 627 | id: id
|
|---|
| 628 | })), /*#__PURE__*/React.createElement(PieImpl, _extends({}, propsWithoutId, {
|
|---|
| 629 | id: id
|
|---|
| 630 | }))));
|
|---|
| 631 | }
|
|---|
| 632 | Pie.displayName = 'Pie'; |
|---|