UNPKG

recharts

Version:
460 lines (449 loc) • 18.6 kB
var _excluded = ["textAnchor", "startOffset"], _excluded2 = ["labelRef", "background", "halo"], _excluded3 = ["content"]; 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 { cloneElement, createContext, createElement, isValidElement, useContext, useMemo } from 'react'; import { clsx } from '../util/clsx'; import { isValidTextAnchor, resolveBackgroundPadding, Text } from './Text'; import { isNullish, isNumber, isNumOrStr, mathSign } from '../util/DataUtils'; import { polarToCartesian } from '../util/PolarUtils'; import { useViewBox } from '../context/chartLayoutContext'; import { useAppSelector } from '../state/hooks'; import { selectPolarViewBox } from '../state/selectors/polarAxisSelectors'; import { resolveDefaultProps } from '../util/resolveDefaultProps'; import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents'; import { ZIndexLayer } from '../zIndex/ZIndexLayer'; import { DefaultZIndexes } from '../zIndex/DefaultZIndexes'; import { getLabelBackgroundZIndex, LabelZIndexProvider, useLabelZIndex } from '../zIndex/labelBackgroundZIndex'; import { getCartesianPosition } from '../cartesian/getCartesianPosition'; import { cartesianViewBoxToTrapezoid } from '../cartesian/cartesianViewBoxToTrapezoid'; import { applyLabelTheme, useGlobalLabelTheme } from '../theme/labelTheme'; /** * @inline * @public */ /** * @inline * @public */ /** * @inline */ /** * @public */ /** * @public */ var CartesianLabelContext = /*#__PURE__*/createContext(null); export var CartesianLabelContextProvider = _ref => { var x = _ref.x, y = _ref.y, upperWidth = _ref.upperWidth, lowerWidth = _ref.lowerWidth, width = _ref.width, height = _ref.height, children = _ref.children; var viewBox = useMemo(() => ({ x, y, upperWidth, lowerWidth, width, height }), [x, y, upperWidth, lowerWidth, width, height]); return /*#__PURE__*/React.createElement(CartesianLabelContext.Provider, { value: viewBox }, children); }; var useCartesianLabelContext = () => { var labelChildContext = useContext(CartesianLabelContext); var chartContext = useViewBox(); return labelChildContext || (chartContext ? cartesianViewBoxToTrapezoid(chartContext) : undefined); }; var PolarLabelContext = /*#__PURE__*/createContext(null); export var PolarLabelContextProvider = _ref2 => { var cx = _ref2.cx, cy = _ref2.cy, innerRadius = _ref2.innerRadius, outerRadius = _ref2.outerRadius, startAngle = _ref2.startAngle, endAngle = _ref2.endAngle, clockWise = _ref2.clockWise, children = _ref2.children; var viewBox = useMemo(() => ({ cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise }), [cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise]); return /*#__PURE__*/React.createElement(PolarLabelContext.Provider, { value: viewBox }, children); }; export var usePolarLabelContext = () => { var labelChildContext = useContext(PolarLabelContext); var chartContext = useAppSelector(selectPolarViewBox); return labelChildContext || chartContext; }; var getLabel = props => { var value = props.value, formatter = props.formatter; var label = isNullish(props.children) ? value : props.children; if (typeof formatter === 'function') { return formatter(label); } return label; }; export var isLabelContentAFunction = content => { return content != null && typeof content === 'function'; }; var getDeltaAngle = (startAngle, endAngle) => { var sign = mathSign(endAngle - startAngle); var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360); return sign * deltaAngle; }; var renderRadialLabel = (labelProps, position, label, attrs, viewBox, halo) => { var offset = labelProps.offset, className = labelProps.className, background = labelProps.background; var cx = viewBox.cx, cy = viewBox.cy, innerRadius = viewBox.innerRadius, outerRadius = viewBox.outerRadius, startAngle = viewBox.startAngle, endAngle = viewBox.endAngle, clockWise = viewBox.clockWise; var radius = (innerRadius + outerRadius) / 2; var deltaAngle = getDeltaAngle(startAngle, endAngle); var sign = deltaAngle >= 0 ? 1 : -1; var labelAngle, direction; switch (position) { case 'insideStart': labelAngle = startAngle + sign * offset; direction = clockWise; break; case 'insideEnd': labelAngle = endAngle - sign * offset; direction = !clockWise; break; case 'end': labelAngle = endAngle + sign * offset; direction = clockWise; break; default: throw new Error("Unsupported position ".concat(position)); } direction = deltaAngle <= 0 ? direction : !direction; var hasBackground = background != null && background !== false; var backgroundPadding = hasBackground ? resolveBackgroundPadding(typeof background === 'object' ? background.padding : undefined).x : 0; var directionSign = direction ? 1 : -1; var textAnchor = attrs.textAnchor, requestedStartOffset = attrs.startOffset, textAttrs = _objectWithoutProperties(attrs, _excluded); var pathPadding = requestedStartOffset == null || isNumber(requestedStartOffset) ? backgroundPadding : 0; var backgroundPaddingAngle = radius > 0 ? pathPadding / radius * (180 / Math.PI) : 0; var pathStartAngle = labelAngle - directionSign * backgroundPaddingAngle; var startPoint = polarToCartesian(cx, cy, radius, pathStartAngle); var endPoint = polarToCartesian(cx, cy, radius, pathStartAngle + directionSign * 359); var path = "M".concat(startPoint.x, ",").concat(startPoint.y, "\n A").concat(radius, ",").concat(radius, ",0,1,").concat(direction ? 0 : 1, ",\n ").concat(endPoint.x, ",").concat(endPoint.y); var resolvedStartOffset = isNumber(requestedStartOffset) ? requestedStartOffset + pathPadding : requestedStartOffset !== null && requestedStartOffset !== void 0 ? requestedStartOffset : pathPadding || undefined; return /*#__PURE__*/React.createElement(Text, _extends({}, textAttrs, { dominantBaseline: "central", className: clsx('recharts-radial-bar-label', className), textAnchor: isValidTextAnchor(textAnchor) ? textAnchor : undefined, textPath: path, startOffset: resolvedStartOffset, background: background, halo: halo }), label); }; var getAttrsOfPolarLabel = (viewBox, offset, position) => { var cx = viewBox.cx, cy = viewBox.cy, innerRadius = viewBox.innerRadius, outerRadius = viewBox.outerRadius, startAngle = viewBox.startAngle, endAngle = viewBox.endAngle; var midAngle = (startAngle + endAngle) / 2; if (position === 'outside') { var _polarToCartesian = polarToCartesian(cx, cy, outerRadius + offset, midAngle), _x = _polarToCartesian.x, _y = _polarToCartesian.y; return { x: _x, y: _y, textAnchor: _x >= cx ? 'start' : 'end', verticalAnchor: 'middle' }; } if (position === 'center') { return { x: cx, y: cy, textAnchor: 'middle', verticalAnchor: 'middle' }; } if (position === 'centerTop') { return { x: cx, y: cy, textAnchor: 'middle', verticalAnchor: 'start' }; } if (position === 'centerBottom') { return { x: cx, y: cy, textAnchor: 'middle', verticalAnchor: 'end' }; } var r = (innerRadius + outerRadius) / 2; var _polarToCartesian2 = polarToCartesian(cx, cy, r, midAngle), x = _polarToCartesian2.x, y = _polarToCartesian2.y; return { x, y, textAnchor: 'middle', verticalAnchor: 'middle' }; }; /** * Returns true if the label renders in a polar view box, which decides how its `position` is interpreted. */ export var isPolarViewBox = viewBox => viewBox != null && 'cx' in viewBox && isNumber(viewBox.cx); export var defaultLabelProps = { angle: 0, offset: 5, zIndex: DefaultZIndexes.label, position: 'middle', textBreakAll: false }; function polarViewBoxToTrapezoid(viewBox) { if (!isPolarViewBox(viewBox)) { return viewBox; } var cx = viewBox.cx, cy = viewBox.cy, outerRadius = viewBox.outerRadius; var diameter = outerRadius * 2; return { x: cx - outerRadius, y: cy - outerRadius, width: diameter, upperWidth: diameter, lowerWidth: diameter, height: diameter }; } /** * @consumes CartesianViewBoxContext * @consumes PolarViewBoxContext * @consumes CartesianLabelContext * @consumes PolarLabelContext * @public */ export function Label(outerProps) { var _positionAttrs, _positionAttrs2; var labelTheme = useGlobalLabelTheme(); var themedProps = labelTheme == null ? outerProps : _objectSpread(_objectSpread({}, outerProps), applyLabelTheme(labelTheme, outerProps)); var props = resolveDefaultProps(themedProps, defaultLabelProps); var viewBoxFromProps = props.viewBox, parentViewBox = props.parentViewBox, position = props.position, value = props.value, children = props.children, content = props.content, _props$className = props.className, className = _props$className === void 0 ? '' : _props$className, textBreakAll = props.textBreakAll, labelRef = props.labelRef; var polarViewBox = usePolarLabelContext(); var parentPolarViewBox = useContext(PolarLabelContext); var cartesianViewBox = useCartesianLabelContext(); /* * Labels inside a LabelList render with zIndex 0, in the layer of the list, * so their backgrounds go below the zIndex of the list. */ var enclosingLabelZIndex = useLabelZIndex(); var labelZIndex = props.zIndex === 0 ? enclosingLabelZIndex !== null && enclosingLabelZIndex !== void 0 ? enclosingLabelZIndex : props.zIndex : props.zIndex; var backgroundZIndex = getLabelBackgroundZIndex(labelZIndex, props.background); /* * I am not proud about this solution, but it's a quick fix for https://github.com/recharts/recharts/issues/6030#issuecomment-3155352460. * What we should really do is split Label into two components: CartesianLabel and PolarLabel and then handle their respective viewBoxes separately. * Also other components should set its own viewBox in a context so that we can fix https://github.com/recharts/recharts/issues/6156 * A Pie or a PolarRadiusAxis sets its own polar viewBox, so a centered Label inside of it uses that one. */ var resolvedViewBox = position === 'center' ? parentPolarViewBox !== null && parentPolarViewBox !== void 0 ? parentPolarViewBox : cartesianViewBox : polarViewBox !== null && polarViewBox !== void 0 ? polarViewBox : cartesianViewBox; var viewBox, label, positionAttrs; if (viewBoxFromProps == null) { viewBox = resolvedViewBox; } else if (isPolarViewBox(viewBoxFromProps)) { viewBox = viewBoxFromProps; } else { viewBox = cartesianViewBoxToTrapezoid(viewBoxFromProps); } var cartesianBox = polarViewBoxToTrapezoid(viewBox); if (!viewBox || isNullish(value) && isNullish(children) && ! /*#__PURE__*/isValidElement(content) && typeof content !== 'function') { return null; } // TODO: Generic Polar Hook var isRadialPolarLabel = isPolarViewBox(viewBox) && (position === 'insideStart' || position === 'insideEnd' || position === 'end'); if (isPolarViewBox(viewBox)) { if (!isRadialPolarLabel) { positionAttrs = getAttrsOfPolarLabel(viewBox, props.offset, props.position); } } else if (cartesianBox) { var cartesianResult = getCartesianPosition({ viewBox: cartesianBox, position, offset: props.offset, parentViewBox: isPolarViewBox(parentViewBox) ? undefined : parentViewBox, clamp: true }); positionAttrs = _objectSpread(_objectSpread({ x: cartesianResult.x, y: cartesianResult.y, textAnchor: cartesianResult.horizontalAnchor, verticalAnchor: cartesianResult.verticalAnchor }, cartesianResult.width !== undefined ? { width: cartesianResult.width } : {}), cartesianResult.height !== undefined ? { height: cartesianResult.height } : {}); } /* * Custom content receives the computed x and y too, so that custom labels * can be positioned the same way the built-in Text label is. Explicitly * passed props win over the computed values. We deliberately forward only * x and y (not the anchors): a custom label that spreads its props into a * nested Label would otherwise have that Label's own computed anchors * overridden by the inherited ones. * https://github.com/recharts/recharts/issues/5067 */ var propsWithViewBox = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, ((_positionAttrs = positionAttrs) === null || _positionAttrs === void 0 ? void 0 : _positionAttrs.x) !== undefined ? { x: positionAttrs.x } : {}), ((_positionAttrs2 = positionAttrs) === null || _positionAttrs2 === void 0 ? void 0 : _positionAttrs2.y) !== undefined ? { y: positionAttrs.y } : {}), props), {}, { viewBox }); if (/*#__PURE__*/isValidElement(content)) { /* * The element is often a plain SVG element such as `<text>`, * where `background` would end up as an invalid DOM attribute. */ var _ = propsWithViewBox.labelRef, _background = propsWithViewBox.background, _halo = propsWithViewBox.halo, propsForElement = _objectWithoutProperties(propsWithViewBox, _excluded2); return /*#__PURE__*/cloneElement(content, propsForElement); } if (typeof content === 'function') { var _2 = propsWithViewBox.content, propsForContent = _objectWithoutProperties(propsWithViewBox, _excluded3); // @ts-expect-error we're not checking if the content component returns something that Text is able to render label = /*#__PURE__*/createElement(content, propsForContent); if (/*#__PURE__*/isValidElement(label)) { return label; } } else { label = getLabel(props); } var attrs = svgPropertiesAndEvents(props); if (isRadialPolarLabel && isPolarViewBox(viewBox)) { return renderRadialLabel(props, position, label, attrs, viewBox, props.halo); } if (positionAttrs == null) { return null; } return /*#__PURE__*/React.createElement(React.Fragment, null, props.background && /*#__PURE__*/React.createElement(ZIndexLayer, { zIndex: backgroundZIndex }), /*#__PURE__*/React.createElement(ZIndexLayer, { zIndex: props.zIndex }, /*#__PURE__*/React.createElement(LabelZIndexProvider, { value: labelZIndex }, /*#__PURE__*/React.createElement(Text, _extends({ ref: labelRef, className: clsx('recharts-label', className) }, attrs, positionAttrs, { /* * textAnchor is decided by default based on the `position` * but we allow overriding via props for precise control. */ textAnchor: isValidTextAnchor(attrs.textAnchor) ? attrs.textAnchor : positionAttrs.textAnchor, breakAll: textBreakAll, background: props.background, halo: props.halo }), label)))); } Label.displayName = 'Label'; var parseLabel = (label, viewBox, labelRef) => { if (!label && label !== 0) { return null; } var commonProps = { viewBox, labelRef }; if (label === true) { return /*#__PURE__*/React.createElement(Label, _extends({ key: "label-implicit" }, commonProps)); } if (isNumOrStr(label)) { return /*#__PURE__*/React.createElement(Label, _extends({ key: "label-implicit", value: label }, commonProps)); } if (/*#__PURE__*/isValidElement(label)) { if (label.type === Label) { return /*#__PURE__*/cloneElement(label, _objectSpread({ key: 'label-implicit' }, commonProps)); } return /*#__PURE__*/React.createElement(Label, _extends({ key: "label-implicit", content: label }, commonProps)); } if (isLabelContentAFunction(label)) { return /*#__PURE__*/React.createElement(Label, _extends({ key: "label-implicit", content: label }, commonProps)); } if (label && typeof label === 'object') { return /*#__PURE__*/React.createElement(Label, _extends({}, label, { key: "label-implicit" }, commonProps)); } return null; }; export function CartesianLabelFromLabelProp(_ref3) { var label = _ref3.label, labelRef = _ref3.labelRef; var viewBox = useCartesianLabelContext(); return parseLabel(label, viewBox, labelRef) || null; } export function PolarLabelFromLabelProp(_ref4) { var label = _ref4.label; var viewBox = usePolarLabelContext(); return parseLabel(label, viewBox) || null; }