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