UNPKG

recharts

Version:
244 lines (233 loc) • 11.2 kB
var _excluded = ["valueAccessor"], _excluded2 = ["dataKey", "clockWise", "id", "textBreakAll", "zIndex"]; 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 { createContext, useContext } from 'react'; import { isLabelContentAFunction, isPolarViewBox, Label } from './Label'; import { Layer } from '../container/Layer'; import { getValueByDataKey } from '../util/ChartUtils'; import { isNullish } from '../util/DataUtils'; import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents'; import { ZIndexLayer } from '../zIndex/ZIndexLayer'; import { DefaultZIndexes } from '../zIndex/DefaultZIndexes'; import { getLabelBackgroundZIndex, LabelZIndexProvider } from '../zIndex/labelBackgroundZIndex'; import { isRenderableText } from './Text'; import { useGraphicalItemClipArea } from '../cartesian/GraphicalItemClipPath'; import { applyLabelTheme, DataLabelContextProvider, getAutomaticDataLabelFill, resolveLabelBackground, useDataLabelColors, useDataLabelThemeResolver } from '../theme/labelTheme'; import { useRechartsTheme } from '../theme/RechartsThemeContext'; import { getEffectivePageBackground } from '../theme/pageBackground'; /** * @public */ /** * This is public API because we expose it as the valueAccessor parameter. * * The properties of "viewBox" are repeated as the root props of the entry object. * So it doesn't matter if you read entry.x or entry.viewBox.x, they are the same. * * It's not necessary to pass redundant data, but we keep it for backward compatibility. */ /** * LabelList props do not allow refs because the same props are reused in multiple elements so we don't have a good single place to ref to. */ /** * @public */ /** * This is the type accepted for the `label` prop on various graphical items. * It accepts: * * boolean: * true = labels show, * false = labels don't show * React element: * will be cloned with extra props * function: * is used as <Label content={function} />, so this will be called once for each individual label (so typically once for each data point) * object: * the props to be passed to a LabelList component * * @inline * @public */ var defaultAccessor = entry => { var val = Array.isArray(entry.value) ? entry.value[entry.value.length - 1] : entry.value; if (isRenderableText(val)) { return val; } return undefined; }; var CartesianLabelListContext = /*#__PURE__*/createContext(undefined); export var CartesianLabelListContextProvider = CartesianLabelListContext.Provider; var PolarLabelListContext = /*#__PURE__*/createContext(undefined); export var PolarLabelListContextProvider = PolarLabelListContext.Provider; var LabelListClipAreaContext = /*#__PURE__*/createContext(undefined); /** * Graphical items that can be clipped (for example when an axis has `allowDataOverflow`) render this around their labels. * The labels are not clipped, because the label of a point on the edge of the domain is often drawn outside of the clip area. * Instead, LabelList skips the labels of elements that are completely outside of the clip area. */ export function SetLabelListClipArea(_ref) { var xAxisId = _ref.xAxisId, yAxisId = _ref.yAxisId, children = _ref.children; var clipArea = useGraphicalItemClipArea(xAxisId, yAxisId); return /*#__PURE__*/React.createElement(LabelListClipAreaContext.Provider, { value: clipArea }, children); } function isInRange(start, size, areaStart, areaSize) { var areaEnd = areaStart + areaSize; if (size === 0) { // A point exactly on the edge of the domain is inside, so that it keeps its label. return start >= areaStart && start <= areaEnd; } // A bar that only touches the edge from outside is not visible at all. return Math.max(start, start + size) > areaStart && Math.min(start, start + size) < areaEnd; } /** * The element of a label is a point (Line, Area), a rectangle (Bar), or the box of a symbol (Scatter). * The label is skipped if its element is outside of the clip area in one of the clipped dimensions. */ function isClippedAway(entry, clipArea) { if (entry == null || clipArea == null) { return false; } return clipArea.isXClipped && !isInRange(entry.x, entry.width, clipArea.x, clipArea.width) || clipArea.isYClipped && !isInRange(entry.y, entry.height, clipArea.y, clipArea.height); } /** Resolves eager or lazy Cartesian label entries only when a LabelList consumes the context. */ function useCartesianLabelListContext() { var value = useContext(CartesianLabelListContext); return typeof value === 'function' ? value() : value; } function usePolarLabelListContext() { return useContext(PolarLabelListContext); } /** * @consumes LabelListContext * @public */ export function LabelList(_ref2) { var _ref2$valueAccessor = _ref2.valueAccessor, valueAccessor = _ref2$valueAccessor === void 0 ? defaultAccessor : _ref2$valueAccessor, restProps = _objectWithoutProperties(_ref2, _excluded); var dataKey = restProps.dataKey, clockWise = restProps.clockWise, id = restProps.id, textBreakAll = restProps.textBreakAll, zIndex = restProps.zIndex, others = _objectWithoutProperties(restProps, _excluded2); var cartesianData = useCartesianLabelListContext(); var polarData = usePolarLabelListContext(); var clipArea = useContext(LabelListClipAreaContext); var resolveLabelTheme = useDataLabelThemeResolver(); var theme = useRechartsTheme(); var dataLabelColors = useDataLabelColors(); // Automatic colors apply only with a theme. Without one, labels keep the fill of their entry. var automaticLabelColors = theme == null ? undefined : dataLabelColors; var pageBackground = getEffectivePageBackground(theme); var data = cartesianData || polarData; var labelZIndex = zIndex !== null && zIndex !== void 0 ? zIndex : DefaultZIndexes.label; var backgroundZIndex = getLabelBackgroundZIndex(labelZIndex, others.background); if (!data || !data.length) { return null; } return /*#__PURE__*/React.createElement(React.Fragment, null, others.background && /*#__PURE__*/React.createElement(ZIndexLayer, { zIndex: backgroundZIndex }), /*#__PURE__*/React.createElement(ZIndexLayer, { zIndex: labelZIndex }, /*#__PURE__*/React.createElement(DataLabelContextProvider, { value: true }, /*#__PURE__*/React.createElement(LabelZIndexProvider, { value: labelZIndex }, /*#__PURE__*/React.createElement(Layer, { className: "recharts-label-list" }, data.map((entry, position) => { var _entry$originalDataIn, _automaticLabelColors, _automaticLabelColors2; if (isClippedAway(cartesianData === null || cartesianData === void 0 ? void 0 : cartesianData[position], clipArea)) { return null; } /* * `position` is where the entry sits in the list, `index` is the data entry the label belongs to. * They differ when the graphical item does not render every data entry. * Colors and themes read the lists by position; ids, keys and the index users get are data indexes. */ var index = (_entry$originalDataIn = entry.originalDataIndex) !== null && _entry$originalDataIn !== void 0 ? _entry$originalDataIn : position; var value = isNullish(dataKey) ? valueAccessor(entry, index) : getValueByDataKey(entry.payload, dataKey); var idProps = isNullish(id) ? {} : { id: "".concat(id, "-").concat(index) }; /* * The explicit fill from LabelList props wins, then the fill from the label theme. * Without a theme, the label falls back to the fill of the entry. * With a theme, the fallback depends on the position, so that a label on top of a shape stays readable. */ var labelTheme = resolveLabelTheme === null || resolveLabelTheme === void 0 ? void 0 : resolveLabelTheme(position); var background = resolveLabelBackground(restProps.background, labelTheme === null || labelTheme === void 0 ? void 0 : labelTheme.background); var fallbackFill = automaticLabelColors == null ? entry.fill : getAutomaticDataLabelFill({ position: others.position, isPolar: isPolarViewBox(entry.viewBox), hasFilledShapes: automaticLabelColors.hasFilledShapes, shapeColors: (_automaticLabelColors = (_automaticLabelColors2 = automaticLabelColors.getShapeColors) === null || _automaticLabelColors2 === void 0 ? void 0 : _automaticLabelColors2.call(automaticLabelColors, position)) !== null && _automaticLabelColors !== void 0 ? _automaticLabelColors : { fill: entry.fill }, pageBackground, hasOwnBackground: Boolean(background) || Boolean(others.halo) }); var themedProps = applyLabelTheme(labelTheme, restProps, fallbackFill); return /*#__PURE__*/React.createElement(Label, _extends({ key: "label-".concat(index) }, svgPropertiesAndEvents(entry), others, idProps, { fill: themedProps.fill, style: themedProps.style, background: themedProps.background, parentViewBox: entry.parentViewBox, value: value, textBreakAll: textBreakAll, viewBox: entry.viewBox, index: index /* * Here we don't want to use the default Label zIndex, * we want it to inherit the zIndex of the LabelList itself * which means just rendering as a regular child, without portaling anywhere. */, zIndex: 0 })); })))))); } LabelList.displayName = 'LabelList'; function isLabelListProps(value) { return typeof value === 'object' && value !== null && ! /*#__PURE__*/React.isValidElement(value); } export function LabelListFromLabelProp(_ref3) { var label = _ref3.label, pointerEvents = _ref3.pointerEvents; if (!label) { return null; } if (label === true) { return /*#__PURE__*/React.createElement(LabelList, { key: "labelList-implicit", pointerEvents: pointerEvents }); } if (/*#__PURE__*/React.isValidElement(label) || isLabelContentAFunction(label)) { return /*#__PURE__*/React.createElement(LabelList, { key: "labelList-implicit", content: label, pointerEvents: pointerEvents }); } if (isLabelListProps(label)) { var _label$pointerEvents; return /*#__PURE__*/React.createElement(LabelList, _extends({ key: "labelList-implicit" }, label, { type: String(label.type), pointerEvents: (_label$pointerEvents = label.pointerEvents) !== null && _label$pointerEvents !== void 0 ? _label$pointerEvents : pointerEvents })); } return null; }