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