recharts
Version:
React charts
653 lines (640 loc) • 30.3 kB
JavaScript
var _excluded = ["onMouseEnter", "onClick", "onMouseLeave", "shape", "activeShape", "indexedActiveStyles"],
_excluded2 = ["id"],
_excluded3 = ["stroke", "fill", "legendType", "hide", "isAnimationActive", "animationBegin", "animationDuration", "animationEasing", "nameKey", "lastShapeType", "id", "indexedStyles", "unthemedColors", "funnelFillObject", "themeFillObjects", "hasExplicitColors"],
_excluded4 = ["id"];
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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 { useMemo, useRef } from 'react';
import omit from 'es-toolkit/compat/omit';
import { selectActiveTooltipIndex } from '../state/selectors/tooltipSelectors';
import { useAppSelector } from '../state/hooks';
import { Layer } from '../container/Layer';
import { CartesianLabelListContextProvider, LabelListFromLabelProp } from '../component/LabelList';
import { getPercentValue, interpolate } from '../util/DataUtils';
import { getValueByDataKey } from '../util/ChartUtils';
import { adaptEventsOfChild } from '../util/types';
import { defaultFunnelShape, FunnelTrapezoid } from '../util/FunnelUtils';
import { useMouseClickItemDispatch, useMouseEnterItemDispatch, useMouseLeaveItemDispatch } from '../context/tooltipContext';
import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
import { SetLegendPayload } from '../state/SetLegendPayload';
import { selectFunnelLegend, selectFunnelTrapezoids } from '../state/selectors/funnelSelectors';
import { clsx } from '../util/clsx';
import { findAllByType } from '../util/ReactUtils';
import { Cell } from '../component/Cell';
import { resolveDefaultProps } from '../util/resolveDefaultProps';
import { usePlotArea } from '../hooks';
import { svgPropertiesNoEvents } from '../util/svgPropertiesNoEvents';
import { AnimatedItems, useAnimationCallbacks } from '../animation/AnimatedItems';
import { matchAppend } from '../animation/matchBy';
import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
import { useCartesianChartLayout } from '../context/chartLayoutContext';
import { getColorGraphicalItems } from '../theme/themeFill';
import { FillObjectDefinition, getEntryFillId, getFillUrl, getIndexedEntryFillObject, splitFill, useThemeFillObjects } from '../fill/fillDefinitions';
import { useBackwardsCompatibleTheme } from '../theme/useBackwardsCompatibleTheme';
import { getOwnStyles, hasOwnColors, useUnthemedColors, withoutColors } from '../theme/dataEntryStyles';
import { getActiveStyleOverrides } from '../theme/activeStyles';
import { DataLabelColorsContextProvider, DataLabelThemeContextProvider, filledShapeLabelColors, useIndexedLabelThemeResolver } from '../theme/labelTheme';
/**
* @public
*/
/**
* Internal props, combination of external props + defaultProps + private Recharts state
*/
/**
* External props, intended for end users to fill in
*
* The type is exported only so that TypeScript can name it in the declaration files of projects
* that wrap Recharts components. It is not part of the public API and may change in any release.
*
* @internal
*/
/**
* Props after the fill object is split off: `fill` is the base color.
*/
/**
* @public
*/
/**
* The color a trapezoid is rendered with: its fill, or its stroke when there is no fill.
* Tooltip and Legend both report this so they always match the chart.
*/
export function getFunnelEntryColor(entry) {
if (entry != null && typeof entry === 'object') {
if ('fill' in entry && typeof entry.fill === 'string') {
return entry.fill;
}
if ('stroke' in entry && typeof entry.stroke === 'string') {
return entry.stroke;
}
}
return undefined;
}
var SetFunnelTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
var dataKey = _ref.dataKey,
nameKey = _ref.nameKey,
stroke = _ref.stroke,
strokeWidth = _ref.strokeWidth,
fill = _ref.fill,
name = _ref.name,
hide = _ref.hide,
tooltipType = _ref.tooltipType,
formatter = _ref.formatter,
trapezoids = _ref.trapezoids,
id = _ref.id;
var tooltipEntrySettings = {
/*
* Per-item tooltip payloads, like Pie: each trapezoid carries its resolved color so
* the tooltip item matches the chart. An explicit `name` prop still overrides the
* per-entry names, as it did before.
*/
dataDefinedOnItem: trapezoids.map(trapezoid =>
/*
* An explicit `name` prop overrides per-entry names, but only for entries whose
* datum does not provide a name through nameKey - datum names always win.
*/
name == null ? trapezoid.tooltipPayload : trapezoid.tooltipPayload.map(item => getValueByDataKey(item.payload, nameKey) != null ? item : _objectSpread(_objectSpread({}, item), {}, {
name
}))),
getPosition: index => {
var _trapezoids$Number;
return (_trapezoids$Number = trapezoids[Number(index)]) === null || _trapezoids$Number === void 0 ? void 0 : _trapezoids$Number.tooltipPosition;
},
settings: {
stroke,
strokeWidth,
fill,
dataKey,
name,
nameKey,
hide,
type: tooltipType,
color: fill,
unit: '',
// Funnel does not have unit, why?
formatter,
graphicalItemId: id
}
};
return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
tooltipEntrySettings: tooltipEntrySettings
});
});
function SetFunnelPayloadLegend(_ref2) {
var funnelSettings = _ref2.funnelSettings,
fillObjects = _ref2.fillObjects;
var legendPayload = useAppSelector(state => selectFunnelLegend(state, funnelSettings));
if (legendPayload == null) {
return null;
}
var legendPayloadWithFills = legendPayload.map((entry, index) => {
var fill = fillObjects[index];
return fill == null ? entry : _objectSpread(_objectSpread({}, entry), {}, {
fill
});
});
return /*#__PURE__*/React.createElement(SetLegendPayload, {
legendPayload: legendPayloadWithFills
});
}
/**
* Returns the data entry of a trapezoid, as the user wrote it. Undefined for trapezoids made from Cells only.
* @param trapezoid the trapezoid
*/
function getFunnelDataEntry(trapezoid) {
var payload = trapezoid.payload;
return payload != null && typeof payload === 'object' && 'payload' in payload ? payload.payload : undefined;
}
function FunnelLabelListProvider(_ref3) {
var showLabels = _ref3.showLabels,
trapezoids = _ref3.trapezoids,
children = _ref3.children;
var labelListEntries = useMemo(() => {
if (!showLabels) {
return undefined;
}
return trapezoids === null || trapezoids === void 0 ? void 0 : trapezoids.map(entry => {
var viewBox = entry.labelViewBox;
return _objectSpread(_objectSpread({}, viewBox), {}, {
value: entry.name,
payload: entry.payload,
parentViewBox: entry.parentViewBox,
viewBox,
fill: entry.fill
});
});
}, [showLabels, trapezoids]);
return /*#__PURE__*/React.createElement(CartesianLabelListContextProvider, {
value: labelListEntries
}, children);
}
/**
* Returns what the user styled a trapezoid with: its `Cell` props if they have colors, otherwise its data entry.
* The trapezoid `payload` already includes the theme colors, so the data entry is the `payload` one level deeper.
*/
function getOwnColorSource(trapezoid, cellProps) {
if (hasOwnColors(cellProps)) {
return cellProps;
}
var payload = trapezoid.payload;
return payload != null && typeof payload === 'object' && 'payload' in payload ? payload.payload : undefined;
}
function FunnelTrapezoids(props) {
var trapezoids = props.trapezoids,
allOtherFunnelProps = props.allOtherFunnelProps,
animationElapsedTime = props.animationElapsedTime,
isAnimating = props.isAnimating,
isEntrance = props.isEntrance;
var activeItemIndex = useAppSelector(selectActiveTooltipIndex);
var onMouseEnterFromProps = allOtherFunnelProps.onMouseEnter,
onItemClickFromProps = allOtherFunnelProps.onClick,
onMouseLeaveFromProps = allOtherFunnelProps.onMouseLeave,
shape = allOtherFunnelProps.shape,
activeShape = allOtherFunnelProps.activeShape,
indexedActiveStyles = allOtherFunnelProps.indexedActiveStyles,
restOfAllOtherProps = _objectWithoutProperties(allOtherFunnelProps, _excluded);
var cells = findAllByType(allOtherFunnelProps.children, Cell);
var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, allOtherFunnelProps.dataKey, allOtherFunnelProps.id);
var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps);
var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, allOtherFunnelProps.dataKey, allOtherFunnelProps.id);
return /*#__PURE__*/React.createElement(React.Fragment, null, trapezoids.map((entry, i) => {
var _cells$i, _allOtherFunnelProps$, _allOtherFunnelProps$2, _activeStyleOverrides;
var isActiveIndex = Boolean(activeShape) && activeItemIndex === String(i);
var trapezoidOptions = isActiveIndex ? activeShape : shape;
var activeStyleOverrides = isActiveIndex && indexedActiveStyles != null && indexedActiveStyles.length > 0 ? getActiveStyleOverrides(activeShape, indexedActiveStyles[i % indexedActiveStyles.length], getOwnColorSource(entry, (_cells$i = cells[i]) === null || _cells$i === void 0 ? void 0 : _cells$i.props)) : undefined;
var _entry$fill$activeSty = _objectSpread(_objectSpread(_objectSpread({}, entry), {}, {
fill: (_allOtherFunnelProps$ = (_allOtherFunnelProps$2 = allOtherFunnelProps.trapezoidPaintFills) === null || _allOtherFunnelProps$2 === void 0 ? void 0 : _allOtherFunnelProps$2[i]) !== null && _allOtherFunnelProps$ !== void 0 ? _allOtherFunnelProps$ : entry.fill
}, activeStyleOverrides), {}, {
option: trapezoidOptions,
isActive: isActiveIndex,
stroke: (_activeStyleOverrides = activeStyleOverrides === null || activeStyleOverrides === void 0 ? void 0 : activeStyleOverrides.stroke) !== null && _activeStyleOverrides !== void 0 ? _activeStyleOverrides : entry.stroke,
animationElapsedTime,
isAnimating,
isEntrance
}),
id = _entry$fill$activeSty.id,
trapezoidProps = _objectWithoutProperties(_entry$fill$activeSty, _excluded2);
return /*#__PURE__*/React.createElement(Layer, _extends({
key: "trapezoid-".concat(i),
className: "recharts-funnel-trapezoid"
}, adaptEventsOfChild(restOfAllOtherProps, entry, i), {
onMouseEnter: onMouseEnterFromContext(entry, i),
onMouseLeave: onMouseLeaveFromContext(entry, i),
onClick: onClickFromContext(entry, i)
}), /*#__PURE__*/React.createElement(FunnelTrapezoid, trapezoidProps));
}));
}
var defaultFunnelAnimateItems = (items, animationElapsedTime) => {
if (items == null) return [];
if (animationElapsedTime === 1) {
return items.flatMap(item => item.status === 'removed' ? [] : [item.next]);
}
return items.flatMap(item => {
if (item.status === 'removed') return [];
if (item.status === 'matched') {
return [_objectSpread(_objectSpread({}, item.next), {}, {
x: interpolate(item.prev.x, item.next.x, animationElapsedTime),
y: interpolate(item.prev.y, item.next.y, animationElapsedTime),
upperWidth: interpolate(item.prev.upperWidth, item.next.upperWidth, animationElapsedTime),
lowerWidth: interpolate(item.prev.lowerWidth, item.next.lowerWidth, animationElapsedTime),
height: interpolate(item.prev.height, item.next.height, animationElapsedTime)
})];
}
// added
var next = item.next;
return [_objectSpread(_objectSpread({}, next), {}, {
x: interpolate(next.x + next.upperWidth / 2, next.x, animationElapsedTime),
y: interpolate(next.y + next.height / 2, next.y, animationElapsedTime),
upperWidth: interpolate(0, next.upperWidth, animationElapsedTime),
lowerWidth: interpolate(0, next.lowerWidth, animationElapsedTime),
height: interpolate(0, next.height, animationElapsedTime)
})];
});
};
function TrapezoidsWithAnimation(_ref4) {
var previousTrapezoidsRef = _ref4.previousTrapezoidsRef,
props = _ref4.props;
var trapezoids = props.trapezoids,
isAnimationActive = props.isAnimationActive,
animationBegin = props.animationBegin,
animationDuration = props.animationDuration,
animationEasing = props.animationEasing,
animationInterpolateFn = props.animationInterpolateFn;
var layout = useCartesianChartLayout();
var _useAnimationCallback = useAnimationCallbacks(props.onAnimationStart, props.onAnimationEnd),
isAnimating = _useAnimationCallback.isAnimating,
handleAnimationStart = _useAnimationCallback.handleAnimationStart,
handleAnimationEnd = _useAnimationCallback.handleAnimationEnd;
if (layout == null) return null;
return /*#__PURE__*/React.createElement(FunnelLabelListProvider, {
showLabels: !isAnimating,
trapezoids: trapezoids
}, /*#__PURE__*/React.createElement(AnimatedItems, {
animationInput: trapezoids,
animationIdPrefix: "recharts-funnel-",
items: trapezoids,
previousItemsRef: previousTrapezoidsRef,
isAnimationActive: isAnimationActive,
animationBegin: animationBegin,
animationDuration: animationDuration,
animationEasing: animationEasing,
onAnimationStart: handleAnimationStart,
onAnimationEnd: handleAnimationEnd,
animationInterpolateFn: animationInterpolateFn,
animationMatchBy: props.animationMatchBy,
layout: layout
}, (stepData, animationElapsedTime, isEntrance) => /*#__PURE__*/React.createElement(Layer, null, /*#__PURE__*/React.createElement(FunnelTrapezoids, {
trapezoids: stepData,
allOtherFunnelProps: props,
animationElapsedTime: animationElapsedTime,
isAnimating: isAnimating || animationElapsedTime < 1,
isEntrance: isEntrance
}))), /*#__PURE__*/React.createElement(LabelListFromLabelProp, {
label: props.label
}), props.children);
}
function RenderTrapezoids(props) {
var previousTrapezoidsRef = useRef(undefined);
return /*#__PURE__*/React.createElement(TrapezoidsWithAnimation, {
props: props,
previousTrapezoidsRef: previousTrapezoidsRef
});
}
/**
* Every themeable style key set to undefined.
* Spreading this first removes the legacy defaults, which never apply together with a theme.
*/
var NO_STYLES = {
fill: undefined,
fillOpacity: undefined,
stroke: undefined,
strokeOpacity: undefined,
strokeWidth: undefined,
strokeDasharray: undefined
};
var getRealWidthHeight = (customWidth, offset) => {
var width = offset.width,
height = offset.height,
left = offset.left,
top = offset.top;
var realWidth = getPercentValue(customWidth, width, width);
return {
realWidth,
realHeight: height,
offsetX: left,
offsetY: top
};
};
export var defaultFunnelProps = {
animationBegin: 400,
animationDuration: 1500,
animationEasing: 'ease',
animationInterpolateFn: defaultFunnelAnimateItems,
animationMatchBy: matchAppend,
fill: '#808080',
hide: false,
isAnimationActive: 'auto',
lastShapeType: 'triangle',
legendType: 'rect',
nameKey: 'name',
reversed: false,
shape: defaultFunnelShape,
stroke: '#fff'
};
function FunnelImpl(props) {
var plotArea = usePlotArea();
var stroke = props.stroke,
fill = props.fill,
legendType = props.legendType,
hide = props.hide,
isAnimationActive = props.isAnimationActive,
animationBegin = props.animationBegin,
animationDuration = props.animationDuration,
animationEasing = props.animationEasing,
nameKey = props.nameKey,
lastShapeType = props.lastShapeType,
id = props.id,
indexedStyles = props.indexedStyles,
unthemedColors = props.unthemedColors,
funnelFillObject = props.funnelFillObject,
themeFillObjects = props.themeFillObjects,
hasExplicitColors = props.hasExplicitColors,
everythingElse = _objectWithoutProperties(props, _excluded3);
var presentationProps = svgPropertiesNoEvents(props);
var cells = findAllByType(props.children, Cell);
var funnelSettings = useMemo(() => ({
dataKey: props.dataKey,
nameKey,
data: props.data,
tooltipType: props.tooltipType,
lastShapeType,
reversed: props.reversed,
customWidth: props.width,
cells,
presentationProps,
indexedStyles,
unthemedColors,
legendType: props.legendType,
id
}), [props.dataKey, nameKey, props.data, props.tooltipType, lastShapeType, props.reversed, props.width, cells, presentationProps, indexedStyles, unthemedColors, props.legendType, id]);
var trapezoids = useAppSelector(state => selectFunnelTrapezoids(state, funnelSettings));
var trapezoidFillObjects = (trapezoids !== null && trapezoids !== void 0 ? trapezoids : []).map((trapezoid, index) => getIndexedEntryFillObject(getFunnelDataEntry(trapezoid), index, funnelFillObject, themeFillObjects, hasExplicitColors));
if (hide || !trapezoids || !trapezoids.length || !plotArea) {
return null;
}
var height = plotArea.height,
width = plotArea.width;
var layerClass = clsx('recharts-trapezoids', props.className);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetFunnelPayloadLegend, {
funnelSettings: funnelSettings,
fillObjects: trapezoidFillObjects
}), /*#__PURE__*/React.createElement(SetFunnelTooltipEntrySettings, {
dataKey: props.dataKey,
nameKey: props.nameKey,
stroke: props.stroke,
strokeWidth: props.strokeWidth,
fill: props.fill,
name: props.name,
hide: props.hide,
tooltipType: props.tooltipType,
formatter: props.formatter,
trapezoids: trapezoids,
id: id
}), /*#__PURE__*/React.createElement(Layer, {
className: layerClass
}, trapezoidFillObjects.some(fillObject => fillObject != null) && /*#__PURE__*/React.createElement("defs", null, trapezoidFillObjects.map((fillObject, index) => {
var _trapezoids$index;
return fillObject == null ? null : /*#__PURE__*/React.createElement(FillObjectDefinition
// the trapezoid index is the identity of its definition
, {
key: index,
id: getEntryFillId(id, index),
fillObject: fillObject,
color: (_trapezoids$index = trapezoids[index]) === null || _trapezoids$index === void 0 ? void 0 : _trapezoids$index.fill
});
})), /*#__PURE__*/React.createElement(RenderTrapezoids, _extends({
trapezoidPaintFills: trapezoidFillObjects.map((fillObject, index) => fillObject == null ? undefined : getFillUrl(getEntryFillId(id, index)))
}, everythingElse, {
id: id,
stroke: stroke,
fill: fill,
nameKey: nameKey,
lastShapeType: lastShapeType,
animationBegin: animationBegin,
animationDuration: animationDuration,
animationEasing: animationEasing,
isAnimationActive: isAnimationActive,
hide: hide,
legendType: legendType,
height: height,
width: width,
trapezoids: trapezoids
}))));
}
export function computeFunnelTrapezoids(_ref5) {
var dataKey = _ref5.dataKey,
nameKey = _ref5.nameKey,
displayedData = _ref5.displayedData,
tooltipType = _ref5.tooltipType,
lastShapeType = _ref5.lastShapeType,
reversed = _ref5.reversed,
offset = _ref5.offset,
customWidth = _ref5.customWidth,
graphicalItemId = _ref5.graphicalItemId;
var _getRealWidthHeight = getRealWidthHeight(customWidth, offset),
realHeight = _getRealWidthHeight.realHeight,
realWidth = _getRealWidthHeight.realWidth,
offsetX = _getRealWidthHeight.offsetX,
offsetY = _getRealWidthHeight.offsetY;
var values = displayedData.map(entry => {
var val = getValueByDataKey(entry, dataKey, 0);
return typeof val === 'number' ? val : 0;
});
var maxValue = Math.max.apply(null, values);
var len = displayedData.length;
var rowHeight = realHeight / len;
var parentViewBox = {
x: offset.left,
y: offset.top,
width: offset.width,
height: offset.height
};
var trapezoids = displayedData.map((entry, i) => {
// getValueByDataKey does not validate the output type
var rawVal = getValueByDataKey(entry, dataKey, 0);
var name = String(getValueByDataKey(entry, nameKey, i));
var val = rawVal;
var nextVal;
if (i !== len - 1) {
var nextDataValue = getValueByDataKey(displayedData[i + 1], dataKey, 0);
if (typeof nextDataValue === 'number') {
nextVal = nextDataValue;
} else if (Array.isArray(nextDataValue)) {
var _nextDataValue = _slicedToArray(nextDataValue, 2),
first = _nextDataValue[0],
second = _nextDataValue[1];
if (typeof first === 'number') {
val = first;
}
if (typeof second === 'number') {
nextVal = second;
}
} else if (nextDataValue === null) {
nextVal = 0;
}
} else if (rawVal instanceof Array && rawVal.length === 2) {
var _rawVal = _slicedToArray(rawVal, 2),
_first = _rawVal[0],
_second = _rawVal[1];
if (typeof _first === 'number') {
val = _first;
}
if (typeof _second === 'number') {
nextVal = _second;
}
} else if (lastShapeType === 'rectangle') {
nextVal = val;
} else {
nextVal = 0;
}
// @ts-expect-error this is a problem if we have ranged values because `val` can be an array
var x = maxValue === 0 ? offsetX : (maxValue - val) * realWidth / (2 * maxValue) + offsetX;
var y = rowHeight * i + offsetY;
// @ts-expect-error getValueByDataKey does not validate the output type
var upperWidth = maxValue === 0 ? 0 : val / maxValue * realWidth;
// @ts-expect-error nextVal could be an array
var lowerWidth = maxValue === 0 ? 0 : nextVal / maxValue * realWidth;
var trapezoidColor = getFunnelEntryColor(entry);
var tooltipPayload = [_objectSpread({
name,
value: val,
payload: entry,
dataKey,
type: tooltipType,
graphicalItemId
}, trapezoidColor != null ? {
color: trapezoidColor,
fill: trapezoidColor
} : {})];
var tooltipPosition = {
x: x + upperWidth / 2,
y: y + rowHeight / 2
};
var trapezoidViewBox = {
x,
y,
upperWidth,
lowerWidth,
width: Math.max(upperWidth, lowerWidth),
height: rowHeight
};
return _objectSpread(_objectSpread(_objectSpread({}, trapezoidViewBox), {}, {
name,
val,
tooltipPayload,
tooltipPosition
}, entry != null && typeof entry === 'object' ? omit(entry, ['width']) : {}), {}, {
payload: entry,
parentViewBox,
labelViewBox: trapezoidViewBox
});
});
if (reversed) {
trapezoids = trapezoids.map((entry, index) => {
var reversedViewBox = {
x: entry.x - (entry.lowerWidth - entry.upperWidth) / 2,
y: entry.y - index * rowHeight + (len - 1 - index) * rowHeight,
upperWidth: entry.lowerWidth,
lowerWidth: entry.upperWidth,
width: Math.max(entry.lowerWidth, entry.upperWidth),
height: rowHeight
};
return _objectSpread(_objectSpread(_objectSpread({}, entry), reversedViewBox), {}, {
tooltipPosition: _objectSpread(_objectSpread({}, entry.tooltipPosition), {}, {
y: entry.y - index * rowHeight + (len - 1 - index) * rowHeight + rowHeight / 2
}),
labelViewBox: reversedViewBox
});
});
}
return trapezoids;
}
/**
* @consumes CartesianViewBoxContext
* @provides LabelListContext
* @provides CellReader
* @consumes RechartsThemeContext
*/
function FunnelFn(propsWithFillObject) {
var _splitFill = splitFill(propsWithFillObject.fill),
fillColor = _splitFill.color,
funnelFillObject = _splitFill.fillObject;
// From here on, `fill` is the base color
var outsideProps = useMemo(() => _objectSpread(_objectSpread({}, propsWithFillObject), {}, {
fill: fillColor
}), [propsWithFillObject, fillColor]);
var theme = useBackwardsCompatibleTheme(rechartsTheme => ({
graphicalItems: rechartsTheme.graphicalItems
}), {}, undefined);
/*
* Funnel is per-index: each trapezoid at index `i` picks its styles from
* graphicalItems[i % graphicalItems.length].
* If the Funnel sets its own fill or stroke, it drops the theme fill and stroke together
* so that its colors are not mixed with the theme colors.
* Other explicit style props merge with the theme field by field.
*/
var hasExplicitColors = hasOwnColors(outsideProps);
var indexedStyles = useMemo(() => {
var graphicalItems = getColorGraphicalItems(theme === null || theme === void 0 ? void 0 : theme.graphicalItems);
if (graphicalItems == null || graphicalItems.length === 0) {
return [];
}
return graphicalItems.map(style => _objectSpread(_objectSpread(_objectSpread({}, NO_STYLES), getOwnStyles(hasExplicitColors ? withoutColors(style) : style)), getOwnStyles(outsideProps)));
}, [outsideProps, theme, hasExplicitColors]);
var unthemedColors = useUnthemedColors(outsideProps);
/*
* The active styles are per-index too, same as `indexedStyles`.
* The theme active colors go with the theme colors, so a Funnel with its own colors drops them too.
*/
var themeGraphicalItems = getColorGraphicalItems(theme === null || theme === void 0 ? void 0 : theme.graphicalItems);
var indexedActiveStyles = useMemo(() => (themeGraphicalItems !== null && themeGraphicalItems !== void 0 ? themeGraphicalItems : []).map(style => hasExplicitColors ? withoutColors(style.active) : style.active), [themeGraphicalItems, hasExplicitColors]);
var resolveLabelTheme = useIndexedLabelThemeResolver(getColorGraphicalItems(theme === null || theme === void 0 ? void 0 : theme.graphicalItems));
var themeFillObjects = useThemeFillObjects();
var _resolveDefaultProps = resolveDefaultProps(outsideProps, defaultFunnelProps),
externalId = _resolveDefaultProps.id,
resolvedProps = _objectWithoutProperties(_resolveDefaultProps, _excluded4);
return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
id: externalId,
type: "funnel"
}, id => /*#__PURE__*/React.createElement(DataLabelThemeContextProvider, {
value: resolveLabelTheme
}, /*#__PURE__*/React.createElement(DataLabelColorsContextProvider, {
value: filledShapeLabelColors
}, /*#__PURE__*/React.createElement(FunnelImpl, _extends({}, resolvedProps, {
id: id,
indexedStyles: indexedStyles,
unthemedColors: indexedStyles.length > 0 ? unthemedColors : undefined,
indexedActiveStyles: indexedActiveStyles,
funnelFillObject: funnelFillObject,
themeFillObjects: themeFillObjects,
hasExplicitColors: hasExplicitColors
})))));
}
/**
* @public
*/
export var Funnel = FunnelFn;
// @ts-expect-error we need to set the displayName for debugging purposes
Funnel.displayName = 'Funnel';