| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.CartesianLabelListContextProvider = void 0;
|
|---|
| 7 | exports.LabelList = LabelList;
|
|---|
| 8 | exports.LabelListFromLabelProp = LabelListFromLabelProp;
|
|---|
| 9 | exports.PolarLabelListContextProvider = void 0;
|
|---|
| 10 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 11 | var React = _react;
|
|---|
| 12 | var _last = _interopRequireDefault(require("es-toolkit/compat/last"));
|
|---|
| 13 | var _Label = require("./Label");
|
|---|
| 14 | var _Layer = require("../container/Layer");
|
|---|
| 15 | var _ChartUtils = require("../util/ChartUtils");
|
|---|
| 16 | var _DataUtils = require("../util/DataUtils");
|
|---|
| 17 | var _svgPropertiesAndEvents = require("../util/svgPropertiesAndEvents");
|
|---|
| 18 | var _ZIndexLayer = require("../zIndex/ZIndexLayer");
|
|---|
| 19 | var _DefaultZIndexes = require("../zIndex/DefaultZIndexes");
|
|---|
| 20 | var _excluded = ["valueAccessor"],
|
|---|
| 21 | _excluded2 = ["dataKey", "clockWise", "id", "textBreakAll", "zIndex"];
|
|---|
| 22 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 23 | 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); }
|
|---|
| 24 | 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); }
|
|---|
| 25 | 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; }
|
|---|
| 26 | 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; }
|
|---|
| 27 | /**
|
|---|
| 28 | * This is public API because we expose it as the valueAccessor parameter.
|
|---|
| 29 | *
|
|---|
| 30 | * The properties of "viewBox" are repeated as the root props of the entry object.
|
|---|
| 31 | * So it doesn't matter if you read entry.x or entry.viewBox.x, they are the same.
|
|---|
| 32 | *
|
|---|
| 33 | * It's not necessary to pass redundant data, but we keep it for backward compatibility.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * 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.
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * This is the type accepted for the `label` prop on various graphical items.
|
|---|
| 42 | * It accepts:
|
|---|
| 43 | *
|
|---|
| 44 | * boolean:
|
|---|
| 45 | * true = labels show,
|
|---|
| 46 | * false = labels don't show
|
|---|
| 47 | * React element:
|
|---|
| 48 | * will be cloned with extra props
|
|---|
| 49 | * function:
|
|---|
| 50 | * is used as <Label content={function} />, so this will be called once for each individual label (so typically once for each data point)
|
|---|
| 51 | * object:
|
|---|
| 52 | * the props to be passed to a LabelList component
|
|---|
| 53 | *
|
|---|
| 54 | * @inline
|
|---|
| 55 | */
|
|---|
| 56 |
|
|---|
| 57 | var defaultAccessor = entry => Array.isArray(entry.value) ? (0, _last.default)(entry.value) : entry.value;
|
|---|
| 58 | var CartesianLabelListContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|---|
| 59 | var CartesianLabelListContextProvider = exports.CartesianLabelListContextProvider = CartesianLabelListContext.Provider;
|
|---|
| 60 | var PolarLabelListContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|---|
| 61 | var PolarLabelListContextProvider = exports.PolarLabelListContextProvider = PolarLabelListContext.Provider;
|
|---|
| 62 | function useCartesianLabelListContext() {
|
|---|
| 63 | return (0, _react.useContext)(CartesianLabelListContext);
|
|---|
| 64 | }
|
|---|
| 65 | function usePolarLabelListContext() {
|
|---|
| 66 | return (0, _react.useContext)(PolarLabelListContext);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * @consumes LabelListContext
|
|---|
| 71 | */
|
|---|
| 72 | function LabelList(_ref) {
|
|---|
| 73 | var {
|
|---|
| 74 | valueAccessor = defaultAccessor
|
|---|
| 75 | } = _ref,
|
|---|
| 76 | restProps = _objectWithoutProperties(_ref, _excluded);
|
|---|
| 77 | var {
|
|---|
| 78 | dataKey,
|
|---|
| 79 | clockWise,
|
|---|
| 80 | id,
|
|---|
| 81 | textBreakAll,
|
|---|
| 82 | zIndex
|
|---|
| 83 | } = restProps,
|
|---|
| 84 | others = _objectWithoutProperties(restProps, _excluded2);
|
|---|
| 85 | var cartesianData = useCartesianLabelListContext();
|
|---|
| 86 | var polarData = usePolarLabelListContext();
|
|---|
| 87 | var data = cartesianData || polarData;
|
|---|
| 88 | if (!data || !data.length) {
|
|---|
| 89 | return null;
|
|---|
| 90 | }
|
|---|
| 91 | return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
|
|---|
| 92 | zIndex: zIndex !== null && zIndex !== void 0 ? zIndex : _DefaultZIndexes.DefaultZIndexes.label
|
|---|
| 93 | }, /*#__PURE__*/React.createElement(_Layer.Layer, {
|
|---|
| 94 | className: "recharts-label-list"
|
|---|
| 95 | }, data.map((entry, index) => {
|
|---|
| 96 | var _restProps$fill;
|
|---|
| 97 | var value = (0, _DataUtils.isNullish)(dataKey) ? valueAccessor(entry, index) : (0, _ChartUtils.getValueByDataKey)(entry.payload, dataKey);
|
|---|
| 98 | var idProps = (0, _DataUtils.isNullish)(id) ? {} : {
|
|---|
| 99 | id: "".concat(id, "-").concat(index)
|
|---|
| 100 | };
|
|---|
| 101 | return /*#__PURE__*/React.createElement(_Label.Label, _extends({
|
|---|
| 102 | key: "label-".concat(index)
|
|---|
| 103 | }, (0, _svgPropertiesAndEvents.svgPropertiesAndEvents)(entry), others, idProps, {
|
|---|
| 104 | /*
|
|---|
| 105 | * Prefer to use the explicit fill from LabelList props.
|
|---|
| 106 | * Only in an absence of that, fall back to the fill of the entry.
|
|---|
| 107 | * The entry fill can be quite difficult to see especially in Bar, Pie, RadialBar in inside positions.
|
|---|
| 108 | * On the other hand it's quite convenient in Scatter, Line, or when the position is outside the Bar, Pie filled shapes.
|
|---|
| 109 | */
|
|---|
| 110 | fill: (_restProps$fill = restProps.fill) !== null && _restProps$fill !== void 0 ? _restProps$fill : entry.fill,
|
|---|
| 111 | parentViewBox: entry.parentViewBox,
|
|---|
| 112 | value: value,
|
|---|
| 113 | textBreakAll: textBreakAll,
|
|---|
| 114 | viewBox: entry.viewBox,
|
|---|
| 115 | index: index
|
|---|
| 116 | /*
|
|---|
| 117 | * Here we don't want to use the default Label zIndex,
|
|---|
| 118 | * we want it to inherit the zIndex of the LabelList itself
|
|---|
| 119 | * which means just rendering as a regular child, without portaling anywhere.
|
|---|
| 120 | */,
|
|---|
| 121 | zIndex: 0
|
|---|
| 122 | }));
|
|---|
| 123 | })));
|
|---|
| 124 | }
|
|---|
| 125 | LabelList.displayName = 'LabelList';
|
|---|
| 126 | function LabelListFromLabelProp(_ref2) {
|
|---|
| 127 | var {
|
|---|
| 128 | label
|
|---|
| 129 | } = _ref2;
|
|---|
| 130 | if (!label) {
|
|---|
| 131 | return null;
|
|---|
| 132 | }
|
|---|
| 133 | if (label === true) {
|
|---|
| 134 | return /*#__PURE__*/React.createElement(LabelList, {
|
|---|
| 135 | key: "labelList-implicit"
|
|---|
| 136 | });
|
|---|
| 137 | }
|
|---|
| 138 | if (/*#__PURE__*/React.isValidElement(label) || (0, _Label.isLabelContentAFunction)(label)) {
|
|---|
| 139 | return /*#__PURE__*/React.createElement(LabelList, {
|
|---|
| 140 | key: "labelList-implicit",
|
|---|
| 141 | content: label
|
|---|
| 142 | });
|
|---|
| 143 | }
|
|---|
| 144 | if (typeof label === 'object') {
|
|---|
| 145 | return /*#__PURE__*/React.createElement(LabelList, _extends({
|
|---|
| 146 | key: "labelList-implicit"
|
|---|
| 147 | }, label, {
|
|---|
| 148 | type: String(label.type)
|
|---|
| 149 | }));
|
|---|
| 150 | }
|
|---|
| 151 | return null;
|
|---|
| 152 | } |
|---|