recharts
Version:
React charts
254 lines (243 loc) • 12.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CartesianLabelListContextProvider = void 0;
exports.LabelList = LabelList;
exports.LabelListFromLabelProp = LabelListFromLabelProp;
exports.PolarLabelListContextProvider = void 0;
exports.SetLabelListClipArea = SetLabelListClipArea;
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _Label = require("./Label");
var _Layer = require("../container/Layer");
var _ChartUtils = require("../util/ChartUtils");
var _DataUtils = require("../util/DataUtils");
var _svgPropertiesAndEvents = require("../util/svgPropertiesAndEvents");
var _ZIndexLayer = require("../zIndex/ZIndexLayer");
var _DefaultZIndexes = require("../zIndex/DefaultZIndexes");
var _labelBackgroundZIndex = require("../zIndex/labelBackgroundZIndex");
var _Text = require("./Text");
var _GraphicalItemClipPath = require("../cartesian/GraphicalItemClipPath");
var _labelTheme = require("../theme/labelTheme");
var _RechartsThemeContext = require("../theme/RechartsThemeContext");
var _pageBackground = require("../theme/pageBackground");
var _excluded = ["valueAccessor"],
_excluded2 = ["dataKey", "clockWise", "id", "textBreakAll", "zIndex"];
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); }
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; }
/**
* @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 ((0, _Text.isRenderableText)(val)) {
return val;
}
return undefined;
};
var CartesianLabelListContext = /*#__PURE__*/(0, _react.createContext)(undefined);
var CartesianLabelListContextProvider = exports.CartesianLabelListContextProvider = CartesianLabelListContext.Provider;
var PolarLabelListContext = /*#__PURE__*/(0, _react.createContext)(undefined);
var PolarLabelListContextProvider = exports.PolarLabelListContextProvider = PolarLabelListContext.Provider;
var LabelListClipAreaContext = /*#__PURE__*/(0, _react.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.
*/
function SetLabelListClipArea(_ref) {
var xAxisId = _ref.xAxisId,
yAxisId = _ref.yAxisId,
children = _ref.children;
var clipArea = (0, _GraphicalItemClipPath.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 = (0, _react.useContext)(CartesianLabelListContext);
return typeof value === 'function' ? value() : value;
}
function usePolarLabelListContext() {
return (0, _react.useContext)(PolarLabelListContext);
}
/**
* @consumes LabelListContext
* @public
*/
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 = (0, _react.useContext)(LabelListClipAreaContext);
var resolveLabelTheme = (0, _labelTheme.useDataLabelThemeResolver)();
var theme = (0, _RechartsThemeContext.useRechartsTheme)();
var dataLabelColors = (0, _labelTheme.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 = (0, _pageBackground.getEffectivePageBackground)(theme);
var data = cartesianData || polarData;
var labelZIndex = zIndex !== null && zIndex !== void 0 ? zIndex : _DefaultZIndexes.DefaultZIndexes.label;
var backgroundZIndex = (0, _labelBackgroundZIndex.getLabelBackgroundZIndex)(labelZIndex, others.background);
if (!data || !data.length) {
return null;
}
return /*#__PURE__*/React.createElement(React.Fragment, null, others.background && /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
zIndex: backgroundZIndex
}), /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
zIndex: labelZIndex
}, /*#__PURE__*/React.createElement(_labelTheme.DataLabelContextProvider, {
value: true
}, /*#__PURE__*/React.createElement(_labelBackgroundZIndex.LabelZIndexProvider, {
value: labelZIndex
}, /*#__PURE__*/React.createElement(_Layer.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 = (0, _DataUtils.isNullish)(dataKey) ? valueAccessor(entry, index) : (0, _ChartUtils.getValueByDataKey)(entry.payload, dataKey);
var idProps = (0, _DataUtils.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 = (0, _labelTheme.resolveLabelBackground)(restProps.background, labelTheme === null || labelTheme === void 0 ? void 0 : labelTheme.background);
var fallbackFill = automaticLabelColors == null ? entry.fill : (0, _labelTheme.getAutomaticDataLabelFill)({
position: others.position,
isPolar: (0, _Label.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 = (0, _labelTheme.applyLabelTheme)(labelTheme, restProps, fallbackFill);
return /*#__PURE__*/React.createElement(_Label.Label, _extends({
key: "label-".concat(index)
}, (0, _svgPropertiesAndEvents.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);
}
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) || (0, _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;
}