source: node_modules/recharts/es6/cartesian/Funnel.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 17.2 KB
Line 
1var _excluded = ["onMouseEnter", "onClick", "onMouseLeave", "shape", "activeShape"],
2 _excluded2 = ["id"],
3 _excluded3 = ["stroke", "fill", "legendType", "hide", "isAnimationActive", "animationBegin", "animationDuration", "animationEasing", "nameKey", "lastShapeType", "id"],
4 _excluded4 = ["id"];
5function _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); }
6function _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; }
7function _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; }
8function 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; }
9function _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; }
10function _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; }
11function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
12function _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); }
13import * as React from 'react';
14import { useCallback, useMemo, useRef, useState } from 'react';
15import omit from 'es-toolkit/compat/omit';
16import { clsx } from 'clsx';
17import { selectActiveIndex } from '../state/selectors/selectors';
18import { useAppSelector } from '../state/hooks';
19import { Layer } from '../container/Layer';
20import { CartesianLabelListContextProvider, LabelListFromLabelProp } from '../component/LabelList';
21import { getPercentValue, interpolate } from '../util/DataUtils';
22import { getValueByDataKey } from '../util/ChartUtils';
23import { adaptEventsOfChild } from '../util/types';
24import { FunnelTrapezoid } from '../util/FunnelUtils';
25import { useMouseClickItemDispatch, useMouseEnterItemDispatch, useMouseLeaveItemDispatch } from '../context/tooltipContext';
26import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
27import { selectFunnelTrapezoids } from '../state/selectors/funnelSelectors';
28import { findAllByType } from '../util/ReactUtils';
29import { Cell } from '../component/Cell';
30import { resolveDefaultProps } from '../util/resolveDefaultProps';
31import { usePlotArea } from '../hooks';
32import { svgPropertiesNoEvents } from '../util/svgPropertiesNoEvents';
33import { JavascriptAnimate } from '../animation/JavascriptAnimate';
34import { useAnimationId } from '../util/useAnimationId';
35import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
36
37/**
38 * Internal props, combination of external props + defaultProps + private Recharts state
39 */
40
41/**
42 * External props, intended for end users to fill in
43 */
44
45var SetFunnelTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
46 var {
47 dataKey,
48 nameKey,
49 stroke,
50 strokeWidth,
51 fill,
52 name,
53 hide,
54 tooltipType,
55 data,
56 trapezoids,
57 id
58 } = _ref;
59 var tooltipEntrySettings = {
60 dataDefinedOnItem: data,
61 getPosition: index => {
62 var _trapezoids$Number;
63 return (_trapezoids$Number = trapezoids[Number(index)]) === null || _trapezoids$Number === void 0 ? void 0 : _trapezoids$Number.tooltipPosition;
64 },
65 settings: {
66 stroke,
67 strokeWidth,
68 fill,
69 dataKey,
70 name,
71 nameKey,
72 hide,
73 type: tooltipType,
74 color: fill,
75 unit: '',
76 // Funnel does not have unit, why?
77 graphicalItemId: id
78 }
79 };
80 return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
81 tooltipEntrySettings: tooltipEntrySettings
82 });
83});
84function FunnelLabelListProvider(_ref2) {
85 var {
86 showLabels,
87 trapezoids,
88 children
89 } = _ref2;
90 var labelListEntries = useMemo(() => {
91 if (!showLabels) {
92 return undefined;
93 }
94 return trapezoids === null || trapezoids === void 0 ? void 0 : trapezoids.map(entry => {
95 var viewBox = entry.labelViewBox;
96 return _objectSpread(_objectSpread({}, viewBox), {}, {
97 value: entry.name,
98 payload: entry.payload,
99 parentViewBox: entry.parentViewBox,
100 viewBox,
101 fill: entry.fill
102 });
103 });
104 }, [showLabels, trapezoids]);
105 return /*#__PURE__*/React.createElement(CartesianLabelListContextProvider, {
106 value: labelListEntries
107 }, children);
108}
109function FunnelTrapezoids(props) {
110 var {
111 trapezoids,
112 allOtherFunnelProps
113 } = props;
114 var activeItemIndex = useAppSelector(state => selectActiveIndex(state, 'item', state.tooltip.settings.trigger, undefined));
115 var {
116 onMouseEnter: onMouseEnterFromProps,
117 onClick: onItemClickFromProps,
118 onMouseLeave: onMouseLeaveFromProps,
119 shape,
120 activeShape
121 } = allOtherFunnelProps,
122 restOfAllOtherProps = _objectWithoutProperties(allOtherFunnelProps, _excluded);
123 var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, allOtherFunnelProps.dataKey, allOtherFunnelProps.id);
124 var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps);
125 var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, allOtherFunnelProps.dataKey, allOtherFunnelProps.id);
126 return /*#__PURE__*/React.createElement(React.Fragment, null, trapezoids.map((entry, i) => {
127 var isActiveIndex = Boolean(activeShape) && activeItemIndex === String(i);
128 var trapezoidOptions = isActiveIndex ? activeShape : shape;
129 var _entry$option$isActiv = _objectSpread(_objectSpread({}, entry), {}, {
130 option: trapezoidOptions,
131 isActive: isActiveIndex,
132 stroke: entry.stroke
133 }),
134 {
135 id
136 } = _entry$option$isActiv,
137 trapezoidProps = _objectWithoutProperties(_entry$option$isActiv, _excluded2);
138 return /*#__PURE__*/React.createElement(Layer, _extends({
139 key: "trapezoid-".concat(entry === null || entry === void 0 ? void 0 : entry.x, "-").concat(entry === null || entry === void 0 ? void 0 : entry.y, "-").concat(entry === null || entry === void 0 ? void 0 : entry.name, "-").concat(entry === null || entry === void 0 ? void 0 : entry.value),
140 className: "recharts-funnel-trapezoid"
141 }, adaptEventsOfChild(restOfAllOtherProps, entry, i), {
142 // @ts-expect-error the types need a bit of attention
143 onMouseEnter: onMouseEnterFromContext(entry, i)
144 // @ts-expect-error the types need a bit of attention
145 ,
146 onMouseLeave: onMouseLeaveFromContext(entry, i)
147 // @ts-expect-error the types need a bit of attention
148 ,
149 onClick: onClickFromContext(entry, i)
150 }), /*#__PURE__*/React.createElement(FunnelTrapezoid, trapezoidProps));
151 }));
152}
153function TrapezoidsWithAnimation(_ref3) {
154 var {
155 previousTrapezoidsRef,
156 props
157 } = _ref3;
158 var {
159 trapezoids,
160 isAnimationActive,
161 animationBegin,
162 animationDuration,
163 animationEasing,
164 onAnimationEnd,
165 onAnimationStart
166 } = props;
167 var prevTrapezoids = previousTrapezoidsRef.current;
168 var [isAnimating, setIsAnimating] = useState(false);
169 var showLabels = !isAnimating;
170 var animationId = useAnimationId(trapezoids, 'recharts-funnel-');
171 var handleAnimationEnd = useCallback(() => {
172 if (typeof onAnimationEnd === 'function') {
173 onAnimationEnd();
174 }
175 setIsAnimating(false);
176 }, [onAnimationEnd]);
177 var handleAnimationStart = useCallback(() => {
178 if (typeof onAnimationStart === 'function') {
179 onAnimationStart();
180 }
181 setIsAnimating(true);
182 }, [onAnimationStart]);
183 return /*#__PURE__*/React.createElement(FunnelLabelListProvider, {
184 showLabels: showLabels,
185 trapezoids: trapezoids
186 }, /*#__PURE__*/React.createElement(JavascriptAnimate, {
187 animationId: animationId,
188 begin: animationBegin,
189 duration: animationDuration,
190 isActive: isAnimationActive,
191 easing: animationEasing,
192 key: animationId,
193 onAnimationStart: handleAnimationStart,
194 onAnimationEnd: handleAnimationEnd
195 }, t => {
196 var stepData = t === 1 ? trapezoids : trapezoids.map((entry, index) => {
197 var prev = prevTrapezoids && prevTrapezoids[index];
198 if (prev) {
199 return _objectSpread(_objectSpread({}, entry), {}, {
200 x: interpolate(prev.x, entry.x, t),
201 y: interpolate(prev.y, entry.y, t),
202 upperWidth: interpolate(prev.upperWidth, entry.upperWidth, t),
203 lowerWidth: interpolate(prev.lowerWidth, entry.lowerWidth, t),
204 height: interpolate(prev.height, entry.height, t)
205 });
206 }
207 return _objectSpread(_objectSpread({}, entry), {}, {
208 x: interpolate(entry.x + entry.upperWidth / 2, entry.x, t),
209 y: interpolate(entry.y + entry.height / 2, entry.y, t),
210 upperWidth: interpolate(0, entry.upperWidth, t),
211 lowerWidth: interpolate(0, entry.lowerWidth, t),
212 height: interpolate(0, entry.height, t)
213 });
214 });
215 if (t > 0) {
216 // eslint-disable-next-line no-param-reassign
217 previousTrapezoidsRef.current = stepData;
218 }
219 return /*#__PURE__*/React.createElement(Layer, null, /*#__PURE__*/React.createElement(FunnelTrapezoids, {
220 trapezoids: stepData,
221 allOtherFunnelProps: props
222 }));
223 }), /*#__PURE__*/React.createElement(LabelListFromLabelProp, {
224 label: props.label
225 }), props.children);
226}
227function RenderTrapezoids(props) {
228 var previousTrapezoidsRef = useRef(undefined);
229 return /*#__PURE__*/React.createElement(TrapezoidsWithAnimation, {
230 props: props,
231 previousTrapezoidsRef: previousTrapezoidsRef
232 });
233}
234var getRealWidthHeight = (customWidth, offset) => {
235 var {
236 width,
237 height,
238 left,
239 top
240 } = offset;
241 var realWidth = getPercentValue(customWidth, width, width);
242 return {
243 realWidth,
244 realHeight: height,
245 offsetX: left,
246 offsetY: top
247 };
248};
249export var defaultFunnelProps = {
250 animationBegin: 400,
251 animationDuration: 1500,
252 animationEasing: 'ease',
253 fill: '#808080',
254 hide: false,
255 isAnimationActive: 'auto',
256 lastShapeType: 'triangle',
257 legendType: 'rect',
258 nameKey: 'name',
259 reversed: false,
260 stroke: '#fff'
261};
262function FunnelImpl(props) {
263 var plotArea = usePlotArea();
264 var {
265 stroke,
266 fill,
267 legendType,
268 hide,
269 isAnimationActive,
270 animationBegin,
271 animationDuration,
272 animationEasing,
273 nameKey,
274 lastShapeType,
275 id
276 } = props,
277 everythingElse = _objectWithoutProperties(props, _excluded3);
278 var presentationProps = svgPropertiesNoEvents(props);
279 var cells = findAllByType(props.children, Cell);
280 var funnelSettings = useMemo(() => ({
281 dataKey: props.dataKey,
282 nameKey,
283 data: props.data,
284 tooltipType: props.tooltipType,
285 lastShapeType,
286 reversed: props.reversed,
287 customWidth: props.width,
288 cells,
289 presentationProps,
290 id
291 }), [props.dataKey, nameKey, props.data, props.tooltipType, lastShapeType, props.reversed, props.width, cells, presentationProps, id]);
292 var trapezoids = useAppSelector(state => selectFunnelTrapezoids(state, funnelSettings));
293 if (hide || !trapezoids || !trapezoids.length || !plotArea) {
294 return null;
295 }
296 var {
297 height,
298 width
299 } = plotArea;
300 var layerClass = clsx('recharts-trapezoids', props.className);
301 return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetFunnelTooltipEntrySettings, {
302 dataKey: props.dataKey,
303 nameKey: props.nameKey,
304 stroke: props.stroke,
305 strokeWidth: props.strokeWidth,
306 fill: props.fill,
307 name: props.name,
308 hide: props.hide,
309 tooltipType: props.tooltipType,
310 data: props.data,
311 trapezoids: trapezoids,
312 id: id
313 }), /*#__PURE__*/React.createElement(Layer, {
314 className: layerClass
315 }, /*#__PURE__*/React.createElement(RenderTrapezoids, _extends({}, everythingElse, {
316 id: id,
317 stroke: stroke,
318 fill: fill,
319 nameKey: nameKey,
320 lastShapeType: lastShapeType,
321 animationBegin: animationBegin,
322 animationDuration: animationDuration,
323 animationEasing: animationEasing,
324 isAnimationActive: isAnimationActive,
325 hide: hide,
326 legendType: legendType,
327 height: height,
328 width: width,
329 trapezoids: trapezoids
330 }))));
331}
332export function computeFunnelTrapezoids(_ref4) {
333 var {
334 dataKey,
335 nameKey,
336 displayedData,
337 tooltipType,
338 lastShapeType,
339 reversed,
340 offset,
341 customWidth,
342 graphicalItemId
343 } = _ref4;
344 var {
345 realHeight,
346 realWidth,
347 offsetX,
348 offsetY
349 } = getRealWidthHeight(customWidth, offset);
350 var values = displayedData.map(entry => {
351 var val = getValueByDataKey(entry, dataKey, 0);
352 return typeof val === 'number' ? val : 0;
353 });
354 var maxValue = Math.max.apply(null, values);
355 var len = displayedData.length;
356 var rowHeight = realHeight / len;
357 var parentViewBox = {
358 x: offset.left,
359 y: offset.top,
360 width: offset.width,
361 height: offset.height
362 };
363 var trapezoids = displayedData.map((entry, i) => {
364 // @ts-expect-error getValueByDataKey does not validate the output type
365 var rawVal = getValueByDataKey(entry, dataKey, 0);
366 var name = String(getValueByDataKey(entry, nameKey, i));
367 var val = rawVal;
368 var nextVal;
369 if (i !== len - 1) {
370 var nextDataValue = getValueByDataKey(displayedData[i + 1], dataKey, 0);
371 if (typeof nextDataValue === 'number') {
372 nextVal = nextDataValue;
373 } else if (Array.isArray(nextDataValue)) {
374 var [first, second] = nextDataValue;
375 if (typeof first === 'number') {
376 val = first;
377 }
378 if (typeof second === 'number') {
379 nextVal = second;
380 }
381 }
382 } else if (rawVal instanceof Array && rawVal.length === 2) {
383 var [_first, _second] = rawVal;
384 if (typeof _first === 'number') {
385 val = _first;
386 }
387 if (typeof _second === 'number') {
388 nextVal = _second;
389 }
390 } else if (lastShapeType === 'rectangle') {
391 nextVal = val;
392 } else {
393 nextVal = 0;
394 }
395
396 // @ts-expect-error this is a problem if we have ranged values because `val` can be an array
397 var x = (maxValue - val) * realWidth / (2 * maxValue) + offsetX;
398 var y = rowHeight * i + offsetY;
399 // @ts-expect-error getValueByDataKey does not validate the output type
400 var upperWidth = val / maxValue * realWidth;
401 // @ts-expect-error nextVal could be an array
402 var lowerWidth = nextVal / maxValue * realWidth;
403 var tooltipPayload = [{
404 name,
405 value: val,
406 payload: entry,
407 dataKey,
408 type: tooltipType,
409 graphicalItemId
410 }];
411 var tooltipPosition = {
412 x: x + upperWidth / 2,
413 y: y + rowHeight / 2
414 };
415 var trapezoidViewBox = {
416 x,
417 y,
418 upperWidth,
419 lowerWidth,
420 width: Math.max(upperWidth, lowerWidth),
421 height: rowHeight
422 };
423 return _objectSpread(_objectSpread(_objectSpread({}, trapezoidViewBox), {}, {
424 name,
425 val,
426 tooltipPayload,
427 tooltipPosition
428 }, entry != null && typeof entry === 'object' ? omit(entry, ['width']) : {}), {}, {
429 payload: entry,
430 parentViewBox,
431 labelViewBox: trapezoidViewBox
432 });
433 });
434 if (reversed) {
435 trapezoids = trapezoids.map((entry, index) => {
436 var reversedViewBox = {
437 x: entry.x - (entry.lowerWidth - entry.upperWidth) / 2,
438 y: entry.y - index * rowHeight + (len - 1 - index) * rowHeight,
439 upperWidth: entry.lowerWidth,
440 lowerWidth: entry.upperWidth,
441 width: Math.max(entry.lowerWidth, entry.upperWidth),
442 height: rowHeight
443 };
444 return _objectSpread(_objectSpread(_objectSpread({}, entry), reversedViewBox), {}, {
445 tooltipPosition: _objectSpread(_objectSpread({}, entry.tooltipPosition), {}, {
446 y: entry.y - index * rowHeight + (len - 1 - index) * rowHeight + rowHeight / 2
447 }),
448 labelViewBox: reversedViewBox
449 });
450 });
451 }
452 return trapezoids;
453}
454
455/**
456 * @consumes CartesianViewBoxContext
457 * @provides LabelListContext
458 * @provides CellReader
459 */
460export function Funnel(outsideProps) {
461 var _resolveDefaultProps = resolveDefaultProps(outsideProps, defaultFunnelProps),
462 {
463 id: externalId
464 } = _resolveDefaultProps,
465 props = _objectWithoutProperties(_resolveDefaultProps, _excluded4);
466 return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
467 id: externalId,
468 type: "funnel"
469 }, id => /*#__PURE__*/React.createElement(FunnelImpl, _extends({}, props, {
470 id: id
471 })));
472}
473Funnel.displayName = 'Funnel';
Note: See TracBrowser for help on using the repository browser.