| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.Cursor = Cursor;
|
|---|
| 7 | exports.CursorInternal = CursorInternal;
|
|---|
| 8 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 9 | var React = _react;
|
|---|
| 10 | var _clsx = require("clsx");
|
|---|
| 11 | var _types = require("../util/types");
|
|---|
| 12 | var _Curve = require("../shape/Curve");
|
|---|
| 13 | var _Cross = require("../shape/Cross");
|
|---|
| 14 | var _getCursorRectangle = require("../util/cursor/getCursorRectangle");
|
|---|
| 15 | var _Rectangle = require("../shape/Rectangle");
|
|---|
| 16 | var _getRadialCursorPoints = require("../util/cursor/getRadialCursorPoints");
|
|---|
| 17 | var _Sector = require("../shape/Sector");
|
|---|
| 18 | var _getCursorPoints = require("../util/cursor/getCursorPoints");
|
|---|
| 19 | var _chartLayoutContext = require("../context/chartLayoutContext");
|
|---|
| 20 | var _useTooltipAxis = require("../context/useTooltipAxis");
|
|---|
| 21 | var _selectors = require("../state/selectors/selectors");
|
|---|
| 22 | var _svgPropertiesNoEvents = require("../util/svgPropertiesNoEvents");
|
|---|
| 23 | var _ZIndexLayer = require("../zIndex/ZIndexLayer");
|
|---|
| 24 | var _DefaultZIndexes = require("../zIndex/DefaultZIndexes");
|
|---|
| 25 | function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|---|
| 26 | function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|---|
| 27 | 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; }
|
|---|
| 28 | 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; }
|
|---|
| 29 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 30 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 31 | 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); }
|
|---|
| 32 | /**
|
|---|
| 33 | * If set false, no cursor will be drawn when tooltip is active.
|
|---|
| 34 | * If set an object, the option is the configuration of cursor.
|
|---|
| 35 | * If set a React element, the option is the custom react element of drawing cursor
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | function RenderCursor(_ref) {
|
|---|
| 39 | var {
|
|---|
| 40 | cursor,
|
|---|
| 41 | cursorComp,
|
|---|
| 42 | cursorProps
|
|---|
| 43 | } = _ref;
|
|---|
| 44 | if (/*#__PURE__*/(0, _react.isValidElement)(cursor)) {
|
|---|
| 45 | return /*#__PURE__*/(0, _react.cloneElement)(cursor, cursorProps);
|
|---|
| 46 | }
|
|---|
| 47 | return /*#__PURE__*/(0, _react.createElement)(cursorComp, cursorProps);
|
|---|
| 48 | }
|
|---|
| 49 | function CursorInternal(props) {
|
|---|
| 50 | var _props$zIndex;
|
|---|
| 51 | var {
|
|---|
| 52 | coordinate,
|
|---|
| 53 | payload,
|
|---|
| 54 | index,
|
|---|
| 55 | offset,
|
|---|
| 56 | tooltipAxisBandSize,
|
|---|
| 57 | layout,
|
|---|
| 58 | cursor,
|
|---|
| 59 | tooltipEventType,
|
|---|
| 60 | chartName
|
|---|
| 61 | } = props;
|
|---|
| 62 |
|
|---|
| 63 | // The cursor is a part of the Tooltip, and it should be shown (by default) when the Tooltip is active.
|
|---|
| 64 | var activeCoordinate = coordinate;
|
|---|
| 65 | var activePayload = payload;
|
|---|
| 66 | var activeTooltipIndex = index;
|
|---|
| 67 | if (!cursor || !activeCoordinate || chartName !== 'ScatterChart' && tooltipEventType !== 'axis') {
|
|---|
| 68 | return null;
|
|---|
| 69 | }
|
|---|
| 70 | var restProps, cursorComp, preferredZIndex;
|
|---|
| 71 | if (chartName === 'ScatterChart') {
|
|---|
| 72 | restProps = activeCoordinate;
|
|---|
| 73 | cursorComp = _Cross.Cross;
|
|---|
| 74 | preferredZIndex = _DefaultZIndexes.DefaultZIndexes.cursorLine;
|
|---|
| 75 | } else if (chartName === 'BarChart') {
|
|---|
| 76 | restProps = (0, _getCursorRectangle.getCursorRectangle)(layout, activeCoordinate, offset, tooltipAxisBandSize);
|
|---|
| 77 | cursorComp = _Rectangle.Rectangle;
|
|---|
| 78 | preferredZIndex = _DefaultZIndexes.DefaultZIndexes.cursorRectangle;
|
|---|
| 79 | } else if (layout === 'radial' && (0, _types.isPolarCoordinate)(activeCoordinate)) {
|
|---|
| 80 | var {
|
|---|
| 81 | cx,
|
|---|
| 82 | cy,
|
|---|
| 83 | radius,
|
|---|
| 84 | startAngle,
|
|---|
| 85 | endAngle
|
|---|
| 86 | } = (0, _getRadialCursorPoints.getRadialCursorPoints)(activeCoordinate);
|
|---|
| 87 | restProps = {
|
|---|
| 88 | cx,
|
|---|
| 89 | cy,
|
|---|
| 90 | startAngle,
|
|---|
| 91 | endAngle,
|
|---|
| 92 | innerRadius: radius,
|
|---|
| 93 | outerRadius: radius
|
|---|
| 94 | };
|
|---|
| 95 | cursorComp = _Sector.Sector;
|
|---|
| 96 | preferredZIndex = _DefaultZIndexes.DefaultZIndexes.cursorLine;
|
|---|
| 97 | } else {
|
|---|
| 98 | restProps = {
|
|---|
| 99 | points: (0, _getCursorPoints.getCursorPoints)(layout, activeCoordinate, offset)
|
|---|
| 100 | };
|
|---|
| 101 | cursorComp = _Curve.Curve;
|
|---|
| 102 | preferredZIndex = _DefaultZIndexes.DefaultZIndexes.cursorLine;
|
|---|
| 103 | }
|
|---|
| 104 | var extraClassName = typeof cursor === 'object' && 'className' in cursor ? cursor.className : undefined;
|
|---|
| 105 | var cursorProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
|
|---|
| 106 | stroke: '#ccc',
|
|---|
| 107 | pointerEvents: 'none'
|
|---|
| 108 | }, offset), restProps), (0, _svgPropertiesNoEvents.svgPropertiesNoEventsFromUnknown)(cursor)), {}, {
|
|---|
| 109 | payload: activePayload,
|
|---|
| 110 | payloadIndex: activeTooltipIndex,
|
|---|
| 111 | className: (0, _clsx.clsx)('recharts-tooltip-cursor', extraClassName)
|
|---|
| 112 | });
|
|---|
| 113 | return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
|
|---|
| 114 | zIndex: (_props$zIndex = props.zIndex) !== null && _props$zIndex !== void 0 ? _props$zIndex : preferredZIndex
|
|---|
| 115 | }, /*#__PURE__*/React.createElement(RenderCursor, {
|
|---|
| 116 | cursor: cursor,
|
|---|
| 117 | cursorComp: cursorComp,
|
|---|
| 118 | cursorProps: cursorProps
|
|---|
| 119 | }));
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /*
|
|---|
| 123 | * Cursor is the background, or a highlight,
|
|---|
| 124 | * that shows when user mouses over or activates
|
|---|
| 125 | * an area.
|
|---|
| 126 | *
|
|---|
| 127 | * It usually shows together with a tooltip
|
|---|
| 128 | * to emphasise which part of the chart does the tooltip refer to.
|
|---|
| 129 | */
|
|---|
| 130 | function Cursor(props) {
|
|---|
| 131 | var tooltipAxisBandSize = (0, _useTooltipAxis.useTooltipAxisBandSize)();
|
|---|
| 132 | var offset = (0, _chartLayoutContext.useOffsetInternal)();
|
|---|
| 133 | var layout = (0, _chartLayoutContext.useChartLayout)();
|
|---|
| 134 | var chartName = (0, _selectors.useChartName)();
|
|---|
| 135 | if (tooltipAxisBandSize == null || offset == null || layout == null || chartName == null) {
|
|---|
| 136 | return null;
|
|---|
| 137 | }
|
|---|
| 138 | return /*#__PURE__*/React.createElement(CursorInternal, _extends({}, props, {
|
|---|
| 139 | offset: offset,
|
|---|
| 140 | layout: layout,
|
|---|
| 141 | tooltipAxisBandSize: tooltipAxisBandSize,
|
|---|
| 142 | chartName: chartName
|
|---|
| 143 | }));
|
|---|
| 144 | } |
|---|