| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.payloadSearcher = exports.defaultSunburstChartProps = exports.SunburstChart = void 0;
|
|---|
| 7 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 8 | var React = _react;
|
|---|
| 9 | var _d3Scale = require("victory-vendor/d3-scale");
|
|---|
| 10 | var _clsx = require("clsx");
|
|---|
| 11 | var _get = _interopRequireDefault(require("es-toolkit/compat/get"));
|
|---|
| 12 | var _Surface = require("../container/Surface");
|
|---|
| 13 | var _Layer = require("../container/Layer");
|
|---|
| 14 | var _Sector = require("../shape/Sector");
|
|---|
| 15 | var _Text = require("../component/Text");
|
|---|
| 16 | var _PolarUtils = require("../util/PolarUtils");
|
|---|
| 17 | var _chartLayoutContext = require("../context/chartLayoutContext");
|
|---|
| 18 | var _tooltipPortalContext = require("../context/tooltipPortalContext");
|
|---|
| 19 | var _RechartsWrapper = require("./RechartsWrapper");
|
|---|
| 20 | var _tooltipSlice = require("../state/tooltipSlice");
|
|---|
| 21 | var _SetTooltipEntrySettings = require("../state/SetTooltipEntrySettings");
|
|---|
| 22 | var _RechartsStoreProvider = require("../state/RechartsStoreProvider");
|
|---|
| 23 | var _hooks = require("../state/hooks");
|
|---|
| 24 | var _RegisterGraphicalItemId = require("../context/RegisterGraphicalItemId");
|
|---|
| 25 | var _resolveDefaultProps = require("../util/resolveDefaultProps");
|
|---|
| 26 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 27 | 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); }
|
|---|
| 28 | 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); }
|
|---|
| 29 | 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; }
|
|---|
| 30 | 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; }
|
|---|
| 31 | 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; }
|
|---|
| 32 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 33 | 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); }
|
|---|
| 34 | /**
|
|---|
| 35 | * We require tooltipIndex on each node internally to track which node is active in the tooltip.
|
|---|
| 36 | * This is not required from the outside user - we can calculate it as we traverse the tree.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | var defaultTextProps = {
|
|---|
| 40 | fontWeight: 'bold',
|
|---|
| 41 | paintOrder: 'stroke fill',
|
|---|
| 42 | fontSize: '.75rem',
|
|---|
| 43 | stroke: '#FFF',
|
|---|
| 44 | fill: 'black',
|
|---|
| 45 | pointerEvents: 'none'
|
|---|
| 46 | };
|
|---|
| 47 | function getMaxDepthOf(node) {
|
|---|
| 48 | if (!node.children || node.children.length === 0) return 1;
|
|---|
| 49 |
|
|---|
| 50 | // Calculate depth for each child and find the maximum
|
|---|
| 51 | var childDepths = node.children.map(d => getMaxDepthOf(d));
|
|---|
| 52 | return 1 + Math.max(...childDepths);
|
|---|
| 53 | }
|
|---|
| 54 | var SetSunburstTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
|
|---|
| 55 | var {
|
|---|
| 56 | dataKey,
|
|---|
| 57 | nameKey,
|
|---|
| 58 | data,
|
|---|
| 59 | stroke,
|
|---|
| 60 | fill,
|
|---|
| 61 | positions,
|
|---|
| 62 | id
|
|---|
| 63 | } = _ref;
|
|---|
| 64 | var tooltipEntrySettings = {
|
|---|
| 65 | dataDefinedOnItem: data.children,
|
|---|
| 66 | getPosition: index => positions.get(index),
|
|---|
| 67 | // Sunburst does not support many of the properties as other charts do so there's plenty of defaults here
|
|---|
| 68 | settings: {
|
|---|
| 69 | stroke,
|
|---|
| 70 | strokeWidth: undefined,
|
|---|
| 71 | fill,
|
|---|
| 72 | nameKey,
|
|---|
| 73 | dataKey,
|
|---|
| 74 | // if there is a nameKey use it, otherwise make the name of the tooltip the dataKey itself
|
|---|
| 75 | name: nameKey ? undefined : dataKey,
|
|---|
| 76 | hide: false,
|
|---|
| 77 | type: undefined,
|
|---|
| 78 | color: fill,
|
|---|
| 79 | unit: '',
|
|---|
| 80 | graphicalItemId: id
|
|---|
| 81 | }
|
|---|
| 82 | };
|
|---|
| 83 | return /*#__PURE__*/React.createElement(_SetTooltipEntrySettings.SetTooltipEntrySettings, {
|
|---|
| 84 | tooltipEntrySettings: tooltipEntrySettings
|
|---|
| 85 | });
|
|---|
| 86 | });
|
|---|
| 87 |
|
|---|
| 88 | // Why is margin not a sunburst prop? No clue. Probably it should be
|
|---|
| 89 | var defaultSunburstMargin = {
|
|---|
| 90 | top: 0,
|
|---|
| 91 | right: 0,
|
|---|
| 92 | bottom: 0,
|
|---|
| 93 | left: 0
|
|---|
| 94 | };
|
|---|
| 95 | var payloadSearcher = (data, activeIndex) => {
|
|---|
| 96 | if (activeIndex == null) {
|
|---|
| 97 | return undefined;
|
|---|
| 98 | }
|
|---|
| 99 | return (0, _get.default)(data, activeIndex);
|
|---|
| 100 | };
|
|---|
| 101 | exports.payloadSearcher = payloadSearcher;
|
|---|
| 102 | var addToSunburstNodeIndex = function addToSunburstNodeIndex(indexInChildrenArr) {
|
|---|
| 103 | var activeTooltipIndexSoFar = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|---|
| 104 | return "".concat(activeTooltipIndexSoFar, "children[").concat(indexInChildrenArr, "]");
|
|---|
| 105 | };
|
|---|
| 106 | var preloadedState = {
|
|---|
| 107 | options: {
|
|---|
| 108 | validateTooltipEventTypes: ['item'],
|
|---|
| 109 | defaultTooltipEventType: 'item',
|
|---|
| 110 | chartName: 'Sunburst',
|
|---|
| 111 | tooltipPayloadSearcher: payloadSearcher,
|
|---|
| 112 | eventEmitter: undefined
|
|---|
| 113 | }
|
|---|
| 114 | };
|
|---|
| 115 | var defaultSunburstChartProps = exports.defaultSunburstChartProps = {
|
|---|
| 116 | padding: 2,
|
|---|
| 117 | dataKey: 'value',
|
|---|
| 118 | nameKey: 'name',
|
|---|
| 119 | ringPadding: 2,
|
|---|
| 120 | innerRadius: 50,
|
|---|
| 121 | fill: '#333',
|
|---|
| 122 | stroke: '#FFF',
|
|---|
| 123 | textOptions: defaultTextProps,
|
|---|
| 124 | startAngle: 0,
|
|---|
| 125 | endAngle: 360,
|
|---|
| 126 | responsive: false
|
|---|
| 127 | };
|
|---|
| 128 | var SunburstChartImpl = _ref2 => {
|
|---|
| 129 | var {
|
|---|
| 130 | className,
|
|---|
| 131 | data,
|
|---|
| 132 | children,
|
|---|
| 133 | padding,
|
|---|
| 134 | dataKey,
|
|---|
| 135 | nameKey,
|
|---|
| 136 | ringPadding,
|
|---|
| 137 | innerRadius,
|
|---|
| 138 | fill,
|
|---|
| 139 | stroke,
|
|---|
| 140 | textOptions,
|
|---|
| 141 | outerRadius: outerRadiusFromProps,
|
|---|
| 142 | cx: cxFromProps,
|
|---|
| 143 | cy: cyFromProps,
|
|---|
| 144 | startAngle,
|
|---|
| 145 | endAngle,
|
|---|
| 146 | onClick,
|
|---|
| 147 | onMouseEnter,
|
|---|
| 148 | onMouseLeave,
|
|---|
| 149 | id
|
|---|
| 150 | } = _ref2;
|
|---|
| 151 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 152 | var width = (0, _chartLayoutContext.useChartWidth)();
|
|---|
| 153 | var height = (0, _chartLayoutContext.useChartHeight)();
|
|---|
| 154 | if (width == null || height == null) {
|
|---|
| 155 | return null;
|
|---|
| 156 | }
|
|---|
| 157 | var outerRadius = outerRadiusFromProps !== null && outerRadiusFromProps !== void 0 ? outerRadiusFromProps : Math.min(width, height) / 2;
|
|---|
| 158 | var cx = cxFromProps !== null && cxFromProps !== void 0 ? cxFromProps : width / 2;
|
|---|
| 159 | var cy = cyFromProps !== null && cyFromProps !== void 0 ? cyFromProps : height / 2;
|
|---|
| 160 | var rScale = (0, _d3Scale.scaleLinear)([0, data[dataKey]], [0, endAngle]);
|
|---|
| 161 | var treeDepth = getMaxDepthOf(data);
|
|---|
| 162 | var thickness = (outerRadius - innerRadius) / treeDepth;
|
|---|
| 163 | var sectors = [];
|
|---|
| 164 | var positions = new Map([]);
|
|---|
| 165 |
|
|---|
| 166 | // event handlers
|
|---|
| 167 | function handleMouseEnter(node, e) {
|
|---|
| 168 | if (onMouseEnter) onMouseEnter(node, e);
|
|---|
| 169 | dispatch((0, _tooltipSlice.setActiveMouseOverItemIndex)({
|
|---|
| 170 | activeIndex: node.tooltipIndex,
|
|---|
| 171 | activeDataKey: dataKey,
|
|---|
| 172 | activeCoordinate: positions.get(node.name),
|
|---|
| 173 | activeGraphicalItemId: id
|
|---|
| 174 | }));
|
|---|
| 175 | }
|
|---|
| 176 | function handleMouseLeave(node, e) {
|
|---|
| 177 | if (onMouseLeave) onMouseLeave(node, e);
|
|---|
| 178 | dispatch((0, _tooltipSlice.mouseLeaveItem)());
|
|---|
| 179 | }
|
|---|
| 180 | function handleClick(node) {
|
|---|
| 181 | if (onClick) onClick(node);
|
|---|
| 182 | dispatch((0, _tooltipSlice.setActiveClickItemIndex)({
|
|---|
| 183 | activeIndex: node.tooltipIndex,
|
|---|
| 184 | activeDataKey: dataKey,
|
|---|
| 185 | activeCoordinate: positions.get(node.name),
|
|---|
| 186 | activeGraphicalItemId: id
|
|---|
| 187 | }));
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | // recursively add nodes for each data point and its children
|
|---|
| 191 | function drawArcs(childNodes, options) {
|
|---|
| 192 | var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
|---|
| 193 | var {
|
|---|
| 194 | radius,
|
|---|
| 195 | innerR,
|
|---|
| 196 | initialAngle,
|
|---|
| 197 | childColor,
|
|---|
| 198 | nestedActiveTooltipIndex
|
|---|
| 199 | } = options;
|
|---|
| 200 | var currentAngle = initialAngle;
|
|---|
| 201 | if (!childNodes) return; // base case: no children of this node
|
|---|
| 202 |
|
|---|
| 203 | childNodes.forEach((d, i) => {
|
|---|
| 204 | var _ref3, _d$fill;
|
|---|
| 205 | var currentTooltipIndex = depth === 1 ? "[".concat(i, "]") : addToSunburstNodeIndex(i, nestedActiveTooltipIndex);
|
|---|
| 206 | var nodeWithIndex = _objectSpread(_objectSpread({}, d), {}, {
|
|---|
| 207 | tooltipIndex: currentTooltipIndex
|
|---|
| 208 | });
|
|---|
| 209 | var arcLength = rScale(d[dataKey]);
|
|---|
| 210 | var start = currentAngle;
|
|---|
| 211 | // color priority - if there's a color on the individual point use that, otherwise use parent color or default
|
|---|
| 212 | var fillColor = (_ref3 = (_d$fill = d === null || d === void 0 ? void 0 : d.fill) !== null && _d$fill !== void 0 ? _d$fill : childColor) !== null && _ref3 !== void 0 ? _ref3 : fill;
|
|---|
| 213 | var {
|
|---|
| 214 | x: textX,
|
|---|
| 215 | y: textY
|
|---|
| 216 | } = (0, _PolarUtils.polarToCartesian)(0, 0, innerR + radius / 2, -(start + arcLength - arcLength / 2));
|
|---|
| 217 | currentAngle += arcLength;
|
|---|
| 218 | sectors.push(/*#__PURE__*/React.createElement("g", {
|
|---|
| 219 | key: "sunburst-sector-".concat(d.name, "-").concat(i)
|
|---|
| 220 | }, /*#__PURE__*/React.createElement(_Sector.Sector, {
|
|---|
| 221 | onClick: () => handleClick(nodeWithIndex),
|
|---|
| 222 | onMouseEnter: e => handleMouseEnter(nodeWithIndex, e),
|
|---|
| 223 | onMouseLeave: e => handleMouseLeave(nodeWithIndex, e),
|
|---|
| 224 | fill: fillColor,
|
|---|
| 225 | stroke: stroke,
|
|---|
| 226 | strokeWidth: padding,
|
|---|
| 227 | startAngle: start,
|
|---|
| 228 | endAngle: start + arcLength,
|
|---|
| 229 | innerRadius: innerR,
|
|---|
| 230 | outerRadius: innerR + radius,
|
|---|
| 231 | cx: cx,
|
|---|
| 232 | cy: cy
|
|---|
| 233 | }), /*#__PURE__*/React.createElement(_Text.Text, _extends({}, textOptions, {
|
|---|
| 234 | alignmentBaseline: "middle",
|
|---|
| 235 | textAnchor: "middle",
|
|---|
| 236 | x: textX + cx,
|
|---|
| 237 | y: cy - textY
|
|---|
| 238 | }), d[dataKey])));
|
|---|
| 239 | var {
|
|---|
| 240 | x: tooltipX,
|
|---|
| 241 | y: tooltipY
|
|---|
| 242 | } = (0, _PolarUtils.polarToCartesian)(cx, cy, innerR + radius / 2, start);
|
|---|
| 243 | positions.set(d.name, {
|
|---|
| 244 | x: tooltipX,
|
|---|
| 245 | y: tooltipY
|
|---|
| 246 | });
|
|---|
| 247 | return drawArcs(d.children, {
|
|---|
| 248 | radius,
|
|---|
| 249 | innerR: innerR + radius + ringPadding,
|
|---|
| 250 | initialAngle: start,
|
|---|
| 251 | childColor: fillColor,
|
|---|
| 252 | nestedActiveTooltipIndex: currentTooltipIndex
|
|---|
| 253 | }, depth + 1);
|
|---|
| 254 | });
|
|---|
| 255 | }
|
|---|
| 256 | drawArcs(data.children, {
|
|---|
| 257 | radius: thickness,
|
|---|
| 258 | innerR: innerRadius,
|
|---|
| 259 | initialAngle: startAngle
|
|---|
| 260 | });
|
|---|
| 261 | var layerClass = (0, _clsx.clsx)('recharts-sunburst', className);
|
|---|
| 262 | return /*#__PURE__*/React.createElement(_Surface.Surface, {
|
|---|
| 263 | width: width,
|
|---|
| 264 | height: height
|
|---|
| 265 | }, /*#__PURE__*/React.createElement(_Layer.Layer, {
|
|---|
| 266 | className: layerClass
|
|---|
| 267 | }, sectors), /*#__PURE__*/React.createElement(SetSunburstTooltipEntrySettings, {
|
|---|
| 268 | dataKey: dataKey,
|
|---|
| 269 | nameKey: nameKey,
|
|---|
| 270 | data: data,
|
|---|
| 271 | stroke: stroke,
|
|---|
| 272 | fill: fill,
|
|---|
| 273 | positions: positions,
|
|---|
| 274 | id: id
|
|---|
| 275 | }), children);
|
|---|
| 276 | };
|
|---|
| 277 |
|
|---|
| 278 | /**
|
|---|
| 279 | * The sunburst is a hierarchical chart, similar to a {@link Treemap}, plotted in polar coordinates.
|
|---|
| 280 | * Sunburst charts effectively convey the hierarchical relationships and proportions within each level.
|
|---|
| 281 | * It is easy to see all the middle layers in the hierarchy, which might get lost in other visualizations.
|
|---|
| 282 | * For some datasets, the radial layout may be more visually appealing and intuitive than a traditional {@link Treemap}.
|
|---|
| 283 | *
|
|---|
| 284 | * @consumes ResponsiveContainerContext
|
|---|
| 285 | * @provides TooltipEntrySettings
|
|---|
| 286 | */
|
|---|
| 287 | var SunburstChart = outsideProps => {
|
|---|
| 288 | var props = (0, _resolveDefaultProps.resolveDefaultProps)(outsideProps, defaultSunburstChartProps);
|
|---|
| 289 | var {
|
|---|
| 290 | className,
|
|---|
| 291 | width,
|
|---|
| 292 | height,
|
|---|
| 293 | responsive,
|
|---|
| 294 | style,
|
|---|
| 295 | id: externalId
|
|---|
| 296 | } = props;
|
|---|
| 297 | var [tooltipPortal, setTooltipPortal] = (0, _react.useState)(null);
|
|---|
| 298 | return /*#__PURE__*/React.createElement(_RechartsStoreProvider.RechartsStoreProvider, {
|
|---|
| 299 | preloadedState: preloadedState,
|
|---|
| 300 | reduxStoreName: className !== null && className !== void 0 ? className : 'SunburstChart'
|
|---|
| 301 | }, /*#__PURE__*/React.createElement(_chartLayoutContext.ReportChartSize, {
|
|---|
| 302 | width: width,
|
|---|
| 303 | height: height
|
|---|
| 304 | }), /*#__PURE__*/React.createElement(_chartLayoutContext.ReportChartMargin, {
|
|---|
| 305 | margin: defaultSunburstMargin
|
|---|
| 306 | }), /*#__PURE__*/React.createElement(_tooltipPortalContext.TooltipPortalContext.Provider, {
|
|---|
| 307 | value: tooltipPortal
|
|---|
| 308 | }, /*#__PURE__*/React.createElement(_RechartsWrapper.RechartsWrapper, {
|
|---|
| 309 | className: className,
|
|---|
| 310 | width: width,
|
|---|
| 311 | height: height,
|
|---|
| 312 | responsive: responsive,
|
|---|
| 313 | style: style,
|
|---|
| 314 | ref: node => {
|
|---|
| 315 | if (tooltipPortal == null && node != null) {
|
|---|
| 316 | setTooltipPortal(node);
|
|---|
| 317 | }
|
|---|
| 318 | },
|
|---|
| 319 | onMouseEnter: undefined,
|
|---|
| 320 | onMouseLeave: undefined,
|
|---|
| 321 | onClick: undefined,
|
|---|
| 322 | onMouseMove: undefined,
|
|---|
| 323 | onMouseDown: undefined,
|
|---|
| 324 | onMouseUp: undefined,
|
|---|
| 325 | onContextMenu: undefined,
|
|---|
| 326 | onDoubleClick: undefined,
|
|---|
| 327 | onTouchStart: undefined,
|
|---|
| 328 | onTouchMove: undefined,
|
|---|
| 329 | onTouchEnd: undefined
|
|---|
| 330 | }, /*#__PURE__*/React.createElement(_RegisterGraphicalItemId.RegisterGraphicalItemId, {
|
|---|
| 331 | id: externalId,
|
|---|
| 332 | type: "sunburst"
|
|---|
| 333 | }, id => /*#__PURE__*/React.createElement(SunburstChartImpl, _extends({}, props, {
|
|---|
| 334 | id: id
|
|---|
| 335 | }))))));
|
|---|
| 336 | };
|
|---|
| 337 | exports.SunburstChart = SunburstChart; |
|---|