Cursor.js 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 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; }
  3. 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; }
  4. 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; }
  5. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  6. 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); }
  7. import { cloneElement, createElement, isValidElement } from 'react';
  8. import clsx from 'clsx';
  9. import { Curve } from '../shape/Curve';
  10. import { Cross } from '../shape/Cross';
  11. import { getCursorRectangle } from '../util/cursor/getCursorRectangle';
  12. import { Rectangle } from '../shape/Rectangle';
  13. import { getRadialCursorPoints } from '../util/cursor/getRadialCursorPoints';
  14. import { Sector } from '../shape/Sector';
  15. import { getCursorPoints } from '../util/cursor/getCursorPoints';
  16. import { filterProps } from '../util/ReactUtils';
  17. /*
  18. * Cursor is the background, or a highlight,
  19. * that shows when user mouses over or activates
  20. * an area.
  21. *
  22. * It usually shows together with a tooltip
  23. * to emphasise which part of the chart does the tooltip refer to.
  24. */
  25. export function Cursor(props) {
  26. var _element$props$cursor, _defaultProps;
  27. var element = props.element,
  28. tooltipEventType = props.tooltipEventType,
  29. isActive = props.isActive,
  30. activeCoordinate = props.activeCoordinate,
  31. activePayload = props.activePayload,
  32. offset = props.offset,
  33. activeTooltipIndex = props.activeTooltipIndex,
  34. tooltipAxisBandSize = props.tooltipAxisBandSize,
  35. layout = props.layout,
  36. chartName = props.chartName;
  37. var elementPropsCursor = (_element$props$cursor = element.props.cursor) !== null && _element$props$cursor !== void 0 ? _element$props$cursor : (_defaultProps = element.type.defaultProps) === null || _defaultProps === void 0 ? void 0 : _defaultProps.cursor;
  38. if (!element || !elementPropsCursor || !isActive || !activeCoordinate || chartName !== 'ScatterChart' && tooltipEventType !== 'axis') {
  39. return null;
  40. }
  41. var restProps;
  42. var cursorComp = Curve;
  43. if (chartName === 'ScatterChart') {
  44. restProps = activeCoordinate;
  45. cursorComp = Cross;
  46. } else if (chartName === 'BarChart') {
  47. restProps = getCursorRectangle(layout, activeCoordinate, offset, tooltipAxisBandSize);
  48. cursorComp = Rectangle;
  49. } else if (layout === 'radial') {
  50. var _getRadialCursorPoint = getRadialCursorPoints(activeCoordinate),
  51. cx = _getRadialCursorPoint.cx,
  52. cy = _getRadialCursorPoint.cy,
  53. radius = _getRadialCursorPoint.radius,
  54. startAngle = _getRadialCursorPoint.startAngle,
  55. endAngle = _getRadialCursorPoint.endAngle;
  56. restProps = {
  57. cx: cx,
  58. cy: cy,
  59. startAngle: startAngle,
  60. endAngle: endAngle,
  61. innerRadius: radius,
  62. outerRadius: radius
  63. };
  64. cursorComp = Sector;
  65. } else {
  66. restProps = {
  67. points: getCursorPoints(layout, activeCoordinate, offset)
  68. };
  69. cursorComp = Curve;
  70. }
  71. var cursorProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
  72. stroke: '#ccc',
  73. pointerEvents: 'none'
  74. }, offset), restProps), filterProps(elementPropsCursor, false)), {}, {
  75. payload: activePayload,
  76. payloadIndex: activeTooltipIndex,
  77. className: clsx('recharts-tooltip-cursor', elementPropsCursor.className)
  78. });
  79. return /*#__PURE__*/isValidElement(elementPropsCursor) ? /*#__PURE__*/cloneElement(elementPropsCursor, cursorProps) : /*#__PURE__*/createElement(cursorComp, cursorProps);
  80. }