source: node_modules/recharts/lib/component/Label.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 15.4 KB
RevLine 
[a762898]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.CartesianLabelContextProvider = void 0;
7exports.CartesianLabelFromLabelProp = CartesianLabelFromLabelProp;
8exports.Label = Label;
9exports.PolarLabelContextProvider = void 0;
10exports.PolarLabelFromLabelProp = PolarLabelFromLabelProp;
11exports.usePolarLabelContext = exports.isLabelContentAFunction = exports.defaultLabelProps = void 0;
12var _react = _interopRequireWildcard(require("react"));
13var React = _react;
14var _clsx = require("clsx");
15var _Text = require("./Text");
16var _DataUtils = require("../util/DataUtils");
17var _PolarUtils = require("../util/PolarUtils");
18var _chartLayoutContext = require("../context/chartLayoutContext");
19var _hooks = require("../state/hooks");
20var _polarAxisSelectors = require("../state/selectors/polarAxisSelectors");
21var _resolveDefaultProps = require("../util/resolveDefaultProps");
22var _svgPropertiesAndEvents = require("../util/svgPropertiesAndEvents");
23var _ZIndexLayer = require("../zIndex/ZIndexLayer");
24var _DefaultZIndexes = require("../zIndex/DefaultZIndexes");
25var _getCartesianPosition = require("../cartesian/getCartesianPosition");
26var _excluded = ["labelRef"],
27 _excluded2 = ["content"];
28function _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); }
29function _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; }
30function _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; }
31function 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; }
32function _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; }
33function _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; }
34function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
35function _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); }
36function _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); }
37/**
38 * @inline
39 */
40
41/**
42 * @inline
43 */
44
45/**
46 * @inline
47 */
48
49var CartesianLabelContext = /*#__PURE__*/(0, _react.createContext)(null);
50var CartesianLabelContextProvider = _ref => {
51 var {
52 x,
53 y,
54 upperWidth,
55 lowerWidth,
56 width,
57 height,
58 children
59 } = _ref;
60 var viewBox = (0, _react.useMemo)(() => ({
61 x,
62 y,
63 upperWidth,
64 lowerWidth,
65 width,
66 height
67 }), [x, y, upperWidth, lowerWidth, width, height]);
68 return /*#__PURE__*/React.createElement(CartesianLabelContext.Provider, {
69 value: viewBox
70 }, children);
71};
72exports.CartesianLabelContextProvider = CartesianLabelContextProvider;
73var useCartesianLabelContext = () => {
74 var labelChildContext = (0, _react.useContext)(CartesianLabelContext);
75 var chartContext = (0, _chartLayoutContext.useViewBox)();
76 return labelChildContext || (chartContext ? (0, _chartLayoutContext.cartesianViewBoxToTrapezoid)(chartContext) : undefined);
77};
78var PolarLabelContext = /*#__PURE__*/(0, _react.createContext)(null);
79var PolarLabelContextProvider = _ref2 => {
80 var {
81 cx,
82 cy,
83 innerRadius,
84 outerRadius,
85 startAngle,
86 endAngle,
87 clockWise,
88 children
89 } = _ref2;
90 var viewBox = (0, _react.useMemo)(() => ({
91 cx,
92 cy,
93 innerRadius,
94 outerRadius,
95 startAngle,
96 endAngle,
97 clockWise
98 }), [cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise]);
99 return /*#__PURE__*/React.createElement(PolarLabelContext.Provider, {
100 value: viewBox
101 }, children);
102};
103exports.PolarLabelContextProvider = PolarLabelContextProvider;
104var usePolarLabelContext = () => {
105 var labelChildContext = (0, _react.useContext)(PolarLabelContext);
106 var chartContext = (0, _hooks.useAppSelector)(_polarAxisSelectors.selectPolarViewBox);
107 return labelChildContext || chartContext;
108};
109exports.usePolarLabelContext = usePolarLabelContext;
110var getLabel = props => {
111 var {
112 value,
113 formatter
114 } = props;
115 var label = (0, _DataUtils.isNullish)(props.children) ? value : props.children;
116 if (typeof formatter === 'function') {
117 return formatter(label);
118 }
119 return label;
120};
121var isLabelContentAFunction = content => {
122 return content != null && typeof content === 'function';
123};
124exports.isLabelContentAFunction = isLabelContentAFunction;
125var getDeltaAngle = (startAngle, endAngle) => {
126 var sign = (0, _DataUtils.mathSign)(endAngle - startAngle);
127 var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360);
128 return sign * deltaAngle;
129};
130var renderRadialLabel = (labelProps, position, label, attrs, viewBox) => {
131 var {
132 offset,
133 className
134 } = labelProps;
135 var {
136 cx,
137 cy,
138 innerRadius,
139 outerRadius,
140 startAngle,
141 endAngle,
142 clockWise
143 } = viewBox;
144 var radius = (innerRadius + outerRadius) / 2;
145 var deltaAngle = getDeltaAngle(startAngle, endAngle);
146 var sign = deltaAngle >= 0 ? 1 : -1;
147 var labelAngle, direction;
148 switch (position) {
149 case 'insideStart':
150 labelAngle = startAngle + sign * offset;
151 direction = clockWise;
152 break;
153 case 'insideEnd':
154 labelAngle = endAngle - sign * offset;
155 direction = !clockWise;
156 break;
157 case 'end':
158 labelAngle = endAngle + sign * offset;
159 direction = clockWise;
160 break;
161 default:
162 throw new Error("Unsupported position ".concat(position));
163 }
164 direction = deltaAngle <= 0 ? direction : !direction;
165 var startPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, radius, labelAngle);
166 var endPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, radius, labelAngle + (direction ? 1 : -1) * 359);
167 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);
168 var id = (0, _DataUtils.isNullish)(labelProps.id) ? (0, _DataUtils.uniqueId)('recharts-radial-line-') : labelProps.id;
169 return /*#__PURE__*/React.createElement("text", _extends({}, attrs, {
170 dominantBaseline: "central",
171 className: (0, _clsx.clsx)('recharts-radial-bar-label', className)
172 }), /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("path", {
173 id: id,
174 d: path
175 })), /*#__PURE__*/React.createElement("textPath", {
176 xlinkHref: "#".concat(id)
177 }, label));
178};
179var getAttrsOfPolarLabel = (viewBox, offset, position) => {
180 var {
181 cx,
182 cy,
183 innerRadius,
184 outerRadius,
185 startAngle,
186 endAngle
187 } = viewBox;
188 var midAngle = (startAngle + endAngle) / 2;
189 if (position === 'outside') {
190 var {
191 x: _x,
192 y: _y
193 } = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius + offset, midAngle);
194 return {
195 x: _x,
196 y: _y,
197 textAnchor: _x >= cx ? 'start' : 'end',
198 verticalAnchor: 'middle'
199 };
200 }
201 if (position === 'center') {
202 return {
203 x: cx,
204 y: cy,
205 textAnchor: 'middle',
206 verticalAnchor: 'middle'
207 };
208 }
209 if (position === 'centerTop') {
210 return {
211 x: cx,
212 y: cy,
213 textAnchor: 'middle',
214 verticalAnchor: 'start'
215 };
216 }
217 if (position === 'centerBottom') {
218 return {
219 x: cx,
220 y: cy,
221 textAnchor: 'middle',
222 verticalAnchor: 'end'
223 };
224 }
225 var r = (innerRadius + outerRadius) / 2;
226 var {
227 x,
228 y
229 } = (0, _PolarUtils.polarToCartesian)(cx, cy, r, midAngle);
230 return {
231 x,
232 y,
233 textAnchor: 'middle',
234 verticalAnchor: 'middle'
235 };
236};
237var isPolar = viewBox => viewBox != null && 'cx' in viewBox && (0, _DataUtils.isNumber)(viewBox.cx);
238var defaultLabelProps = exports.defaultLabelProps = {
239 angle: 0,
240 offset: 5,
241 zIndex: _DefaultZIndexes.DefaultZIndexes.label,
242 position: 'middle',
243 textBreakAll: false
244};
245function polarViewBoxToTrapezoid(viewBox) {
246 if (!isPolar(viewBox)) {
247 return viewBox;
248 }
249 var {
250 cx,
251 cy,
252 outerRadius
253 } = viewBox;
254 var diameter = outerRadius * 2;
255 return {
256 x: cx - outerRadius,
257 y: cy - outerRadius,
258 width: diameter,
259 upperWidth: diameter,
260 lowerWidth: diameter,
261 height: diameter
262 };
263}
264
265/**
266 * @consumes CartesianViewBoxContext
267 * @consumes PolarViewBoxContext
268 * @consumes CartesianLabelContext
269 * @consumes PolarLabelContext
270 */
271function Label(outerProps) {
272 var props = (0, _resolveDefaultProps.resolveDefaultProps)(outerProps, defaultLabelProps);
273 var {
274 viewBox: viewBoxFromProps,
275 parentViewBox,
276 position,
277 value,
278 children,
279 content,
280 className = '',
281 textBreakAll,
282 labelRef
283 } = props;
284 var polarViewBox = usePolarLabelContext();
285 var cartesianViewBox = useCartesianLabelContext();
286
287 /*
288 * I am not proud about this solution, but it's a quick fix for https://github.com/recharts/recharts/issues/6030#issuecomment-3155352460.
289 * What we should really do is split Label into two components: CartesianLabel and PolarLabel and then handle their respective viewBoxes separately.
290 * Also other components should set its own viewBox in a context so that we can fix https://github.com/recharts/recharts/issues/6156
291 */
292 var resolvedViewBox = position === 'center' ? cartesianViewBox : polarViewBox !== null && polarViewBox !== void 0 ? polarViewBox : cartesianViewBox;
293 var viewBox, label, positionAttrs;
294 if (viewBoxFromProps == null) {
295 viewBox = resolvedViewBox;
296 } else if (isPolar(viewBoxFromProps)) {
297 viewBox = viewBoxFromProps;
298 } else {
299 viewBox = (0, _chartLayoutContext.cartesianViewBoxToTrapezoid)(viewBoxFromProps);
300 }
301 var cartesianBox = polarViewBoxToTrapezoid(viewBox);
302 if (!viewBox || (0, _DataUtils.isNullish)(value) && (0, _DataUtils.isNullish)(children) && ! /*#__PURE__*/(0, _react.isValidElement)(content) && typeof content !== 'function') {
303 return null;
304 }
305 var propsWithViewBox = _objectSpread(_objectSpread({}, props), {}, {
306 viewBox
307 });
308 if (/*#__PURE__*/(0, _react.isValidElement)(content)) {
309 var {
310 labelRef: _
311 } = propsWithViewBox,
312 propsWithoutLabelRef = _objectWithoutProperties(propsWithViewBox, _excluded);
313 return /*#__PURE__*/(0, _react.cloneElement)(content, propsWithoutLabelRef);
314 }
315 if (typeof content === 'function') {
316 var {
317 content: _2
318 } = propsWithViewBox,
319 propsForContent = _objectWithoutProperties(propsWithViewBox, _excluded2);
320 // @ts-expect-error we're not checking if the content component returns something that Text is able to render
321 label = /*#__PURE__*/(0, _react.createElement)(content, propsForContent);
322 if (/*#__PURE__*/(0, _react.isValidElement)(label)) {
323 return label;
324 }
325 } else {
326 label = getLabel(props);
327 }
328 var attrs = (0, _svgPropertiesAndEvents.svgPropertiesAndEvents)(props);
329 if (isPolar(viewBox)) {
330 // TODO: Generic Polar Hook
331 if (position === 'insideStart' || position === 'insideEnd' || position === 'end') {
332 return renderRadialLabel(props, position, label, attrs, viewBox);
333 }
334 positionAttrs = getAttrsOfPolarLabel(viewBox, props.offset, props.position);
335 } else {
336 if (!cartesianBox) {
337 return null;
338 }
339 var cartesianResult = (0, _getCartesianPosition.getCartesianPosition)({
340 viewBox: cartesianBox,
341 position,
342 offset: props.offset,
343 parentViewBox: isPolar(parentViewBox) ? undefined : parentViewBox,
344 clamp: true
345 });
346 positionAttrs = _objectSpread(_objectSpread({
347 x: cartesianResult.x,
348 y: cartesianResult.y,
349 textAnchor: cartesianResult.horizontalAnchor,
350 verticalAnchor: cartesianResult.verticalAnchor
351 }, cartesianResult.width !== undefined ? {
352 width: cartesianResult.width
353 } : {}), cartesianResult.height !== undefined ? {
354 height: cartesianResult.height
355 } : {});
356 }
357 return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
358 zIndex: props.zIndex
359 }, /*#__PURE__*/React.createElement(_Text.Text, _extends({
360 ref: labelRef,
361 className: (0, _clsx.clsx)('recharts-label', className)
362 }, attrs, positionAttrs, {
363 /*
364 * textAnchor is decided by default based on the `position`
365 * but we allow overriding via props for precise control.
366 */
367 textAnchor: (0, _Text.isValidTextAnchor)(attrs.textAnchor) ? attrs.textAnchor : positionAttrs.textAnchor,
368 breakAll: textBreakAll
369 }), label));
370}
371Label.displayName = 'Label';
372var parseLabel = (label, viewBox, labelRef) => {
373 if (!label) {
374 return null;
375 }
376 var commonProps = {
377 viewBox,
378 labelRef
379 };
380 if (label === true) {
381 return /*#__PURE__*/React.createElement(Label, _extends({
382 key: "label-implicit"
383 }, commonProps));
384 }
385 if ((0, _DataUtils.isNumOrStr)(label)) {
386 return /*#__PURE__*/React.createElement(Label, _extends({
387 key: "label-implicit",
388 value: label
389 }, commonProps));
390 }
391 if (/*#__PURE__*/(0, _react.isValidElement)(label)) {
392 if (label.type === Label) {
393 return /*#__PURE__*/(0, _react.cloneElement)(label, _objectSpread({
394 key: 'label-implicit'
395 }, commonProps));
396 }
397 return /*#__PURE__*/React.createElement(Label, _extends({
398 key: "label-implicit",
399 content: label
400 }, commonProps));
401 }
402 if (isLabelContentAFunction(label)) {
403 return /*#__PURE__*/React.createElement(Label, _extends({
404 key: "label-implicit",
405 content: label
406 }, commonProps));
407 }
408 if (label && typeof label === 'object') {
409 return /*#__PURE__*/React.createElement(Label, _extends({}, label, {
410 key: "label-implicit"
411 }, commonProps));
412 }
413 return null;
414};
415function CartesianLabelFromLabelProp(_ref3) {
416 var {
417 label,
418 labelRef
419 } = _ref3;
420 var viewBox = useCartesianLabelContext();
421 return parseLabel(label, viewBox, labelRef) || null;
422}
423function PolarLabelFromLabelProp(_ref4) {
424 var {
425 label
426 } = _ref4;
427 var viewBox = usePolarLabelContext();
428 return parseLabel(label, viewBox) || null;
429}
Note: See TracBrowser for help on using the repository browser.