recharts
Version:
React charts
148 lines • 6.09 kB
JavaScript
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); }
/**
* @fileOverview Rectangle
*/
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';
import { clsx } from 'clsx';
import { filterProps } from '../util/ReactUtils';
import { resolveDefaultProps } from '../util/resolveDefaultProps';
import { Animate } from '../animation/Animate';
var getRectanglePath = (x, y, width, height, radius) => {
var maxRadius = Math.min(Math.abs(width) / 2, Math.abs(height) / 2);
var ySign = height >= 0 ? 1 : -1;
var xSign = width >= 0 ? 1 : -1;
var clockWise = height >= 0 && width >= 0 || height < 0 && width < 0 ? 1 : 0;
var path;
if (maxRadius > 0 && radius instanceof Array) {
var newRadius = [0, 0, 0, 0];
for (var i = 0, len = 4; i < len; i++) {
newRadius[i] = radius[i] > maxRadius ? maxRadius : radius[i];
}
path = "M".concat(x, ",").concat(y + ySign * newRadius[0]);
if (newRadius[0] > 0) {
path += "A ".concat(newRadius[0], ",").concat(newRadius[0], ",0,0,").concat(clockWise, ",").concat(x + xSign * newRadius[0], ",").concat(y);
}
path += "L ".concat(x + width - xSign * newRadius[1], ",").concat(y);
if (newRadius[1] > 0) {
path += "A ".concat(newRadius[1], ",").concat(newRadius[1], ",0,0,").concat(clockWise, ",\n ").concat(x + width, ",").concat(y + ySign * newRadius[1]);
}
path += "L ".concat(x + width, ",").concat(y + height - ySign * newRadius[2]);
if (newRadius[2] > 0) {
path += "A ".concat(newRadius[2], ",").concat(newRadius[2], ",0,0,").concat(clockWise, ",\n ").concat(x + width - xSign * newRadius[2], ",").concat(y + height);
}
path += "L ".concat(x + xSign * newRadius[3], ",").concat(y + height);
if (newRadius[3] > 0) {
path += "A ".concat(newRadius[3], ",").concat(newRadius[3], ",0,0,").concat(clockWise, ",\n ").concat(x, ",").concat(y + height - ySign * newRadius[3]);
}
path += 'Z';
} else if (maxRadius > 0 && radius === +radius && radius > 0) {
var _newRadius = Math.min(maxRadius, radius);
path = "M ".concat(x, ",").concat(y + ySign * _newRadius, "\n A ").concat(_newRadius, ",").concat(_newRadius, ",0,0,").concat(clockWise, ",").concat(x + xSign * _newRadius, ",").concat(y, "\n L ").concat(x + width - xSign * _newRadius, ",").concat(y, "\n A ").concat(_newRadius, ",").concat(_newRadius, ",0,0,").concat(clockWise, ",").concat(x + width, ",").concat(y + ySign * _newRadius, "\n L ").concat(x + width, ",").concat(y + height - ySign * _newRadius, "\n A ").concat(_newRadius, ",").concat(_newRadius, ",0,0,").concat(clockWise, ",").concat(x + width - xSign * _newRadius, ",").concat(y + height, "\n L ").concat(x + xSign * _newRadius, ",").concat(y + height, "\n A ").concat(_newRadius, ",").concat(_newRadius, ",0,0,").concat(clockWise, ",").concat(x, ",").concat(y + height - ySign * _newRadius, " Z");
} else {
path = "M ".concat(x, ",").concat(y, " h ").concat(width, " v ").concat(height, " h ").concat(-width, " Z");
}
return path;
};
var defaultProps = {
x: 0,
y: 0,
width: 0,
height: 0,
// The radius of border
// The radius of four corners when radius is a number
// The radius of left-top, right-top, right-bottom, left-bottom when radius is an array
radius: 0,
isAnimationActive: false,
isUpdateAnimationActive: false,
animationBegin: 0,
animationDuration: 1500,
animationEasing: 'ease'
};
export var Rectangle = rectangleProps => {
var props = resolveDefaultProps(rectangleProps, defaultProps);
var pathRef = useRef(null);
var [totalLength, setTotalLength] = useState(-1);
useEffect(() => {
if (pathRef.current && pathRef.current.getTotalLength) {
try {
var pathTotalLength = pathRef.current.getTotalLength();
if (pathTotalLength) {
setTotalLength(pathTotalLength);
}
} catch (_unused) {
// calculate total length error
}
}
}, []);
var {
x,
y,
width,
height,
radius,
className
} = props;
var {
animationEasing,
animationDuration,
animationBegin,
isAnimationActive,
isUpdateAnimationActive
} = props;
if (x !== +x || y !== +y || width !== +width || height !== +height || width === 0 || height === 0) {
return null;
}
var layerClass = clsx('recharts-rectangle', className);
if (!isUpdateAnimationActive) {
return /*#__PURE__*/React.createElement("path", _extends({}, filterProps(props, true), {
className: layerClass,
d: getRectanglePath(x, y, width, height, radius)
}));
}
return /*#__PURE__*/React.createElement(Animate, {
canBegin: totalLength > 0,
from: {
width,
height,
x,
y
},
to: {
width,
height,
x,
y
},
duration: animationDuration
// @ts-expect-error TODO - fix the type error
,
animationEasing: animationEasing,
isActive: isUpdateAnimationActive
}, _ref => {
var {
width: currWidth,
height: currHeight,
x: currX,
y: currY
} = _ref;
return /*#__PURE__*/React.createElement(Animate, {
canBegin: totalLength > 0
// @ts-expect-error TODO - fix the type error
,
from: "0px ".concat(totalLength === -1 ? 1 : totalLength, "px")
// @ts-expect-error TODO - fix the type error
,
to: "".concat(totalLength, "px 0px"),
attributeName: "strokeDasharray",
begin: animationBegin,
duration: animationDuration,
isActive: isAnimationActive,
easing: animationEasing
}, /*#__PURE__*/React.createElement("path", _extends({}, filterProps(props, true), {
className: layerClass,
d: getRectanglePath(currX, currY, currWidth, currHeight, radius),
ref: pathRef
})));
});
};