Label.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 = ["offset"];
  3. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  4. 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."); }
  5. 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); }
  6. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  7. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  8. 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; }
  9. 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; }
  10. 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; }
  11. 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; }
  12. 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; }
  13. 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; }
  14. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  15. 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); }
  16. 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); }
  17. import React, { cloneElement, isValidElement, createElement } from 'react';
  18. import isNil from 'lodash/isNil';
  19. import isFunction from 'lodash/isFunction';
  20. import isObject from 'lodash/isObject';
  21. import clsx from 'clsx';
  22. import { Text } from './Text';
  23. import { findAllByType, filterProps } from '../util/ReactUtils';
  24. import { isNumOrStr, isNumber, isPercent, getPercentValue, uniqueId, mathSign } from '../util/DataUtils';
  25. import { polarToCartesian } from '../util/PolarUtils';
  26. var getLabel = function getLabel(props) {
  27. var value = props.value,
  28. formatter = props.formatter;
  29. var label = isNil(props.children) ? value : props.children;
  30. if (isFunction(formatter)) {
  31. return formatter(label);
  32. }
  33. return label;
  34. };
  35. var getDeltaAngle = function getDeltaAngle(startAngle, endAngle) {
  36. var sign = mathSign(endAngle - startAngle);
  37. var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360);
  38. return sign * deltaAngle;
  39. };
  40. var renderRadialLabel = function renderRadialLabel(labelProps, label, attrs) {
  41. var position = labelProps.position,
  42. viewBox = labelProps.viewBox,
  43. offset = labelProps.offset,
  44. className = labelProps.className;
  45. var _ref = viewBox,
  46. cx = _ref.cx,
  47. cy = _ref.cy,
  48. innerRadius = _ref.innerRadius,
  49. outerRadius = _ref.outerRadius,
  50. startAngle = _ref.startAngle,
  51. endAngle = _ref.endAngle,
  52. clockWise = _ref.clockWise;
  53. var radius = (innerRadius + outerRadius) / 2;
  54. var deltaAngle = getDeltaAngle(startAngle, endAngle);
  55. var sign = deltaAngle >= 0 ? 1 : -1;
  56. var labelAngle, direction;
  57. if (position === 'insideStart') {
  58. labelAngle = startAngle + sign * offset;
  59. direction = clockWise;
  60. } else if (position === 'insideEnd') {
  61. labelAngle = endAngle - sign * offset;
  62. direction = !clockWise;
  63. } else if (position === 'end') {
  64. labelAngle = endAngle + sign * offset;
  65. direction = clockWise;
  66. }
  67. direction = deltaAngle <= 0 ? direction : !direction;
  68. var startPoint = polarToCartesian(cx, cy, radius, labelAngle);
  69. var endPoint = polarToCartesian(cx, cy, radius, labelAngle + (direction ? 1 : -1) * 359);
  70. var path = "M".concat(startPoint.x, ",").concat(startPoint.y, "\n A").concat(radius, ",").concat(radius, ",0,1,").concat(direction ? 0 : 1, ",\n ").concat(endPoint.x, ",").concat(endPoint.y);
  71. var id = isNil(labelProps.id) ? uniqueId('recharts-radial-line-') : labelProps.id;
  72. return /*#__PURE__*/React.createElement("text", _extends({}, attrs, {
  73. dominantBaseline: "central",
  74. className: clsx('recharts-radial-bar-label', className)
  75. }), /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("path", {
  76. id: id,
  77. d: path
  78. })), /*#__PURE__*/React.createElement("textPath", {
  79. xlinkHref: "#".concat(id)
  80. }, label));
  81. };
  82. var getAttrsOfPolarLabel = function getAttrsOfPolarLabel(props) {
  83. var viewBox = props.viewBox,
  84. offset = props.offset,
  85. position = props.position;
  86. var _ref2 = viewBox,
  87. cx = _ref2.cx,
  88. cy = _ref2.cy,
  89. innerRadius = _ref2.innerRadius,
  90. outerRadius = _ref2.outerRadius,
  91. startAngle = _ref2.startAngle,
  92. endAngle = _ref2.endAngle;
  93. var midAngle = (startAngle + endAngle) / 2;
  94. if (position === 'outside') {
  95. var _polarToCartesian = polarToCartesian(cx, cy, outerRadius + offset, midAngle),
  96. _x = _polarToCartesian.x,
  97. _y = _polarToCartesian.y;
  98. return {
  99. x: _x,
  100. y: _y,
  101. textAnchor: _x >= cx ? 'start' : 'end',
  102. verticalAnchor: 'middle'
  103. };
  104. }
  105. if (position === 'center') {
  106. return {
  107. x: cx,
  108. y: cy,
  109. textAnchor: 'middle',
  110. verticalAnchor: 'middle'
  111. };
  112. }
  113. if (position === 'centerTop') {
  114. return {
  115. x: cx,
  116. y: cy,
  117. textAnchor: 'middle',
  118. verticalAnchor: 'start'
  119. };
  120. }
  121. if (position === 'centerBottom') {
  122. return {
  123. x: cx,
  124. y: cy,
  125. textAnchor: 'middle',
  126. verticalAnchor: 'end'
  127. };
  128. }
  129. var r = (innerRadius + outerRadius) / 2;
  130. var _polarToCartesian2 = polarToCartesian(cx, cy, r, midAngle),
  131. x = _polarToCartesian2.x,
  132. y = _polarToCartesian2.y;
  133. return {
  134. x: x,
  135. y: y,
  136. textAnchor: 'middle',
  137. verticalAnchor: 'middle'
  138. };
  139. };
  140. var getAttrsOfCartesianLabel = function getAttrsOfCartesianLabel(props) {
  141. var viewBox = props.viewBox,
  142. parentViewBox = props.parentViewBox,
  143. offset = props.offset,
  144. position = props.position;
  145. var _ref3 = viewBox,
  146. x = _ref3.x,
  147. y = _ref3.y,
  148. width = _ref3.width,
  149. height = _ref3.height;
  150. // Define vertical offsets and position inverts based on the value being positive or negative
  151. var verticalSign = height >= 0 ? 1 : -1;
  152. var verticalOffset = verticalSign * offset;
  153. var verticalEnd = verticalSign > 0 ? 'end' : 'start';
  154. var verticalStart = verticalSign > 0 ? 'start' : 'end';
  155. // Define horizontal offsets and position inverts based on the value being positive or negative
  156. var horizontalSign = width >= 0 ? 1 : -1;
  157. var horizontalOffset = horizontalSign * offset;
  158. var horizontalEnd = horizontalSign > 0 ? 'end' : 'start';
  159. var horizontalStart = horizontalSign > 0 ? 'start' : 'end';
  160. if (position === 'top') {
  161. var attrs = {
  162. x: x + width / 2,
  163. y: y - verticalSign * offset,
  164. textAnchor: 'middle',
  165. verticalAnchor: verticalEnd
  166. };
  167. return _objectSpread(_objectSpread({}, attrs), parentViewBox ? {
  168. height: Math.max(y - parentViewBox.y, 0),
  169. width: width
  170. } : {});
  171. }
  172. if (position === 'bottom') {
  173. var _attrs = {
  174. x: x + width / 2,
  175. y: y + height + verticalOffset,
  176. textAnchor: 'middle',
  177. verticalAnchor: verticalStart
  178. };
  179. return _objectSpread(_objectSpread({}, _attrs), parentViewBox ? {
  180. height: Math.max(parentViewBox.y + parentViewBox.height - (y + height), 0),
  181. width: width
  182. } : {});
  183. }
  184. if (position === 'left') {
  185. var _attrs2 = {
  186. x: x - horizontalOffset,
  187. y: y + height / 2,
  188. textAnchor: horizontalEnd,
  189. verticalAnchor: 'middle'
  190. };
  191. return _objectSpread(_objectSpread({}, _attrs2), parentViewBox ? {
  192. width: Math.max(_attrs2.x - parentViewBox.x, 0),
  193. height: height
  194. } : {});
  195. }
  196. if (position === 'right') {
  197. var _attrs3 = {
  198. x: x + width + horizontalOffset,
  199. y: y + height / 2,
  200. textAnchor: horizontalStart,
  201. verticalAnchor: 'middle'
  202. };
  203. return _objectSpread(_objectSpread({}, _attrs3), parentViewBox ? {
  204. width: Math.max(parentViewBox.x + parentViewBox.width - _attrs3.x, 0),
  205. height: height
  206. } : {});
  207. }
  208. var sizeAttrs = parentViewBox ? {
  209. width: width,
  210. height: height
  211. } : {};
  212. if (position === 'insideLeft') {
  213. return _objectSpread({
  214. x: x + horizontalOffset,
  215. y: y + height / 2,
  216. textAnchor: horizontalStart,
  217. verticalAnchor: 'middle'
  218. }, sizeAttrs);
  219. }
  220. if (position === 'insideRight') {
  221. return _objectSpread({
  222. x: x + width - horizontalOffset,
  223. y: y + height / 2,
  224. textAnchor: horizontalEnd,
  225. verticalAnchor: 'middle'
  226. }, sizeAttrs);
  227. }
  228. if (position === 'insideTop') {
  229. return _objectSpread({
  230. x: x + width / 2,
  231. y: y + verticalOffset,
  232. textAnchor: 'middle',
  233. verticalAnchor: verticalStart
  234. }, sizeAttrs);
  235. }
  236. if (position === 'insideBottom') {
  237. return _objectSpread({
  238. x: x + width / 2,
  239. y: y + height - verticalOffset,
  240. textAnchor: 'middle',
  241. verticalAnchor: verticalEnd
  242. }, sizeAttrs);
  243. }
  244. if (position === 'insideTopLeft') {
  245. return _objectSpread({
  246. x: x + horizontalOffset,
  247. y: y + verticalOffset,
  248. textAnchor: horizontalStart,
  249. verticalAnchor: verticalStart
  250. }, sizeAttrs);
  251. }
  252. if (position === 'insideTopRight') {
  253. return _objectSpread({
  254. x: x + width - horizontalOffset,
  255. y: y + verticalOffset,
  256. textAnchor: horizontalEnd,
  257. verticalAnchor: verticalStart
  258. }, sizeAttrs);
  259. }
  260. if (position === 'insideBottomLeft') {
  261. return _objectSpread({
  262. x: x + horizontalOffset,
  263. y: y + height - verticalOffset,
  264. textAnchor: horizontalStart,
  265. verticalAnchor: verticalEnd
  266. }, sizeAttrs);
  267. }
  268. if (position === 'insideBottomRight') {
  269. return _objectSpread({
  270. x: x + width - horizontalOffset,
  271. y: y + height - verticalOffset,
  272. textAnchor: horizontalEnd,
  273. verticalAnchor: verticalEnd
  274. }, sizeAttrs);
  275. }
  276. if (isObject(position) && (isNumber(position.x) || isPercent(position.x)) && (isNumber(position.y) || isPercent(position.y))) {
  277. return _objectSpread({
  278. x: x + getPercentValue(position.x, width),
  279. y: y + getPercentValue(position.y, height),
  280. textAnchor: 'end',
  281. verticalAnchor: 'end'
  282. }, sizeAttrs);
  283. }
  284. return _objectSpread({
  285. x: x + width / 2,
  286. y: y + height / 2,
  287. textAnchor: 'middle',
  288. verticalAnchor: 'middle'
  289. }, sizeAttrs);
  290. };
  291. var isPolar = function isPolar(viewBox) {
  292. return 'cx' in viewBox && isNumber(viewBox.cx);
  293. };
  294. export function Label(_ref4) {
  295. var _ref4$offset = _ref4.offset,
  296. offset = _ref4$offset === void 0 ? 5 : _ref4$offset,
  297. restProps = _objectWithoutProperties(_ref4, _excluded);
  298. var props = _objectSpread({
  299. offset: offset
  300. }, restProps);
  301. var viewBox = props.viewBox,
  302. position = props.position,
  303. value = props.value,
  304. children = props.children,
  305. content = props.content,
  306. _props$className = props.className,
  307. className = _props$className === void 0 ? '' : _props$className,
  308. textBreakAll = props.textBreakAll;
  309. if (!viewBox || isNil(value) && isNil(children) && ! /*#__PURE__*/isValidElement(content) && !isFunction(content)) {
  310. return null;
  311. }
  312. if ( /*#__PURE__*/isValidElement(content)) {
  313. return /*#__PURE__*/cloneElement(content, props);
  314. }
  315. var label;
  316. if (isFunction(content)) {
  317. label = /*#__PURE__*/createElement(content, props);
  318. if ( /*#__PURE__*/isValidElement(label)) {
  319. return label;
  320. }
  321. } else {
  322. label = getLabel(props);
  323. }
  324. var isPolarLabel = isPolar(viewBox);
  325. var attrs = filterProps(props, true);
  326. if (isPolarLabel && (position === 'insideStart' || position === 'insideEnd' || position === 'end')) {
  327. return renderRadialLabel(props, label, attrs);
  328. }
  329. var positionAttrs = isPolarLabel ? getAttrsOfPolarLabel(props) : getAttrsOfCartesianLabel(props);
  330. return /*#__PURE__*/React.createElement(Text, _extends({
  331. className: clsx('recharts-label', className)
  332. }, attrs, positionAttrs, {
  333. breakAll: textBreakAll
  334. }), label);
  335. }
  336. Label.displayName = 'Label';
  337. var parseViewBox = function parseViewBox(props) {
  338. var cx = props.cx,
  339. cy = props.cy,
  340. angle = props.angle,
  341. startAngle = props.startAngle,
  342. endAngle = props.endAngle,
  343. r = props.r,
  344. radius = props.radius,
  345. innerRadius = props.innerRadius,
  346. outerRadius = props.outerRadius,
  347. x = props.x,
  348. y = props.y,
  349. top = props.top,
  350. left = props.left,
  351. width = props.width,
  352. height = props.height,
  353. clockWise = props.clockWise,
  354. labelViewBox = props.labelViewBox;
  355. if (labelViewBox) {
  356. return labelViewBox;
  357. }
  358. if (isNumber(width) && isNumber(height)) {
  359. if (isNumber(x) && isNumber(y)) {
  360. return {
  361. x: x,
  362. y: y,
  363. width: width,
  364. height: height
  365. };
  366. }
  367. if (isNumber(top) && isNumber(left)) {
  368. return {
  369. x: top,
  370. y: left,
  371. width: width,
  372. height: height
  373. };
  374. }
  375. }
  376. if (isNumber(x) && isNumber(y)) {
  377. return {
  378. x: x,
  379. y: y,
  380. width: 0,
  381. height: 0
  382. };
  383. }
  384. if (isNumber(cx) && isNumber(cy)) {
  385. return {
  386. cx: cx,
  387. cy: cy,
  388. startAngle: startAngle || angle || 0,
  389. endAngle: endAngle || angle || 0,
  390. innerRadius: innerRadius || 0,
  391. outerRadius: outerRadius || radius || r || 0,
  392. clockWise: clockWise
  393. };
  394. }
  395. if (props.viewBox) {
  396. return props.viewBox;
  397. }
  398. return {};
  399. };
  400. var parseLabel = function parseLabel(label, viewBox) {
  401. if (!label) {
  402. return null;
  403. }
  404. if (label === true) {
  405. return /*#__PURE__*/React.createElement(Label, {
  406. key: "label-implicit",
  407. viewBox: viewBox
  408. });
  409. }
  410. if (isNumOrStr(label)) {
  411. return /*#__PURE__*/React.createElement(Label, {
  412. key: "label-implicit",
  413. viewBox: viewBox,
  414. value: label
  415. });
  416. }
  417. if ( /*#__PURE__*/isValidElement(label)) {
  418. if (label.type === Label) {
  419. return /*#__PURE__*/cloneElement(label, {
  420. key: 'label-implicit',
  421. viewBox: viewBox
  422. });
  423. }
  424. return /*#__PURE__*/React.createElement(Label, {
  425. key: "label-implicit",
  426. content: label,
  427. viewBox: viewBox
  428. });
  429. }
  430. if (isFunction(label)) {
  431. return /*#__PURE__*/React.createElement(Label, {
  432. key: "label-implicit",
  433. content: label,
  434. viewBox: viewBox
  435. });
  436. }
  437. if (isObject(label)) {
  438. return /*#__PURE__*/React.createElement(Label, _extends({
  439. viewBox: viewBox
  440. }, label, {
  441. key: "label-implicit"
  442. }));
  443. }
  444. return null;
  445. };
  446. var renderCallByParent = function renderCallByParent(parentProps, viewBox) {
  447. var checkPropsLabel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  448. if (!parentProps || !parentProps.children && checkPropsLabel && !parentProps.label) {
  449. return null;
  450. }
  451. var children = parentProps.children;
  452. var parentViewBox = parseViewBox(parentProps);
  453. var explicitChildren = findAllByType(children, Label).map(function (child, index) {
  454. return /*#__PURE__*/cloneElement(child, {
  455. viewBox: viewBox || parentViewBox,
  456. // eslint-disable-next-line react/no-array-index-key
  457. key: "label-".concat(index)
  458. });
  459. });
  460. if (!checkPropsLabel) {
  461. return explicitChildren;
  462. }
  463. var implicitLabel = parseLabel(parentProps.label, viewBox || parentViewBox);
  464. return [implicitLabel].concat(_toConsumableArray(explicitChildren));
  465. };
  466. Label.parseViewBox = parseViewBox;
  467. Label.renderCallByParent = renderCallByParent;