DefaultLegendContent.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.DefaultLegendContent = void 0;
  6. var _react = _interopRequireWildcard(require("react"));
  7. var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
  8. var _clsx2 = _interopRequireDefault(require("clsx"));
  9. var _LogUtils = require("../util/LogUtils");
  10. var _Surface = require("../container/Surface");
  11. var _Symbols = require("../shape/Symbols");
  12. var _types = require("../util/types");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  14. 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); }
  15. 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; }
  16. 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); }
  17. 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); }
  18. 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; }
  19. 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; }
  20. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  21. 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); } }
  22. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  23. function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
  24. 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); }
  25. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  26. function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
  27. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  28. 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); }
  29. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  30. 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; }
  31. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  32. 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); } /**
  33. * @fileOverview Default Legend Content
  34. */
  35. var SIZE = 32;
  36. var DefaultLegendContent = exports.DefaultLegendContent = /*#__PURE__*/function (_PureComponent) {
  37. function DefaultLegendContent() {
  38. _classCallCheck(this, DefaultLegendContent);
  39. return _callSuper(this, DefaultLegendContent, arguments);
  40. }
  41. _inherits(DefaultLegendContent, _PureComponent);
  42. return _createClass(DefaultLegendContent, [{
  43. key: "renderIcon",
  44. value:
  45. /**
  46. * Render the path of icon
  47. * @param {Object} data Data of each legend item
  48. * @return {String} Path element
  49. */
  50. function renderIcon(data) {
  51. var inactiveColor = this.props.inactiveColor;
  52. var halfSize = SIZE / 2;
  53. var sixthSize = SIZE / 6;
  54. var thirdSize = SIZE / 3;
  55. var color = data.inactive ? inactiveColor : data.color;
  56. if (data.type === 'plainline') {
  57. return /*#__PURE__*/_react["default"].createElement("line", {
  58. strokeWidth: 4,
  59. fill: "none",
  60. stroke: color,
  61. strokeDasharray: data.payload.strokeDasharray,
  62. x1: 0,
  63. y1: halfSize,
  64. x2: SIZE,
  65. y2: halfSize,
  66. className: "recharts-legend-icon"
  67. });
  68. }
  69. if (data.type === 'line') {
  70. return /*#__PURE__*/_react["default"].createElement("path", {
  71. strokeWidth: 4,
  72. fill: "none",
  73. stroke: color,
  74. d: "M0,".concat(halfSize, "h").concat(thirdSize, "\n A").concat(sixthSize, ",").concat(sixthSize, ",0,1,1,").concat(2 * thirdSize, ",").concat(halfSize, "\n H").concat(SIZE, "M").concat(2 * thirdSize, ",").concat(halfSize, "\n A").concat(sixthSize, ",").concat(sixthSize, ",0,1,1,").concat(thirdSize, ",").concat(halfSize),
  75. className: "recharts-legend-icon"
  76. });
  77. }
  78. if (data.type === 'rect') {
  79. return /*#__PURE__*/_react["default"].createElement("path", {
  80. stroke: "none",
  81. fill: color,
  82. d: "M0,".concat(SIZE / 8, "h").concat(SIZE, "v").concat(SIZE * 3 / 4, "h").concat(-SIZE, "z"),
  83. className: "recharts-legend-icon"
  84. });
  85. }
  86. if ( /*#__PURE__*/_react["default"].isValidElement(data.legendIcon)) {
  87. var iconProps = _objectSpread({}, data);
  88. delete iconProps.legendIcon;
  89. return /*#__PURE__*/_react["default"].cloneElement(data.legendIcon, iconProps);
  90. }
  91. return /*#__PURE__*/_react["default"].createElement(_Symbols.Symbols, {
  92. fill: color,
  93. cx: halfSize,
  94. cy: halfSize,
  95. size: SIZE,
  96. sizeType: "diameter",
  97. type: data.type
  98. });
  99. }
  100. /**
  101. * Draw items of legend
  102. * @return {ReactElement} Items
  103. */
  104. }, {
  105. key: "renderItems",
  106. value: function renderItems() {
  107. var _this = this;
  108. var _this$props = this.props,
  109. payload = _this$props.payload,
  110. iconSize = _this$props.iconSize,
  111. layout = _this$props.layout,
  112. formatter = _this$props.formatter,
  113. inactiveColor = _this$props.inactiveColor;
  114. var viewBox = {
  115. x: 0,
  116. y: 0,
  117. width: SIZE,
  118. height: SIZE
  119. };
  120. var itemStyle = {
  121. display: layout === 'horizontal' ? 'inline-block' : 'block',
  122. marginRight: 10
  123. };
  124. var svgStyle = {
  125. display: 'inline-block',
  126. verticalAlign: 'middle',
  127. marginRight: 4
  128. };
  129. return payload.map(function (entry, i) {
  130. var finalFormatter = entry.formatter || formatter;
  131. var className = (0, _clsx2["default"])(_defineProperty(_defineProperty({
  132. 'recharts-legend-item': true
  133. }, "legend-item-".concat(i), true), "inactive", entry.inactive));
  134. if (entry.type === 'none') {
  135. return null;
  136. }
  137. // Do not render entry.value as functions. Always require static string properties.
  138. var entryValue = !(0, _isFunction["default"])(entry.value) ? entry.value : null;
  139. (0, _LogUtils.warn)(!(0, _isFunction["default"])(entry.value), "The name property is also required when using a function for the dataKey of a chart's cartesian components. Ex: <Bar name=\"Name of my Data\"/>" // eslint-disable-line max-len
  140. );
  141. var color = entry.inactive ? inactiveColor : entry.color;
  142. return /*#__PURE__*/_react["default"].createElement("li", _extends({
  143. className: className,
  144. style: itemStyle
  145. // eslint-disable-next-line react/no-array-index-key
  146. ,
  147. key: "legend-item-".concat(i)
  148. }, (0, _types.adaptEventsOfChild)(_this.props, entry, i)), /*#__PURE__*/_react["default"].createElement(_Surface.Surface, {
  149. width: iconSize,
  150. height: iconSize,
  151. viewBox: viewBox,
  152. style: svgStyle
  153. }, _this.renderIcon(entry)), /*#__PURE__*/_react["default"].createElement("span", {
  154. className: "recharts-legend-item-text",
  155. style: {
  156. color: color
  157. }
  158. }, finalFormatter ? finalFormatter(entryValue, entry, i) : entryValue));
  159. });
  160. }
  161. }, {
  162. key: "render",
  163. value: function render() {
  164. var _this$props2 = this.props,
  165. payload = _this$props2.payload,
  166. layout = _this$props2.layout,
  167. align = _this$props2.align;
  168. if (!payload || !payload.length) {
  169. return null;
  170. }
  171. var finalStyle = {
  172. padding: 0,
  173. margin: 0,
  174. textAlign: layout === 'horizontal' ? align : 'left'
  175. };
  176. return /*#__PURE__*/_react["default"].createElement("ul", {
  177. className: "recharts-default-legend",
  178. style: finalStyle
  179. }, this.renderItems());
  180. }
  181. }]);
  182. }(_react.PureComponent);
  183. _defineProperty(DefaultLegendContent, "displayName", 'Legend');
  184. _defineProperty(DefaultLegendContent, "defaultProps", {
  185. iconSize: 14,
  186. layout: 'horizontal',
  187. align: 'center',
  188. verticalAlign: 'middle',
  189. inactiveColor: '#ccc'
  190. });