Legend.js 11 KB

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