recharts
Version:
React charts
623 lines (607 loc) • 28.6 kB
JavaScript
var _excluded = ["textOptions", "value", "fill", "style", "background", "halo", "x", "y"],
_excluded2 = ["fill"],
_excluded3 = ["ref", "children", "offset"],
_excluded4 = ["id"];
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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); }
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; }
import * as React from 'react';
import { useCallback, useMemo, useState } from 'react';
import { scaleLinear } from 'victory-vendor/d3-scale';
import get from 'es-toolkit/compat/get';
import { clsx } from '../util/clsx';
import { Surface } from '../container/Surface';
import { Layer } from '../container/Layer';
import { Sector } from '../shape/Sector';
import { Text } from '../component/Text';
import { LabelList, PolarLabelListContextProvider } from '../component/LabelList';
import { polarToCartesian } from '../util/PolarUtils';
import { ReportChartMargin, ReportChartSize, useChartHeight, useChartWidth } from '../context/chartLayoutContext';
import { TooltipPortalContext } from '../context/tooltipPortalContext';
import { RechartsWrapper } from './RechartsWrapper';
import { mouseLeaveItem, setActiveClickItemIndex, setActiveMouseOverItemIndex } from '../state/tooltipSlice';
import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
import { RechartsStoreProvider } from '../state/RechartsStoreProvider';
import { ReportEventSettings } from '../state/ReportEventSettings';
import { useAppDispatch } from '../state/hooks';
import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
import { resolveDefaultProps } from '../util/resolveDefaultProps';
import { initialEventSettingsState } from '../state/eventSettingsSlice';
import { withFillColor } from '../theme/themeFill';
import { useRechartsTheme } from '../theme/RechartsThemeContext';
import { getOwnStyles, hasOwnColors, withoutColors } from '../theme/dataEntryStyles';
import { getEffectivePageBackground } from '../theme/pageBackground';
import { svgPropertiesNoEventsFromUnknown } from '../util/svgPropertiesNoEvents';
import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents';
import { readEntryFill, splitFill } from '../fill/fillDefinitions';
import { AllZIndexPortals } from '../zIndex/ZIndexPortal';
import { DataLabelColorsContextProvider, DataLabelThemeContextProvider, filledShapeLabelColors } from '../theme/labelTheme';
/**
* @public
*/
/**
* We require tooltipIndex on each node internally to track which node is active in the tooltip.
* This is not required from the outside user - we can calculate it as we traverse the tree.
*/
/**
* @public
*/
var defaultTextProps = {
fontWeight: 'bold',
paintOrder: 'stroke fill',
fontSize: '.75rem',
stroke: '#FFF',
fill: 'black',
pointerEvents: 'none'
};
function getMaxDepthOf(node) {
if (!node.children || node.children.length === 0) return 1;
// Calculate depth for each child and find the maximum
var childDepths = node.children.map(d => getMaxDepthOf(d));
return 1 + Math.max(...childDepths);
}
var SetSunburstTooltipEntrySettings = /*#__PURE__*/React.memo(_ref2 => {
var dataKey = _ref2.dataKey,
nameKey = _ref2.nameKey,
data = _ref2.data,
stroke = _ref2.stroke,
fill = _ref2.fill,
positions = _ref2.positions,
id = _ref2.id;
var tooltipEntrySettings = {
dataDefinedOnItem: data.children,
getPosition: index => positions.get(index),
// Sunburst does not support many of the properties as other charts do so there's plenty of defaults here
settings: {
stroke,
strokeWidth: undefined,
fill,
nameKey,
dataKey,
// if there is a nameKey use it, otherwise make the name of the tooltip the dataKey itself
name: nameKey ? undefined : dataKey,
hide: false,
type: undefined,
color: fill,
unit: '',
graphicalItemId: id
}
};
return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
tooltipEntrySettings: tooltipEntrySettings
});
});
// Why is margin not a sunburst prop? No clue. Probably it should be
var defaultSunburstMargin = {
top: 0,
right: 0,
bottom: 0,
left: 0
};
export var payloadSearcher = (data, activeIndex) => {
if (activeIndex == null) {
return undefined;
}
return get(data, activeIndex);
};
var addToSunburstNodeIndex = function addToSunburstNodeIndex(indexInChildrenArr) {
var activeTooltipIndexSoFar = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return "".concat(activeTooltipIndexSoFar, "children[").concat(indexInChildrenArr, "]");
};
var preloadedState = {
options: {
validateTooltipEventTypes: ['item'],
defaultTooltipEventType: 'item',
chartName: 'Sunburst',
tooltipPayloadSearcher: payloadSearcher,
eventEmitter: undefined
}
};
/**
* Sector coordinates keyed by their tooltip index (`[0]`, `[0]children[1]`, ...) which is the identity
* that the tooltip store and the keyboard navigation use when asking for a position.
*/
/**
* Styles that the chart takes from the theme.
* Undefined if there is no theme, in which case the chart renders with its legacy styles.
*/
/**
* Keep the default labels' Text-only options and baseline while LabelList supplies
* the sector coordinates, branch label theme, and background layering.
*/
function SunburstLabelContent(_ref3) {
var textOptions = _ref3.textOptions,
value = _ref3.value,
fill = _ref3.fill,
style = _ref3.style,
background = _ref3.background,
halo = _ref3.halo,
x = _ref3.x,
y = _ref3.y,
rest = _objectWithoutProperties(_ref3, _excluded);
var theme = useRechartsTheme();
return /*#__PURE__*/React.createElement(Text, _extends({}, textOptions, svgPropertiesAndEvents(rest), {
x: x,
y: y,
fill: theme == null ? textOptions === null || textOptions === void 0 ? void 0 : textOptions.fill : fill,
style: style,
alignmentBaseline: "middle",
textAnchor: "middle",
background: background,
halo: halo
}), value);
}
function isLabelListProps(label) {
return typeof label === 'object' && ! /*#__PURE__*/React.isValidElement(label);
}
function SunburstLabels(_ref4) {
var _ref6, _label$pointerEvents;
var label = _ref4.label,
textOptions = _ref4.textOptions;
var content = useCallback(labelProps => /*#__PURE__*/React.createElement(SunburstLabelContent, _extends({}, labelProps, {
textOptions: textOptions
})), [textOptions]);
var textStyle = textOptions === null || textOptions === void 0 ? void 0 : textOptions.style;
var explicitLabel = label != null && isLabelListProps(label) ? label : undefined;
var explicitStyle = explicitLabel === null || explicitLabel === void 0 ? void 0 : explicitLabel.style;
var explicitFill = explicitLabel === null || explicitLabel === void 0 ? void 0 : explicitLabel.fill;
var style = useMemo(() => {
if (textStyle == null) {
return explicitStyle;
}
if (explicitFill == null) {
return _objectSpread(_objectSpread({}, textStyle), explicitStyle);
}
// Shared CSS fill must not take precedence over an explicit label fill prop.
var _fill = textStyle.fill,
sharedStyle = _objectWithoutProperties(textStyle, _excluded2);
return _objectSpread(_objectSpread({}, sharedStyle), explicitStyle);
}, [textStyle, explicitStyle, explicitFill]);
if (label === false) {
return null;
}
var _ref5 = textOptions !== null && textOptions !== void 0 ? textOptions : {},
_ref = _ref5.ref,
_children = _ref5.children,
_offset = _ref5.offset,
textProps = _objectWithoutProperties(_ref5, _excluded3);
if (label == null || label === true) {
var _textProps$pointerEve;
return /*#__PURE__*/React.createElement(LabelList, _extends({}, textProps, {
pointerEvents: (_textProps$pointerEve = textProps.pointerEvents) !== null && _textProps$pointerEve !== void 0 ? _textProps$pointerEve : 'none',
content: content
}));
}
if (!isLabelListProps(label)) {
var _textProps$pointerEve2;
return /*#__PURE__*/React.createElement(LabelList, _extends({}, textProps, {
content: label,
pointerEvents: (_textProps$pointerEve2 = textProps.pointerEvents) !== null && _textProps$pointerEve2 !== void 0 ? _textProps$pointerEve2 : 'none'
}));
}
return /*#__PURE__*/React.createElement(LabelList, _extends({}, textProps, {
textBreakAll: textOptions === null || textOptions === void 0 ? void 0 : textOptions.breakAll
}, label, {
style: style,
pointerEvents: (_ref6 = (_label$pointerEvents = label.pointerEvents) !== null && _label$pointerEvents !== void 0 ? _label$pointerEvents : textProps.pointerEvents) !== null && _ref6 !== void 0 ? _ref6 : 'none'
}));
}
export var defaultSunburstChartProps = _objectSpread({
padding: 2,
dataKey: 'value',
nameKey: 'name',
ringPadding: 2,
innerRadius: 50,
fill: '#333',
stroke: '#FFF',
textOptions: defaultTextProps,
label: true,
startAngle: 0,
endAngle: 360,
responsive: false
}, initialEventSettingsState);
/**
* Default props when a theme is active.
* The legacy colors and label styles do not apply, the theme provides these instead.
*/
var _fill = defaultSunburstChartProps.fill,
_stroke = defaultSunburstChartProps.stroke,
_textOptions = defaultSunburstChartProps.textOptions,
themedSunburstChartProps = _objectWithoutProperties(defaultSunburstChartProps, ["fill", "stroke", "textOptions"]);
function readEntryStroke(entry) {
if (entry != null && typeof entry === 'object' && 'stroke' in entry && typeof entry.stroke === 'string') {
return entry.stroke;
}
return undefined;
}
function readEntryStrokeWidth(entry) {
if (entry != null && typeof entry === 'object' && 'strokeWidth' in entry) {
var strokeWidth = entry.strokeWidth;
if (typeof strokeWidth === 'number' || typeof strokeWidth === 'string') {
return strokeWidth;
}
}
return undefined;
}
/**
* SVG props from one data entry, without `id`.
* `id` is removed so a data id cannot land on the sector.
*/
function entrySvgProps(entry) {
var props = svgPropertiesNoEventsFromUnknown(entry);
if (props == null) {
return {};
}
var id = props.id,
withoutId = _objectWithoutProperties(props, _excluded4);
return withoutId;
}
/**
* Resolves the props of one sector.
*
* Every valid SVG prop on the data entry is applied, except `id`.
* Entry props merge over the chart props: a value the entry leaves out comes from the chart.
*
* A color is a `fill` or `stroke` on the entry itself or on `<SunburstChart>`.
* If either has one, the theme colors are not applied, but the other theme styles are. A fill inherited from the parent
* does not count, so nested sectors of the same color keep the theme separator.
* Without a theme, the chart's legacy defaults fill that role.
*/
function resolveSectorProps(_ref7) {
var _readEntryStrokeWidth, _ref9, _entryFill$color, _entryFill$fillObject3, _entryFill$fillObject4, _readEntryStroke;
var entry = _ref7.entry,
inheritedFill = _ref7.inheritedFill,
chartFill = _ref7.chartFill,
chartFillObject = _ref7.chartFillObject,
chartStroke = _ref7.chartStroke,
padding = _ref7.padding,
themeStyles = _ref7.themeStyles,
branchStyle = _ref7.branchStyle;
var strokeWidth = (_readEntryStrokeWidth = readEntryStrokeWidth(entry)) !== null && _readEntryStrokeWidth !== void 0 ? _readEntryStrokeWidth : padding;
var hasExplicitColors = chartFill != null || chartStroke != null || hasOwnColors(entry);
var props = {};
var entryFill = readEntryFill(entry);
if (themeStyles != null) {
/*
* Keep non-color theme fields such as fillOpacity and strokeDasharray.
* getOwnStyles leaves out `active`, which is not a sector prop.
*/
var branchColorStyle = withFillColor(branchStyle);
Object.assign(props, getOwnStyles(hasExplicitColors ? withoutColors(branchColorStyle) : branchColorStyle));
}
// Entry props win over the theme, then the resolved colors and separator width.
Object.assign(props, entrySvgProps(entry));
/*
* A fill object paints through its definition, in the color the sector has without it:
* the entry's own fill object first, then the chart's, then the theme branch's.
*/
if (themeStyles != null && !hasExplicitColors) {
var _ref8, _entryFill$fillObject, _entryFill$fillObject2, _branchFill$fillObjec, _themeStyles$pageBack;
var branchFill = splitFill(branchStyle === null || branchStyle === void 0 ? void 0 : branchStyle.fill);
var _color = inheritedFill !== null && inheritedFill !== void 0 ? inheritedFill : branchFill.color;
var _definition = (_ref8 = (_entryFill$fillObject = (_entryFill$fillObject2 = entryFill.fillObject) === null || _entryFill$fillObject2 === void 0 ? void 0 : _entryFill$fillObject2.definition) !== null && _entryFill$fillObject !== void 0 ? _entryFill$fillObject : chartFillObject === null || chartFillObject === void 0 ? void 0 : chartFillObject.definition) !== null && _ref8 !== void 0 ? _ref8 : (_branchFill$fillObjec = branchFill.fillObject) === null || _branchFill$fillObjec === void 0 ? void 0 : _branchFill$fillObjec.definition;
props.fill = _definition == null ? _color : {
color: _color,
definition: _definition
};
props.stroke = (_themeStyles$pageBack = themeStyles.pageBackground) !== null && _themeStyles$pageBack !== void 0 ? _themeStyles$pageBack : branchStyle === null || branchStyle === void 0 ? void 0 : branchStyle.stroke;
props.strokeWidth = strokeWidth;
return props;
}
var color = (_ref9 = (_entryFill$color = entryFill.color) !== null && _entryFill$color !== void 0 ? _entryFill$color : inheritedFill) !== null && _ref9 !== void 0 ? _ref9 : chartFill;
var definition = (_entryFill$fillObject3 = (_entryFill$fillObject4 = entryFill.fillObject) === null || _entryFill$fillObject4 === void 0 ? void 0 : _entryFill$fillObject4.definition) !== null && _entryFill$fillObject3 !== void 0 ? _entryFill$fillObject3 : entryFill.color == null ? chartFillObject === null || chartFillObject === void 0 ? void 0 : chartFillObject.definition : undefined;
props.fill = definition == null ? color : {
color,
definition
};
props.stroke = (_readEntryStroke = readEntryStroke(entry)) !== null && _readEntryStroke !== void 0 ? _readEntryStroke : chartStroke;
props.strokeWidth = strokeWidth;
return props;
}
var SunburstChartContent = _ref0 => {
var className = _ref0.className,
data = _ref0.data,
children = _ref0.children,
padding = _ref0.padding,
dataKey = _ref0.dataKey,
nameKey = _ref0.nameKey,
ringPadding = _ref0.ringPadding,
innerRadius = _ref0.innerRadius,
fill = _ref0.fill,
chartFillObject = _ref0.chartFillObject,
stroke = _ref0.stroke,
textOptions = _ref0.textOptions,
label = _ref0.label,
outerRadiusFromProps = _ref0.outerRadius,
cxFromProps = _ref0.cx,
cyFromProps = _ref0.cy,
startAngle = _ref0.startAngle,
endAngle = _ref0.endAngle,
onClick = _ref0.onClick,
onMouseEnter = _ref0.onMouseEnter,
onMouseLeave = _ref0.onMouseLeave,
id = _ref0.id,
themeStyles = _ref0.themeStyles,
width = _ref0.width,
height = _ref0.height;
var dispatch = useAppDispatch();
var layout = useMemo(() => {
var outerRadius = outerRadiusFromProps !== null && outerRadiusFromProps !== void 0 ? outerRadiusFromProps : Math.min(width, height) / 2;
var cx = cxFromProps !== null && cxFromProps !== void 0 ? cxFromProps : width / 2;
var cy = cyFromProps !== null && cyFromProps !== void 0 ? cyFromProps : height / 2;
var rScale = scaleLinear([0, data[dataKey]], [0, endAngle - startAngle]);
var treeDepth = getMaxDepthOf(data);
var thickness = (outerRadius - innerRadius) / treeDepth;
var sectors = [];
var labelListEntries = [];
var labelThemes = [];
var resolveLabelTheme = index => labelThemes[index];
var positions = new Map([]);
// event handlers
function handleMouseEnter(node, e) {
if (onMouseEnter) onMouseEnter(node, e);
dispatch(setActiveMouseOverItemIndex({
activeIndex: node.tooltipIndex,
activeDataKey: dataKey,
activeCoordinate: positions.get(node.tooltipIndex),
activeGraphicalItemId: id
}));
}
function handleMouseLeave(node, e) {
if (onMouseLeave) onMouseLeave(node, e);
dispatch(mouseLeaveItem());
}
function handleClick(node) {
if (onClick) onClick(node);
dispatch(setActiveClickItemIndex({
activeIndex: node.tooltipIndex,
activeDataKey: dataKey,
activeCoordinate: positions.get(node.tooltipIndex),
activeGraphicalItemId: id
}));
}
// recursively add nodes for each data point and its children
function drawArcs(childNodes, options) {
var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
var radius = options.radius,
innerR = options.innerR,
initialAngle = options.initialAngle,
childColor = options.childColor,
nestedActiveTooltipIndex = options.nestedActiveTooltipIndex;
var currentAngle = initialAngle;
if (!childNodes) return undefined; // base case: no children of this node
return childNodes.map((d, i) => {
var _options$branchStyle;
var currentTooltipIndex = depth === 1 ? "[".concat(i, "]") : addToSunburstNodeIndex(i, nestedActiveTooltipIndex);
var nodeWithIndex = _objectSpread(_objectSpread({}, d), {}, {
tooltipIndex: currentTooltipIndex
});
var arcLength = rScale(d[dataKey]);
var start = currentAngle;
var branchStyle = (_options$branchStyle = options.branchStyle) !== null && _options$branchStyle !== void 0 ? _options$branchStyle : themeStyles == null || themeStyles.graphicalItems.length === 0 ? undefined : themeStyles.graphicalItems[i % themeStyles.graphicalItems.length];
var sectorProps = resolveSectorProps({
entry: d,
inheritedFill: childColor,
chartFill: fill,
chartFillObject,
chartStroke: stroke,
padding,
themeStyles,
branchStyle
});
var fillColor = splitFill(sectorProps.fill).color;
currentAngle += arcLength;
sectors.push(/*#__PURE__*/React.createElement("g", {
key: "sunburst-sector-".concat(currentTooltipIndex)
}, /*#__PURE__*/React.createElement(Sector, _extends({
onClick: () => handleClick(nodeWithIndex),
onMouseEnter: e => handleMouseEnter(nodeWithIndex, e),
onMouseLeave: e => handleMouseLeave(nodeWithIndex, e)
}, sectorProps, {
startAngle: start,
endAngle: start + arcLength,
innerRadius: innerR,
outerRadius: innerR + radius,
cx: cx,
cy: cy
}))));
labelListEntries.push({
value: d[dataKey],
payload: d,
fill: fillColor,
viewBox: {
cx,
cy,
innerRadius: innerR,
outerRadius: innerR + radius,
startAngle: start,
endAngle: start + arcLength,
clockWise: false
}
});
labelThemes.push(branchStyle === null || branchStyle === void 0 ? void 0 : branchStyle.label);
var _polarToCartesian = polarToCartesian(cx, cy, innerR + radius / 2, start),
tooltipX = _polarToCartesian.x,
tooltipY = _polarToCartesian.y;
positions.set(currentTooltipIndex, {
x: tooltipX,
y: tooltipY
});
var renderedChildren = drawArcs(d.children, {
radius,
innerR: innerR + radius + ringPadding,
initialAngle: start,
childColor: fillColor,
branchStyle,
nestedActiveTooltipIndex: currentTooltipIndex
}, depth + 1);
/*
* Tooltip reads the color of the entry from the data node.
* Themed sectors take their color from the theme or from their parent, which the data node does not know,
* so the tooltip gets a copy of the node with the rendered color.
*/
return themeStyles == null ? d : _objectSpread(_objectSpread({}, d), {}, {
fill: fillColor,
children: renderedChildren
});
});
}
var tooltipDataChildren = drawArcs(data.children, {
radius: thickness,
innerR: innerRadius,
initialAngle: startAngle
});
return {
sectors,
positions,
tooltipDataChildren,
labelListEntries,
resolveLabelTheme
};
}, [outerRadiusFromProps, width, height, cxFromProps, cyFromProps, data, dataKey, startAngle, endAngle, innerRadius, onMouseEnter, dispatch, id, onMouseLeave, onClick, themeStyles, fill, chartFillObject, stroke, padding, ringPadding]);
var layerClass = clsx('recharts-sunburst', className);
return /*#__PURE__*/React.createElement(Surface, {
width: width,
height: height
}, /*#__PURE__*/React.createElement(PolarLabelListContextProvider, {
value: layout.labelListEntries
}, /*#__PURE__*/React.createElement(DataLabelThemeContextProvider, {
value: layout.resolveLabelTheme
}, /*#__PURE__*/React.createElement(DataLabelColorsContextProvider, {
value: filledShapeLabelColors
}, /*#__PURE__*/React.createElement(Layer, {
className: layerClass
}, /*#__PURE__*/React.createElement(AllZIndexPortals, {
isPanorama: false
}, layout.sectors, /*#__PURE__*/React.createElement(SunburstLabels, {
label: label,
textOptions: textOptions
}))), children))), /*#__PURE__*/React.createElement(SetSunburstTooltipEntrySettings, {
dataKey: dataKey,
nameKey: nameKey,
data: themeStyles == null ? data : _objectSpread(_objectSpread({}, data), {}, {
children: layout.tooltipDataChildren
}),
stroke: stroke,
fill: fill,
positions: layout.positions,
id: id
}));
};
var SunburstChartImpl = props => {
var width = useChartWidth();
var height = useChartHeight();
if (width == null || height == null) {
return null;
}
return /*#__PURE__*/React.createElement(SunburstChartContent, _extends({}, props, {
width: width,
height: height
}));
};
/**
* The sunburst is a hierarchical chart, similar to a {@link Treemap}, plotted in polar coordinates.
* Sunburst charts effectively convey the hierarchical relationships and proportions within each level.
* It is easy to see all the middle layers in the hierarchy, which might get lost in other visualizations.
* For some datasets, the radial layout may be more visually appealing and intuitive than a traditional {@link Treemap}.
*
* @consumes ResponsiveContainerContext
* @provides TooltipEntrySettings
* @public
*/
export var SunburstChart = outsideProps => {
var theme = useRechartsTheme();
var props = theme == null ? resolveDefaultProps(outsideProps, defaultSunburstChartProps) : resolveDefaultProps(outsideProps, themedSunburstChartProps);
var themeStyles = useMemo(() => theme == null ? undefined : {
graphicalItems: theme.graphicalItems,
pageBackground: getEffectivePageBackground(theme)
}, [theme]);
var className = props.className,
width = props.width,
height = props.height,
responsive = props.responsive,
style = props.style,
externalId = props.id,
throttleDelay = props.throttleDelay,
throttledEvents = props.throttledEvents;
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
tooltipPortal = _useState2[0],
setTooltipPortal = _useState2[1];
return /*#__PURE__*/React.createElement(RechartsStoreProvider, {
preloadedState: preloadedState,
reduxStoreName: className !== null && className !== void 0 ? className : 'SunburstChart'
}, /*#__PURE__*/React.createElement(ReportChartSize, {
width: width,
height: height
}), /*#__PURE__*/React.createElement(ReportChartMargin, {
margin: defaultSunburstMargin
}), /*#__PURE__*/React.createElement(ReportEventSettings, {
throttleDelay: throttleDelay,
throttledEvents: throttledEvents
}), /*#__PURE__*/React.createElement(TooltipPortalContext.Provider, {
value: tooltipPortal
}, /*#__PURE__*/React.createElement(RechartsWrapper, {
className: className,
width: width,
height: height,
responsive: responsive,
style: style,
ref: node => {
if (tooltipPortal == null && node != null) {
setTooltipPortal(node);
}
},
onMouseEnter: undefined,
onMouseLeave: undefined,
onClick: undefined,
onMouseMove: undefined,
onMouseDown: undefined,
onMouseUp: undefined,
onContextMenu: undefined,
onDoubleClick: undefined,
onTouchStart: undefined,
onTouchMove: undefined,
onTouchEnd: undefined
}, /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
id: externalId,
type: "sunburst"
}, id => /*#__PURE__*/React.createElement(SunburstChartImpl, _extends({}, props, {
// the sectors paint the fill object, everything else uses its color
fill: splitFill(props.fill).color,
chartFillObject: splitFill(props.fill).fillObject,
id: id,
themeStyles: themeStyles
}))))));
};