recharts
Version:
React charts
167 lines (162 loc) • 8.59 kB
JavaScript
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); }
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; }
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; }
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; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
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); }
import * as React from 'react';
import { cloneElement, createElement, isValidElement } from 'react';
import { clsx } from '../util/clsx';
import { isPolarCoordinate } from '../util/types';
import { Curve } from '../shape/Curve';
import { Cross } from '../shape/Cross';
import { getCursorRectangle } from '../util/cursor/getCursorRectangle';
import { Rectangle } from '../shape/Rectangle';
import { getRadialCursorPoints } from '../util/cursor/getRadialCursorPoints';
import { Sector } from '../shape/Sector';
import { getCursorPoints } from '../util/cursor/getCursorPoints';
import { useChartLayout, useOffsetInternal } from '../context/chartLayoutContext';
import { useTooltipAxisBandSize } from '../context/useTooltipAxis';
import { useChartName } from '../state/selectors/selectors';
import { useAppSelector } from '../state/hooks';
import { selectIsZoomed } from '../state/selectors/zoomSelectors';
import { useClipPathId } from '../container/ClipPathProvider';
import { svgPropertiesNoEventsFromUnknown } from '../util/svgPropertiesNoEvents';
import { withFillColor } from '../theme/themeFill';
import { useBackwardsCompatibleTheme } from '../theme/useBackwardsCompatibleTheme';
import { useRechartsTheme } from '../theme/RechartsThemeContext';
import { ZIndexLayer } from '../zIndex/ZIndexLayer';
import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
/**
* If set false, no cursor will be drawn when tooltip is active.
* If set an object, the option is the configuration of cursor.
* If set a React element, the option is the custom react element of drawing cursor
* @public
*/
var defaultLegacyCursorThemeProps = {
stroke: '#ccc',
fill: '#ccc'
};
function RenderCursor(_ref) {
var cursor = _ref.cursor,
cursorComp = _ref.cursorComp,
cursorProps = _ref.cursorProps;
if (/*#__PURE__*/isValidElement(cursor)) {
return /*#__PURE__*/cloneElement(cursor, cursorProps);
}
return /*#__PURE__*/createElement(cursorComp, cursorProps);
}
export function CursorInternal(props) {
var _svgPropertiesNoEvent, _useAppSelector, _props$zIndex;
var coordinate = props.coordinate,
payload = props.payload,
index = props.index,
offset = props.offset,
tooltipAxisBandSize = props.tooltipAxisBandSize,
layout = props.layout,
cursor = props.cursor,
tooltipEventType = props.tooltipEventType,
chartName = props.chartName;
var cursorPropsFromDefinition = (_svgPropertiesNoEvent = svgPropertiesNoEventsFromUnknown(cursor)) !== null && _svgPropertiesNoEvent !== void 0 ? _svgPropertiesNoEvent : {};
var activeTheme = useRechartsTheme();
var cursorThemeProps = useBackwardsCompatibleTheme(theme => {
if (/*#__PURE__*/isValidElement(cursor)) {
return undefined;
}
// The cross of a ScatterChart is two lines, so it takes the color of a fill object
return chartName === 'ScatterChart' ? withFillColor(theme.cursor) : theme.cursor;
}, cursorPropsFromDefinition, defaultLegacyCursorThemeProps);
/*
* Bar cursor geometry includes its own legacy fill and stroke. Keep compatibility defaults before geometry,
* while provider styles must override those shape-specific defaults.
*/
var cursorPropsBeforeGeometry = activeTheme == null ? cursorThemeProps : {};
var cursorPropsAfterGeometry = (activeTheme === null || activeTheme === void 0 ? void 0 : activeTheme.cursor) != null && ! /*#__PURE__*/isValidElement(cursor) ? cursorThemeProps : {};
/*
* When zoomed, the cursor (especially the thick band cursor of bar charts) can spill over the
* plot edges, so clip it to the plotting area - the same clip the graphical items already use.
*/
var clipPathId = useClipPathId();
var isZoomed = (_useAppSelector = useAppSelector(selectIsZoomed)) !== null && _useAppSelector !== void 0 ? _useAppSelector : false;
// The cursor is a part of the Tooltip, and it should be shown (by default) when the Tooltip is active.
var activeCoordinate = coordinate;
var activePayload = payload;
var activeTooltipIndex = index;
if (!cursor || !activeCoordinate || chartName !== 'ScatterChart' && tooltipEventType !== 'axis') {
return null;
}
var restProps, cursorComp, preferredZIndex;
if (chartName === 'ScatterChart') {
restProps = activeCoordinate;
cursorComp = Cross;
preferredZIndex = DefaultZIndexes.cursorLine;
} else if (chartName === 'BarChart') {
restProps = getCursorRectangle(layout, activeCoordinate, offset, tooltipAxisBandSize);
cursorComp = Rectangle;
preferredZIndex = DefaultZIndexes.cursorRectangle;
} else if (layout === 'radial' && isPolarCoordinate(activeCoordinate)) {
var _getRadialCursorPoint = getRadialCursorPoints(activeCoordinate),
cx = _getRadialCursorPoint.cx,
cy = _getRadialCursorPoint.cy,
radius = _getRadialCursorPoint.radius,
startAngle = _getRadialCursorPoint.startAngle,
endAngle = _getRadialCursorPoint.endAngle;
restProps = {
cx,
cy,
startAngle,
endAngle,
innerRadius: radius,
outerRadius: radius
};
cursorComp = Sector;
preferredZIndex = DefaultZIndexes.cursorLine;
} else {
restProps = {
points: getCursorPoints(layout, activeCoordinate, offset)
};
cursorComp = Curve;
preferredZIndex = DefaultZIndexes.cursorLine;
}
var extraClassName = typeof cursor === 'object' && 'className' in cursor ? cursor.className : undefined;
var cursorProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cursorPropsBeforeGeometry), {}, {
pointerEvents: 'none'
}, offset), restProps), cursorPropsFromDefinition), cursorPropsAfterGeometry), {}, {
payload: activePayload,
payloadIndex: activeTooltipIndex,
className: clsx('recharts-tooltip-cursor', extraClassName)
});
var cursorElement = /*#__PURE__*/React.createElement(RenderCursor, {
cursor: cursor,
cursorComp: cursorComp,
cursorProps: cursorProps
});
return /*#__PURE__*/React.createElement(ZIndexLayer, {
zIndex: (_props$zIndex = props.zIndex) !== null && _props$zIndex !== void 0 ? _props$zIndex : preferredZIndex
}, isZoomed && clipPathId ? /*#__PURE__*/React.createElement("g", {
clipPath: "url(#".concat(clipPathId, ")")
}, cursorElement) : cursorElement);
}
/*
* Cursor is the background, or a highlight,
* that shows when user mouses over or activates
* an area.
*
* It usually shows together with a tooltip
* to emphasise which part of the chart does the tooltip refer to.
*/
export function Cursor(props) {
var tooltipAxisBandSize = useTooltipAxisBandSize();
var offset = useOffsetInternal();
var layout = useChartLayout();
var chartName = useChartName();
if (tooltipAxisBandSize == null || offset == null || layout == null || chartName == null) {
return null;
}
return /*#__PURE__*/React.createElement(CursorInternal, _extends({}, props, {
offset: offset,
layout: layout,
tooltipAxisBandSize: tooltipAxisBandSize,
chartName: chartName
}));
}