DefaultLegendContent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 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); }
  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. /**
  19. * @fileOverview Default Legend Content
  20. */
  21. import React, { PureComponent } from 'react';
  22. import isFunction from 'lodash/isFunction';
  23. import clsx from 'clsx';
  24. import { warn } from '../util/LogUtils';
  25. import { Surface } from '../container/Surface';
  26. import { Symbols } from '../shape/Symbols';
  27. import { adaptEventsOfChild } from '../util/types';
  28. var SIZE = 32;
  29. export var DefaultLegendContent = /*#__PURE__*/function (_PureComponent) {
  30. function DefaultLegendContent() {
  31. _classCallCheck(this, DefaultLegendContent);
  32. return _callSuper(this, DefaultLegendContent, arguments);
  33. }
  34. _inherits(DefaultLegendContent, _PureComponent);
  35. return _createClass(DefaultLegendContent, [{
  36. key: "renderIcon",
  37. value:
  38. /**
  39. * Render the path of icon
  40. * @param {Object} data Data of each legend item
  41. * @return {String} Path element
  42. */
  43. function renderIcon(data) {
  44. var inactiveColor = this.props.inactiveColor;
  45. var halfSize = SIZE / 2;
  46. var sixthSize = SIZE / 6;
  47. var thirdSize = SIZE / 3;
  48. var color = data.inactive ? inactiveColor : data.color;
  49. if (data.type === 'plainline') {
  50. return /*#__PURE__*/React.createElement("line", {
  51. strokeWidth: 4,
  52. fill: "none",
  53. stroke: color,
  54. strokeDasharray: data.payload.strokeDasharray,
  55. x1: 0,
  56. y1: halfSize,
  57. x2: SIZE,
  58. y2: halfSize,
  59. className: "recharts-legend-icon"
  60. });
  61. }
  62. if (data.type === 'line') {
  63. return /*#__PURE__*/React.createElement("path", {
  64. strokeWidth: 4,
  65. fill: "none",
  66. stroke: color,
  67. 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),
  68. className: "recharts-legend-icon"
  69. });
  70. }
  71. if (data.type === 'rect') {
  72. return /*#__PURE__*/React.createElement("path", {
  73. stroke: "none",
  74. fill: color,
  75. d: "M0,".concat(SIZE / 8, "h").concat(SIZE, "v").concat(SIZE * 3 / 4, "h").concat(-SIZE, "z"),
  76. className: "recharts-legend-icon"
  77. });
  78. }
  79. if ( /*#__PURE__*/React.isValidElement(data.legendIcon)) {
  80. var iconProps = _objectSpread({}, data);
  81. delete iconProps.legendIcon;
  82. return /*#__PURE__*/React.cloneElement(data.legendIcon, iconProps);
  83. }
  84. return /*#__PURE__*/React.createElement(Symbols, {
  85. fill: color,
  86. cx: halfSize,
  87. cy: halfSize,
  88. size: SIZE,
  89. sizeType: "diameter",
  90. type: data.type
  91. });
  92. }
  93. /**
  94. * Draw items of legend
  95. * @return {ReactElement} Items
  96. */
  97. }, {
  98. key: "renderItems",
  99. value: function renderItems() {
  100. var _this = this;
  101. var _this$props = this.props,
  102. payload = _this$props.payload,
  103. iconSize = _this$props.iconSize,
  104. layout = _this$props.layout,
  105. formatter = _this$props.formatter,
  106. inactiveColor = _this$props.inactiveColor;
  107. var viewBox = {
  108. x: 0,
  109. y: 0,
  110. width: SIZE,
  111. height: SIZE
  112. };
  113. var itemStyle = {
  114. display: layout === 'horizontal' ? 'inline-block' : 'block',
  115. marginRight: 10
  116. };
  117. var svgStyle = {
  118. display: 'inline-block',
  119. verticalAlign: 'middle',
  120. marginRight: 4
  121. };
  122. return payload.map(function (entry, i) {
  123. var finalFormatter = entry.formatter || formatter;
  124. var className = clsx(_defineProperty(_defineProperty({
  125. 'recharts-legend-item': true
  126. }, "legend-item-".concat(i), true), "inactive", entry.inactive));
  127. if (entry.type === 'none') {
  128. return null;
  129. }
  130. // Do not render entry.value as functions. Always require static string properties.
  131. var entryValue = !isFunction(entry.value) ? entry.value : null;
  132. warn(!isFunction(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
  133. );
  134. var color = entry.inactive ? inactiveColor : entry.color;
  135. return /*#__PURE__*/React.createElement("li", _extends({
  136. className: className,
  137. style: itemStyle
  138. // eslint-disable-next-line react/no-array-index-key
  139. ,
  140. key: "legend-item-".concat(i)
  141. }, adaptEventsOfChild(_this.props, entry, i)), /*#__PURE__*/React.createElement(Surface, {
  142. width: iconSize,
  143. height: iconSize,
  144. viewBox: viewBox,
  145. style: svgStyle
  146. }, _this.renderIcon(entry)), /*#__PURE__*/React.createElement("span", {
  147. className: "recharts-legend-item-text",
  148. style: {
  149. color: color
  150. }
  151. }, finalFormatter ? finalFormatter(entryValue, entry, i) : entryValue));
  152. });
  153. }
  154. }, {
  155. key: "render",
  156. value: function render() {
  157. var _this$props2 = this.props,
  158. payload = _this$props2.payload,
  159. layout = _this$props2.layout,
  160. align = _this$props2.align;
  161. if (!payload || !payload.length) {
  162. return null;
  163. }
  164. var finalStyle = {
  165. padding: 0,
  166. margin: 0,
  167. textAlign: layout === 'horizontal' ? align : 'left'
  168. };
  169. return /*#__PURE__*/React.createElement("ul", {
  170. className: "recharts-default-legend",
  171. style: finalStyle
  172. }, this.renderItems());
  173. }
  174. }]);
  175. }(PureComponent);
  176. _defineProperty(DefaultLegendContent, "displayName", 'Legend');
  177. _defineProperty(DefaultLegendContent, "defaultProps", {
  178. iconSize: 14,
  179. layout: 'horizontal',
  180. align: 'center',
  181. verticalAlign: 'middle',
  182. inactiveColor: '#ccc'
  183. });