Polygon.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Polygon = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _clsx = _interopRequireDefault(require("clsx"));
  8. var _ReactUtils = require("../util/ReactUtils");
  9. var _excluded = ["points", "className", "baseLinePoints", "connectNulls"];
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  11. function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  12. function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  13. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } } return target; }
  14. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  15. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  16. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  17. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  18. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  19. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /**
  20. * @fileOverview Polygon
  21. */
  22. var isValidatePoint = function isValidatePoint(point) {
  23. return point && point.x === +point.x && point.y === +point.y;
  24. };
  25. var getParsedPoints = function getParsedPoints() {
  26. var points = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  27. var segmentPoints = [[]];
  28. points.forEach(function (entry) {
  29. if (isValidatePoint(entry)) {
  30. segmentPoints[segmentPoints.length - 1].push(entry);
  31. } else if (segmentPoints[segmentPoints.length - 1].length > 0) {
  32. // add another path
  33. segmentPoints.push([]);
  34. }
  35. });
  36. if (isValidatePoint(points[0])) {
  37. segmentPoints[segmentPoints.length - 1].push(points[0]);
  38. }
  39. if (segmentPoints[segmentPoints.length - 1].length <= 0) {
  40. segmentPoints = segmentPoints.slice(0, -1);
  41. }
  42. return segmentPoints;
  43. };
  44. var getSinglePolygonPath = function getSinglePolygonPath(points, connectNulls) {
  45. var segmentPoints = getParsedPoints(points);
  46. if (connectNulls) {
  47. segmentPoints = [segmentPoints.reduce(function (res, segPoints) {
  48. return [].concat(_toConsumableArray(res), _toConsumableArray(segPoints));
  49. }, [])];
  50. }
  51. var polygonPath = segmentPoints.map(function (segPoints) {
  52. return segPoints.reduce(function (path, point, index) {
  53. return "".concat(path).concat(index === 0 ? 'M' : 'L').concat(point.x, ",").concat(point.y);
  54. }, '');
  55. }).join('');
  56. return segmentPoints.length === 1 ? "".concat(polygonPath, "Z") : polygonPath;
  57. };
  58. var getRanglePath = function getRanglePath(points, baseLinePoints, connectNulls) {
  59. var outerPath = getSinglePolygonPath(points, connectNulls);
  60. return "".concat(outerPath.slice(-1) === 'Z' ? outerPath.slice(0, -1) : outerPath, "L").concat(getSinglePolygonPath(baseLinePoints.reverse(), connectNulls).slice(1));
  61. };
  62. var Polygon = exports.Polygon = function Polygon(props) {
  63. var points = props.points,
  64. className = props.className,
  65. baseLinePoints = props.baseLinePoints,
  66. connectNulls = props.connectNulls,
  67. others = _objectWithoutProperties(props, _excluded);
  68. if (!points || !points.length) {
  69. return null;
  70. }
  71. var layerClass = (0, _clsx["default"])('recharts-polygon', className);
  72. if (baseLinePoints && baseLinePoints.length) {
  73. var hasStroke = others.stroke && others.stroke !== 'none';
  74. var rangePath = getRanglePath(points, baseLinePoints, connectNulls);
  75. return /*#__PURE__*/_react["default"].createElement("g", {
  76. className: layerClass
  77. }, /*#__PURE__*/_react["default"].createElement("path", _extends({}, (0, _ReactUtils.filterProps)(others, true), {
  78. fill: rangePath.slice(-1) === 'Z' ? others.fill : 'none',
  79. stroke: "none",
  80. d: rangePath
  81. })), hasStroke ? /*#__PURE__*/_react["default"].createElement("path", _extends({}, (0, _ReactUtils.filterProps)(others, true), {
  82. fill: "none",
  83. d: getSinglePolygonPath(points, connectNulls)
  84. })) : null, hasStroke ? /*#__PURE__*/_react["default"].createElement("path", _extends({}, (0, _ReactUtils.filterProps)(others, true), {
  85. fill: "none",
  86. d: getSinglePolygonPath(baseLinePoints, connectNulls)
  87. })) : null);
  88. }
  89. var singlePath = getSinglePolygonPath(points, connectNulls);
  90. return /*#__PURE__*/_react["default"].createElement("path", _extends({}, (0, _ReactUtils.filterProps)(others, true), {
  91. fill: singlePath.slice(-1) === 'Z' ? others.fill : 'none',
  92. className: layerClass,
  93. d: singlePath
  94. }));
  95. };