| 1 | import { clsx } from 'clsx';
|
|---|
| 2 | import { isNumber } from '../DataUtils';
|
|---|
| 3 | var CSS_CLASS_PREFIX = 'recharts-tooltip-wrapper';
|
|---|
| 4 | var TOOLTIP_HIDDEN = {
|
|---|
| 5 | visibility: 'hidden'
|
|---|
| 6 | };
|
|---|
| 7 | export function getTooltipCSSClassName(_ref) {
|
|---|
| 8 | var {
|
|---|
| 9 | coordinate,
|
|---|
| 10 | translateX,
|
|---|
| 11 | translateY
|
|---|
| 12 | } = _ref;
|
|---|
| 13 | return clsx(CSS_CLASS_PREFIX, {
|
|---|
| 14 | ["".concat(CSS_CLASS_PREFIX, "-right")]: isNumber(translateX) && coordinate && isNumber(coordinate.x) && translateX >= coordinate.x,
|
|---|
| 15 | ["".concat(CSS_CLASS_PREFIX, "-left")]: isNumber(translateX) && coordinate && isNumber(coordinate.x) && translateX < coordinate.x,
|
|---|
| 16 | ["".concat(CSS_CLASS_PREFIX, "-bottom")]: isNumber(translateY) && coordinate && isNumber(coordinate.y) && translateY >= coordinate.y,
|
|---|
| 17 | ["".concat(CSS_CLASS_PREFIX, "-top")]: isNumber(translateY) && coordinate && isNumber(coordinate.y) && translateY < coordinate.y
|
|---|
| 18 | });
|
|---|
| 19 | }
|
|---|
| 20 | export function getTooltipTranslateXY(_ref2) {
|
|---|
| 21 | var {
|
|---|
| 22 | allowEscapeViewBox,
|
|---|
| 23 | coordinate,
|
|---|
| 24 | key,
|
|---|
| 25 | offset,
|
|---|
| 26 | position,
|
|---|
| 27 | reverseDirection,
|
|---|
| 28 | tooltipDimension,
|
|---|
| 29 | viewBox,
|
|---|
| 30 | viewBoxDimension
|
|---|
| 31 | } = _ref2;
|
|---|
| 32 | if (position && isNumber(position[key])) {
|
|---|
| 33 | return position[key];
|
|---|
| 34 | }
|
|---|
| 35 | var negative = coordinate[key] - tooltipDimension - (offset > 0 ? offset : 0);
|
|---|
| 36 | var positive = coordinate[key] + offset;
|
|---|
| 37 | if (allowEscapeViewBox[key]) {
|
|---|
| 38 | return reverseDirection[key] ? negative : positive;
|
|---|
| 39 | }
|
|---|
| 40 | var viewBoxKey = viewBox[key];
|
|---|
| 41 | if (viewBoxKey == null) {
|
|---|
| 42 | return 0;
|
|---|
| 43 | }
|
|---|
| 44 | if (reverseDirection[key]) {
|
|---|
| 45 | var _tooltipBoundary = negative;
|
|---|
| 46 | var _viewBoxBoundary = viewBoxKey;
|
|---|
| 47 | if (_tooltipBoundary < _viewBoxBoundary) {
|
|---|
| 48 | return Math.max(positive, viewBoxKey);
|
|---|
| 49 | }
|
|---|
| 50 | return Math.max(negative, viewBoxKey);
|
|---|
| 51 | }
|
|---|
| 52 | if (viewBoxDimension == null) {
|
|---|
| 53 | return 0;
|
|---|
| 54 | }
|
|---|
| 55 | var tooltipBoundary = positive + tooltipDimension;
|
|---|
| 56 | var viewBoxBoundary = viewBoxKey + viewBoxDimension;
|
|---|
| 57 | if (tooltipBoundary > viewBoxBoundary) {
|
|---|
| 58 | return Math.max(negative, viewBoxKey);
|
|---|
| 59 | }
|
|---|
| 60 | return Math.max(positive, viewBoxKey);
|
|---|
| 61 | }
|
|---|
| 62 | export function getTransformStyle(_ref3) {
|
|---|
| 63 | var {
|
|---|
| 64 | translateX,
|
|---|
| 65 | translateY,
|
|---|
| 66 | useTranslate3d
|
|---|
| 67 | } = _ref3;
|
|---|
| 68 | return {
|
|---|
| 69 | transform: useTranslate3d ? "translate3d(".concat(translateX, "px, ").concat(translateY, "px, 0)") : "translate(".concat(translateX, "px, ").concat(translateY, "px)")
|
|---|
| 70 | };
|
|---|
| 71 | }
|
|---|
| 72 | export function getTooltipTranslate(_ref4) {
|
|---|
| 73 | var {
|
|---|
| 74 | allowEscapeViewBox,
|
|---|
| 75 | coordinate,
|
|---|
| 76 | offsetTop,
|
|---|
| 77 | offsetLeft,
|
|---|
| 78 | position,
|
|---|
| 79 | reverseDirection,
|
|---|
| 80 | tooltipBox,
|
|---|
| 81 | useTranslate3d,
|
|---|
| 82 | viewBox
|
|---|
| 83 | } = _ref4;
|
|---|
| 84 | var cssProperties, translateX, translateY;
|
|---|
| 85 | if (tooltipBox.height > 0 && tooltipBox.width > 0 && coordinate) {
|
|---|
| 86 | translateX = getTooltipTranslateXY({
|
|---|
| 87 | allowEscapeViewBox,
|
|---|
| 88 | coordinate,
|
|---|
| 89 | key: 'x',
|
|---|
| 90 | offset: offsetLeft,
|
|---|
| 91 | position,
|
|---|
| 92 | reverseDirection,
|
|---|
| 93 | tooltipDimension: tooltipBox.width,
|
|---|
| 94 | viewBox,
|
|---|
| 95 | viewBoxDimension: viewBox.width
|
|---|
| 96 | });
|
|---|
| 97 | translateY = getTooltipTranslateXY({
|
|---|
| 98 | allowEscapeViewBox,
|
|---|
| 99 | coordinate,
|
|---|
| 100 | key: 'y',
|
|---|
| 101 | offset: offsetTop,
|
|---|
| 102 | position,
|
|---|
| 103 | reverseDirection,
|
|---|
| 104 | tooltipDimension: tooltipBox.height,
|
|---|
| 105 | viewBox,
|
|---|
| 106 | viewBoxDimension: viewBox.height
|
|---|
| 107 | });
|
|---|
| 108 | cssProperties = getTransformStyle({
|
|---|
| 109 | translateX,
|
|---|
| 110 | translateY,
|
|---|
| 111 | useTranslate3d
|
|---|
| 112 | });
|
|---|
| 113 | } else {
|
|---|
| 114 | cssProperties = TOOLTIP_HIDDEN;
|
|---|
| 115 | }
|
|---|
| 116 | return {
|
|---|
| 117 | cssProperties,
|
|---|
| 118 | cssClasses: getTooltipCSSClassName({
|
|---|
| 119 | translateX,
|
|---|
| 120 | translateY,
|
|---|
| 121 | coordinate
|
|---|
| 122 | })
|
|---|
| 123 | };
|
|---|
| 124 | } |
|---|