Legend.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.Legend = void 0;
  7. var _react = _interopRequireWildcard(require("react"));
  8. var _DefaultLegendContent = require("./DefaultLegendContent");
  9. var _DataUtils = require("../util/DataUtils");
  10. var _getUniqPayload = require("../util/payload/getUniqPayload");
  11. var _excluded = ["ref"];
  12. /**
  13. * @fileOverview Legend
  14. */
  15. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  16. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
  17. 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; }
  18. 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; }
  19. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  20. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  21. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  22. function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
  23. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  24. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  25. function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
  26. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  27. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  28. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  29. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  30. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  31. 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); }
  32. 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; }
  33. 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; }
  34. function defaultUniqBy(entry) {
  35. return entry.value;
  36. }
  37. function renderContent(content, props) {
  38. if ( /*#__PURE__*/_react["default"].isValidElement(content)) {
  39. return /*#__PURE__*/_react["default"].cloneElement(content, props);
  40. }
  41. if (typeof content === 'function') {
  42. return /*#__PURE__*/_react["default"].createElement(content, props);
  43. }
  44. var ref = props.ref,
  45. otherProps = _objectWithoutProperties(props, _excluded);
  46. return /*#__PURE__*/_react["default"].createElement(_DefaultLegendContent.DefaultLegendContent, otherProps);
  47. }
  48. var EPS = 1;
  49. var Legend = exports.Legend = /*#__PURE__*/function (_PureComponent) {
  50. function Legend() {
  51. var _this;
  52. _classCallCheck(this, Legend);
  53. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  54. args[_key] = arguments[_key];
  55. }
  56. _this = _callSuper(this, Legend, [].concat(args));
  57. _defineProperty(_this, "lastBoundingBox", {
  58. width: -1,
  59. height: -1
  60. });
  61. return _this;
  62. }
  63. _inherits(Legend, _PureComponent);
  64. return _createClass(Legend, [{
  65. key: "componentDidMount",
  66. value: function componentDidMount() {
  67. this.updateBBox();
  68. }
  69. }, {
  70. key: "componentDidUpdate",
  71. value: function componentDidUpdate() {
  72. this.updateBBox();
  73. }
  74. }, {
  75. key: "getBBox",
  76. value: function getBBox() {
  77. if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) {
  78. var box = this.wrapperNode.getBoundingClientRect();
  79. box.height = this.wrapperNode.offsetHeight;
  80. box.width = this.wrapperNode.offsetWidth;
  81. return box;
  82. }
  83. return null;
  84. }
  85. }, {
  86. key: "updateBBox",
  87. value: function updateBBox() {
  88. var onBBoxUpdate = this.props.onBBoxUpdate;
  89. var box = this.getBBox();
  90. if (box) {
  91. if (Math.abs(box.width - this.lastBoundingBox.width) > EPS || Math.abs(box.height - this.lastBoundingBox.height) > EPS) {
  92. this.lastBoundingBox.width = box.width;
  93. this.lastBoundingBox.height = box.height;
  94. if (onBBoxUpdate) {
  95. onBBoxUpdate(box);
  96. }
  97. }
  98. } else if (this.lastBoundingBox.width !== -1 || this.lastBoundingBox.height !== -1) {
  99. this.lastBoundingBox.width = -1;
  100. this.lastBoundingBox.height = -1;
  101. if (onBBoxUpdate) {
  102. onBBoxUpdate(null);
  103. }
  104. }
  105. }
  106. }, {
  107. key: "getBBoxSnapshot",
  108. value: function getBBoxSnapshot() {
  109. if (this.lastBoundingBox.width >= 0 && this.lastBoundingBox.height >= 0) {
  110. return _objectSpread({}, this.lastBoundingBox);
  111. }
  112. return {
  113. width: 0,
  114. height: 0
  115. };
  116. }
  117. }, {
  118. key: "getDefaultPosition",
  119. value: function getDefaultPosition(style) {
  120. var _this$props = this.props,
  121. layout = _this$props.layout,
  122. align = _this$props.align,
  123. verticalAlign = _this$props.verticalAlign,
  124. margin = _this$props.margin,
  125. chartWidth = _this$props.chartWidth,
  126. chartHeight = _this$props.chartHeight;
  127. var hPos, vPos;
  128. if (!style || (style.left === undefined || style.left === null) && (style.right === undefined || style.right === null)) {
  129. if (align === 'center' && layout === 'vertical') {
  130. var box = this.getBBoxSnapshot();
  131. hPos = {
  132. left: ((chartWidth || 0) - box.width) / 2
  133. };
  134. } else {
  135. hPos = align === 'right' ? {
  136. right: margin && margin.right || 0
  137. } : {
  138. left: margin && margin.left || 0
  139. };
  140. }
  141. }
  142. if (!style || (style.top === undefined || style.top === null) && (style.bottom === undefined || style.bottom === null)) {
  143. if (verticalAlign === 'middle') {
  144. var _box = this.getBBoxSnapshot();
  145. vPos = {
  146. top: ((chartHeight || 0) - _box.height) / 2
  147. };
  148. } else {
  149. vPos = verticalAlign === 'bottom' ? {
  150. bottom: margin && margin.bottom || 0
  151. } : {
  152. top: margin && margin.top || 0
  153. };
  154. }
  155. }
  156. return _objectSpread(_objectSpread({}, hPos), vPos);
  157. }
  158. }, {
  159. key: "render",
  160. value: function render() {
  161. var _this2 = this;
  162. var _this$props2 = this.props,
  163. content = _this$props2.content,
  164. width = _this$props2.width,
  165. height = _this$props2.height,
  166. wrapperStyle = _this$props2.wrapperStyle,
  167. payloadUniqBy = _this$props2.payloadUniqBy,
  168. payload = _this$props2.payload;
  169. var outerStyle = _objectSpread(_objectSpread({
  170. position: 'absolute',
  171. width: width || 'auto',
  172. height: height || 'auto'
  173. }, this.getDefaultPosition(wrapperStyle)), wrapperStyle);
  174. return /*#__PURE__*/_react["default"].createElement("div", {
  175. className: "recharts-legend-wrapper",
  176. style: outerStyle,
  177. ref: function ref(node) {
  178. _this2.wrapperNode = node;
  179. }
  180. }, renderContent(content, _objectSpread(_objectSpread({}, this.props), {}, {
  181. payload: (0, _getUniqPayload.getUniqPayload)(payload, payloadUniqBy, defaultUniqBy)
  182. })));
  183. }
  184. }], [{
  185. key: "getWithHeight",
  186. value: function getWithHeight(item, chartWidth) {
  187. var _this$defaultProps$it = _objectSpread(_objectSpread({}, this.defaultProps), item.props),
  188. layout = _this$defaultProps$it.layout;
  189. if (layout === 'vertical' && (0, _DataUtils.isNumber)(item.props.height)) {
  190. return {
  191. height: item.props.height
  192. };
  193. }
  194. if (layout === 'horizontal') {
  195. return {
  196. width: item.props.width || chartWidth
  197. };
  198. }
  199. return null;
  200. }
  201. }]);
  202. }(_react.PureComponent);
  203. _defineProperty(Legend, "displayName", 'Legend');
  204. _defineProperty(Legend, "defaultProps", {
  205. iconSize: 14,
  206. layout: 'horizontal',
  207. align: 'center',
  208. verticalAlign: 'bottom'
  209. });