source: node_modules/recharts/lib/shape/Rectangle.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: 12.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.defaultRectangleProps = exports.Rectangle = void 0;
7var _react = _interopRequireWildcard(require("react"));
8var React = _react;
9var _clsx = require("clsx");
10var _resolveDefaultProps = require("../util/resolveDefaultProps");
11var _JavascriptAnimate = require("../animation/JavascriptAnimate");
12var _DataUtils = require("../util/DataUtils");
13var _useAnimationId = require("../util/useAnimationId");
14var _util = require("../animation/util");
15var _svgPropertiesAndEvents = require("../util/svgPropertiesAndEvents");
16var _round = require("../util/round");
17var _excluded = ["radius"],
18 _excluded2 = ["radius"];
19var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8, _templateObject9, _templateObject0;
20/**
21 * @fileOverview Rectangle
22 */
23function _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); }
24function 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; }
25function _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; }
26function _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; }
27function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
28function _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); }
29function _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); }
30function _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; }
31function _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; }
32function _taggedTemplateLiteral(e, t) { return t || (t = e.slice(0)), Object.freeze(Object.defineProperties(e, { raw: { value: Object.freeze(t) } })); }
33/**
34 * @inline
35 */
36
37var getRectanglePath = (x, y, width, height, radius) => {
38 var roundedWidth = (0, _round.round)(width);
39 var roundedHeight = (0, _round.round)(height);
40 var maxRadius = Math.min(Math.abs(roundedWidth) / 2, Math.abs(roundedHeight) / 2);
41 var ySign = roundedHeight >= 0 ? 1 : -1;
42 var xSign = roundedWidth >= 0 ? 1 : -1;
43 var clockWise = roundedHeight >= 0 && roundedWidth >= 0 || roundedHeight < 0 && roundedWidth < 0 ? 1 : 0;
44 var path;
45 if (maxRadius > 0 && Array.isArray(radius)) {
46 var newRadius = [0, 0, 0, 0];
47 for (var i = 0, len = 4; i < len; i++) {
48 var _radius$i;
49 var r = (_radius$i = radius[i]) !== null && _radius$i !== void 0 ? _radius$i : 0;
50 newRadius[i] = r > maxRadius ? maxRadius : r;
51 }
52 path = (0, _round.roundTemplateLiteral)(_templateObject || (_templateObject = _taggedTemplateLiteral(["M", ",", ""])), x, y + ySign * newRadius[0]);
53 if (newRadius[0] > 0) {
54 path += (0, _round.roundTemplateLiteral)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["A ", ",", ",0,0,", ",", ",", ""])), newRadius[0], newRadius[0], clockWise, x + xSign * newRadius[0], y);
55 }
56 path += (0, _round.roundTemplateLiteral)(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["L ", ",", ""])), x + width - xSign * newRadius[1], y);
57 if (newRadius[1] > 0) {
58 path += (0, _round.roundTemplateLiteral)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["A ", ",", ",0,0,", ",\n ", ",", ""])), newRadius[1], newRadius[1], clockWise, x + width, y + ySign * newRadius[1]);
59 }
60 path += (0, _round.roundTemplateLiteral)(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["L ", ",", ""])), x + width, y + height - ySign * newRadius[2]);
61 if (newRadius[2] > 0) {
62 path += (0, _round.roundTemplateLiteral)(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["A ", ",", ",0,0,", ",\n ", ",", ""])), newRadius[2], newRadius[2], clockWise, x + width - xSign * newRadius[2], y + height);
63 }
64 path += (0, _round.roundTemplateLiteral)(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["L ", ",", ""])), x + xSign * newRadius[3], y + height);
65 if (newRadius[3] > 0) {
66 path += (0, _round.roundTemplateLiteral)(_templateObject8 || (_templateObject8 = _taggedTemplateLiteral(["A ", ",", ",0,0,", ",\n ", ",", ""])), newRadius[3], newRadius[3], clockWise, x, y + height - ySign * newRadius[3]);
67 }
68 path += 'Z';
69 } else if (maxRadius > 0 && radius === +radius && radius > 0) {
70 var _newRadius = Math.min(maxRadius, radius);
71 path = (0, _round.roundTemplateLiteral)(_templateObject9 || (_templateObject9 = _taggedTemplateLiteral(["M ", ",", "\n A ", ",", ",0,0,", ",", ",", "\n L ", ",", "\n A ", ",", ",0,0,", ",", ",", "\n L ", ",", "\n A ", ",", ",0,0,", ",", ",", "\n L ", ",", "\n A ", ",", ",0,0,", ",", ",", " Z"])), x, y + ySign * _newRadius, _newRadius, _newRadius, clockWise, x + xSign * _newRadius, y, x + width - xSign * _newRadius, y, _newRadius, _newRadius, clockWise, x + width, y + ySign * _newRadius, x + width, y + height - ySign * _newRadius, _newRadius, _newRadius, clockWise, x + width - xSign * _newRadius, y + height, x + xSign * _newRadius, y + height, _newRadius, _newRadius, clockWise, x, y + height - ySign * _newRadius);
72 } else {
73 path = (0, _round.roundTemplateLiteral)(_templateObject0 || (_templateObject0 = _taggedTemplateLiteral(["M ", ",", " h ", " v ", " h ", " Z"])), x, y, width, height, -width);
74 }
75 return path;
76};
77var defaultRectangleProps = exports.defaultRectangleProps = {
78 x: 0,
79 y: 0,
80 width: 0,
81 height: 0,
82 radius: 0,
83 isAnimationActive: false,
84 isUpdateAnimationActive: false,
85 animationBegin: 0,
86 animationDuration: 1500,
87 animationEasing: 'ease'
88};
89
90/**
91 * Renders a rectangle element. Unlike the {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/rect rect SVG element}, this component supports rounded corners
92 * and animation.
93 *
94 * This component accepts X and Y coordinates in pixels.
95 * If you need to position the rectangle based on your chart's data,
96 * consider using the {@link ReferenceArea} component instead.
97 *
98 * @param rectangleProps
99 * @constructor
100 */
101var Rectangle = rectangleProps => {
102 var props = (0, _resolveDefaultProps.resolveDefaultProps)(rectangleProps, defaultRectangleProps);
103 var pathRef = (0, _react.useRef)(null);
104 var [totalLength, setTotalLength] = (0, _react.useState)(-1);
105 (0, _react.useEffect)(() => {
106 if (pathRef.current && pathRef.current.getTotalLength) {
107 try {
108 var pathTotalLength = pathRef.current.getTotalLength();
109 if (pathTotalLength) {
110 setTotalLength(pathTotalLength);
111 }
112 } catch (_unused) {
113 // calculate total length error
114 }
115 }
116 }, []);
117 var {
118 x,
119 y,
120 width,
121 height,
122 radius,
123 className
124 } = props;
125 var {
126 animationEasing,
127 animationDuration,
128 animationBegin,
129 isAnimationActive,
130 isUpdateAnimationActive
131 } = props;
132 var prevWidthRef = (0, _react.useRef)(width);
133 var prevHeightRef = (0, _react.useRef)(height);
134 var prevXRef = (0, _react.useRef)(x);
135 var prevYRef = (0, _react.useRef)(y);
136 var animationIdInput = (0, _react.useMemo)(() => ({
137 x,
138 y,
139 width,
140 height,
141 radius
142 }), [x, y, width, height, radius]);
143 var animationId = (0, _useAnimationId.useAnimationId)(animationIdInput, 'rectangle-');
144 if (x !== +x || y !== +y || width !== +width || height !== +height || width === 0 || height === 0) {
145 return null;
146 }
147 var layerClass = (0, _clsx.clsx)('recharts-rectangle', className);
148 if (!isUpdateAnimationActive) {
149 var _svgPropertiesAndEven = (0, _svgPropertiesAndEvents.svgPropertiesAndEvents)(props),
150 {
151 radius: _
152 } = _svgPropertiesAndEven,
153 otherPathProps = _objectWithoutProperties(_svgPropertiesAndEven, _excluded);
154 return /*#__PURE__*/React.createElement("path", _extends({}, otherPathProps, {
155 x: (0, _round.round)(x),
156 y: (0, _round.round)(y),
157 width: (0, _round.round)(width),
158 height: (0, _round.round)(height),
159 radius: typeof radius === 'number' ? radius : undefined,
160 className: layerClass,
161 d: getRectanglePath(x, y, width, height, radius)
162 }));
163 }
164 var prevWidth = prevWidthRef.current;
165 var prevHeight = prevHeightRef.current;
166 var prevX = prevXRef.current;
167 var prevY = prevYRef.current;
168 var from = "0px ".concat(totalLength === -1 ? 1 : totalLength, "px");
169 var to = "".concat(totalLength, "px 0px");
170 var transition = (0, _util.getTransitionVal)(['strokeDasharray'], animationDuration, typeof animationEasing === 'string' ? animationEasing : defaultRectangleProps.animationEasing);
171 return /*#__PURE__*/React.createElement(_JavascriptAnimate.JavascriptAnimate, {
172 animationId: animationId,
173 key: animationId,
174 canBegin: totalLength > 0,
175 duration: animationDuration,
176 easing: animationEasing,
177 isActive: isUpdateAnimationActive,
178 begin: animationBegin
179 }, t => {
180 var currWidth = (0, _DataUtils.interpolate)(prevWidth, width, t);
181 var currHeight = (0, _DataUtils.interpolate)(prevHeight, height, t);
182 var currX = (0, _DataUtils.interpolate)(prevX, x, t);
183 var currY = (0, _DataUtils.interpolate)(prevY, y, t);
184 if (pathRef.current) {
185 prevWidthRef.current = currWidth;
186 prevHeightRef.current = currHeight;
187 prevXRef.current = currX;
188 prevYRef.current = currY;
189 }
190 var animationStyle;
191 if (!isAnimationActive) {
192 animationStyle = {
193 strokeDasharray: to
194 };
195 } else if (t > 0) {
196 animationStyle = {
197 transition,
198 strokeDasharray: to
199 };
200 } else {
201 animationStyle = {
202 strokeDasharray: from
203 };
204 }
205 var _svgPropertiesAndEven2 = (0, _svgPropertiesAndEvents.svgPropertiesAndEvents)(props),
206 {
207 radius: _
208 } = _svgPropertiesAndEven2,
209 otherPathProps = _objectWithoutProperties(_svgPropertiesAndEven2, _excluded2);
210 return /*#__PURE__*/React.createElement("path", _extends({}, otherPathProps, {
211 radius: typeof radius === 'number' ? radius : undefined,
212 className: layerClass,
213 d: getRectanglePath(currX, currY, currWidth, currHeight, radius),
214 ref: pathRef,
215 style: _objectSpread(_objectSpread({}, animationStyle), props.style)
216 }));
217 });
218};
219exports.Rectangle = Rectangle;
Note: See TracBrowser for help on using the repository browser.