source: node_modules/recharts/es6/polar/Radar.js@ a762898

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

Added visualizations

  • Property mode set to 100644
File size: 16.0 KB
Line 
1var _excluded = ["id"];
2function _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); }
3function 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; }
4function _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; }
5function _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; }
6function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
7function _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); }
8function _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; }
9function _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; }
10import * as React from 'react';
11import { useCallback, useRef, useState } from 'react';
12import last from 'es-toolkit/compat/last';
13import { clsx } from 'clsx';
14import { interpolate, isNullish, noop } from '../util/DataUtils';
15import { polarToCartesian } from '../util/PolarUtils';
16import { getTooltipNameProp, getValueByDataKey } from '../util/ChartUtils';
17import { Polygon } from '../shape/Polygon';
18import { Layer } from '../container/Layer';
19import { CartesianLabelListContextProvider, LabelListFromLabelProp } from '../component/LabelList';
20import { Dots } from '../component/Dots';
21import { ActivePoints } from '../component/ActivePoints';
22import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
23import { selectRadarPoints } from '../state/selectors/radarSelectors';
24import { useAppSelector } from '../state/hooks';
25import { useIsPanorama } from '../context/PanoramaContext';
26import { SetPolarLegendPayload } from '../state/SetLegendPayload';
27import { useAnimationId } from '../util/useAnimationId';
28import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
29import { SetPolarGraphicalItem } from '../state/SetGraphicalItem';
30import { svgPropertiesNoEvents } from '../util/svgPropertiesNoEvents';
31import { JavascriptAnimate } from '../animation/JavascriptAnimate';
32import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents';
33import { resolveDefaultProps } from '../util/resolveDefaultProps';
34import { ZIndexLayer } from '../zIndex/ZIndexLayer';
35import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
36function getLegendItemColor(stroke, fill) {
37 return stroke && stroke !== 'none' ? stroke : fill;
38}
39var computeLegendPayloadFromRadarSectors = props => {
40 var {
41 dataKey,
42 name,
43 stroke,
44 fill,
45 legendType,
46 hide
47 } = props;
48 return [{
49 inactive: hide,
50 dataKey,
51 type: legendType,
52 color: getLegendItemColor(stroke, fill),
53 value: getTooltipNameProp(name, dataKey),
54 payload: props
55 }];
56};
57var SetRadarTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
58 var {
59 dataKey,
60 stroke,
61 strokeWidth,
62 fill,
63 name,
64 hide,
65 tooltipType,
66 id
67 } = _ref;
68 var tooltipEntrySettings = {
69 /*
70 * I suppose this here _could_ return props.points
71 * because while Radar does not support item tooltip mode, it _could_ support it.
72 * But when I actually do return the points here, a defaultIndex test starts failing.
73 * So, undefined it is.
74 */
75 dataDefinedOnItem: undefined,
76 getPosition: noop,
77 settings: {
78 stroke,
79 strokeWidth,
80 fill,
81 nameKey: undefined,
82 // RadarChart does not have nameKey unfortunately
83 dataKey,
84 name: getTooltipNameProp(name, dataKey),
85 hide,
86 type: tooltipType,
87 color: getLegendItemColor(stroke, fill),
88 unit: '',
89 // why doesn't Radar support unit?
90 graphicalItemId: id
91 }
92 };
93 return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
94 tooltipEntrySettings: tooltipEntrySettings
95 });
96});
97function RadarDotsWrapper(_ref2) {
98 var {
99 points,
100 props
101 } = _ref2;
102 var {
103 dot,
104 dataKey
105 } = props;
106 var {
107 id
108 } = props,
109 propsWithoutId = _objectWithoutProperties(props, _excluded);
110 var baseProps = svgPropertiesNoEvents(propsWithoutId);
111 return /*#__PURE__*/React.createElement(Dots, {
112 points: points,
113 dot: dot,
114 className: "recharts-radar-dots",
115 dotClassName: "recharts-radar-dot",
116 dataKey: dataKey,
117 baseProps: baseProps
118 });
119}
120export function computeRadarPoints(_ref3) {
121 var {
122 radiusAxis,
123 angleAxis,
124 displayedData,
125 dataKey,
126 bandSize
127 } = _ref3;
128 var {
129 cx,
130 cy
131 } = angleAxis;
132 var isRange = false;
133 var points = [];
134 var angleBandSize = angleAxis.type !== 'number' ? bandSize !== null && bandSize !== void 0 ? bandSize : 0 : 0;
135 displayedData.forEach((entry, i) => {
136 var _angleAxis$scale$map, _radiusAxis$scale$map;
137 var name = getValueByDataKey(entry, angleAxis.dataKey, i);
138 var value = getValueByDataKey(entry, dataKey);
139 var angle = ((_angleAxis$scale$map = angleAxis.scale.map(name)) !== null && _angleAxis$scale$map !== void 0 ? _angleAxis$scale$map : 0) + angleBandSize;
140 var pointValue = Array.isArray(value) ? last(value) : value;
141 var radius = isNullish(pointValue) ? 0 : (_radiusAxis$scale$map = radiusAxis.scale.map(pointValue)) !== null && _radiusAxis$scale$map !== void 0 ? _radiusAxis$scale$map : 0;
142 if (Array.isArray(value) && value.length >= 2) {
143 isRange = true;
144 }
145 points.push(_objectSpread(_objectSpread({}, polarToCartesian(cx, cy, radius, angle)), {}, {
146 // @ts-expect-error getValueByDataKey does not validate the output type
147 name,
148 // @ts-expect-error getValueByDataKey does not validate the output type
149 value,
150 cx,
151 cy,
152 radius,
153 angle,
154 payload: entry
155 }));
156 });
157 var baseLinePoints = [];
158 if (isRange) {
159 points.forEach(point => {
160 if (Array.isArray(point.value)) {
161 var _radiusAxis$scale$map2;
162 var baseValue = point.value[0];
163 var radius = isNullish(baseValue) ? 0 : (_radiusAxis$scale$map2 = radiusAxis.scale.map(baseValue)) !== null && _radiusAxis$scale$map2 !== void 0 ? _radiusAxis$scale$map2 : 0;
164 baseLinePoints.push(_objectSpread(_objectSpread({}, point), {}, {
165 radius
166 }, polarToCartesian(cx, cy, radius, point.angle)));
167 } else {
168 baseLinePoints.push(point);
169 }
170 });
171 }
172 return {
173 points,
174 isRange,
175 baseLinePoints
176 };
177}
178function RadarLabelListProvider(_ref4) {
179 var {
180 showLabels,
181 points,
182 children
183 } = _ref4;
184 /*
185 * Radar provides a Cartesian label list context. Do we want to also provide a polar label list context?
186 * That way, users can choose to use polar positions for the Radar labels.
187 */
188 // const labelListEntries: ReadonlyArray<PolarLabelListEntry> = points.map(
189 // (point): PolarLabelListEntry => ({
190 // value: point.value,
191 // payload: point.payload,
192 // parentViewBox: undefined,
193 // clockWise: false,
194 // viewBox: {
195 // cx: point.cx,
196 // cy: point.cy,
197 // innerRadius: point.radius,
198 // outerRadius: point.radius,
199 // startAngle: point.angle,
200 // endAngle: point.angle,
201 // clockWise: false,
202 // },
203 // }),
204 // );
205
206 var labelListEntries = points.map(point => {
207 var _point$value;
208 var viewBox = {
209 x: point.x,
210 y: point.y,
211 width: 0,
212 lowerWidth: 0,
213 upperWidth: 0,
214 height: 0
215 };
216 return _objectSpread(_objectSpread({}, viewBox), {}, {
217 value: (_point$value = point.value) !== null && _point$value !== void 0 ? _point$value : '',
218 payload: point.payload,
219 parentViewBox: undefined,
220 viewBox,
221 fill: undefined
222 });
223 });
224 return /*#__PURE__*/React.createElement(CartesianLabelListContextProvider, {
225 value: showLabels ? labelListEntries : undefined
226 }, children);
227}
228function StaticPolygon(_ref5) {
229 var {
230 points,
231 baseLinePoints,
232 props
233 } = _ref5;
234 if (points == null) {
235 return null;
236 }
237 var {
238 shape,
239 isRange,
240 connectNulls
241 } = props;
242 var handleMouseEnter = e => {
243 var {
244 onMouseEnter
245 } = props;
246 if (onMouseEnter) {
247 onMouseEnter(props, e);
248 }
249 };
250 var handleMouseLeave = e => {
251 var {
252 onMouseLeave
253 } = props;
254 if (onMouseLeave) {
255 onMouseLeave(props, e);
256 }
257 };
258 var radar;
259 if (/*#__PURE__*/React.isValidElement(shape)) {
260 radar = /*#__PURE__*/React.cloneElement(shape, _objectSpread(_objectSpread({}, props), {}, {
261 points
262 }));
263 } else if (typeof shape === 'function') {
264 radar = shape(_objectSpread(_objectSpread({}, props), {}, {
265 points
266 }));
267 } else {
268 radar = /*#__PURE__*/React.createElement(Polygon, _extends({}, svgPropertiesAndEvents(props), {
269 onMouseEnter: handleMouseEnter,
270 onMouseLeave: handleMouseLeave,
271 points: points,
272 baseLinePoints: isRange ? baseLinePoints : undefined,
273 connectNulls: connectNulls
274 }));
275 }
276 return /*#__PURE__*/React.createElement(Layer, {
277 className: "recharts-radar-polygon"
278 }, radar, /*#__PURE__*/React.createElement(RadarDotsWrapper, {
279 props: props,
280 points: points
281 }));
282}
283var interpolatePolarPoint = (prevPoints, prevPointsDiffFactor, t) => (entry, index) => {
284 var prev = prevPoints && prevPoints[Math.floor(index * prevPointsDiffFactor)];
285 if (prev) {
286 return _objectSpread(_objectSpread({}, entry), {}, {
287 x: interpolate(prev.x, entry.x, t),
288 y: interpolate(prev.y, entry.y, t)
289 });
290 }
291 return _objectSpread(_objectSpread({}, entry), {}, {
292 x: interpolate(entry.cx, entry.x, t),
293 y: interpolate(entry.cy, entry.y, t)
294 });
295};
296function PolygonWithAnimation(_ref6) {
297 var {
298 props,
299 previousPointsRef,
300 previousBaseLinePointsRef
301 } = _ref6;
302 var {
303 points,
304 baseLinePoints,
305 isAnimationActive,
306 animationBegin,
307 animationDuration,
308 animationEasing,
309 onAnimationEnd,
310 onAnimationStart
311 } = props;
312 var prevPoints = previousPointsRef.current;
313 var prevBaseLinePoints = previousBaseLinePointsRef.current;
314 var prevPointsDiffFactor = prevPoints ? prevPoints.length / points.length : 1;
315 var prevBaseLinePointsDiffFactor = prevBaseLinePoints ? prevBaseLinePoints.length / baseLinePoints.length : 1;
316 var animationId = useAnimationId(props, 'recharts-radar-');
317 var [isAnimating, setIsAnimating] = useState(false);
318 var showLabels = !isAnimating;
319 var handleAnimationEnd = useCallback(() => {
320 if (typeof onAnimationEnd === 'function') {
321 onAnimationEnd();
322 }
323 setIsAnimating(false);
324 }, [onAnimationEnd]);
325 var handleAnimationStart = useCallback(() => {
326 if (typeof onAnimationStart === 'function') {
327 onAnimationStart();
328 }
329 setIsAnimating(true);
330 }, [onAnimationStart]);
331 return /*#__PURE__*/React.createElement(RadarLabelListProvider, {
332 showLabels: showLabels,
333 points: points
334 }, /*#__PURE__*/React.createElement(JavascriptAnimate, {
335 animationId: animationId,
336 begin: animationBegin,
337 duration: animationDuration,
338 isActive: isAnimationActive,
339 easing: animationEasing,
340 key: "radar-".concat(animationId),
341 onAnimationEnd: handleAnimationEnd,
342 onAnimationStart: handleAnimationStart
343 }, t => {
344 var stepData = t === 1 ? points : points.map(interpolatePolarPoint(prevPoints, prevPointsDiffFactor, t));
345 var stepBaseLinePoints = t === 1 ? baseLinePoints : baseLinePoints === null || baseLinePoints === void 0 ? void 0 : baseLinePoints.map(interpolatePolarPoint(prevBaseLinePoints, prevBaseLinePointsDiffFactor, t));
346 if (t > 0) {
347 // eslint-disable-next-line no-param-reassign
348 previousPointsRef.current = stepData;
349 // eslint-disable-next-line no-param-reassign
350 previousBaseLinePointsRef.current = stepBaseLinePoints;
351 }
352 return /*#__PURE__*/React.createElement(StaticPolygon, {
353 points: stepData,
354 baseLinePoints: stepBaseLinePoints,
355 props: props
356 });
357 }), /*#__PURE__*/React.createElement(LabelListFromLabelProp, {
358 label: props.label
359 }), props.children);
360}
361function RenderPolygon(props) {
362 var previousPointsRef = useRef(undefined);
363 var previousBaseLinePointsRef = useRef(undefined);
364 return /*#__PURE__*/React.createElement(PolygonWithAnimation, {
365 props: props,
366 previousPointsRef: previousPointsRef,
367 previousBaseLinePointsRef: previousBaseLinePointsRef
368 });
369}
370export var defaultRadarProps = {
371 activeDot: true,
372 angleAxisId: 0,
373 animationBegin: 0,
374 animationDuration: 1500,
375 animationEasing: 'ease',
376 dot: false,
377 hide: false,
378 isAnimationActive: 'auto',
379 label: false,
380 legendType: 'rect',
381 radiusAxisId: 0,
382 zIndex: DefaultZIndexes.area
383};
384function RadarWithState(props) {
385 var {
386 hide,
387 className,
388 points
389 } = props;
390 if (hide) {
391 return null;
392 }
393 var layerClass = clsx('recharts-radar', className);
394 return /*#__PURE__*/React.createElement(ZIndexLayer, {
395 zIndex: props.zIndex
396 }, /*#__PURE__*/React.createElement(Layer, {
397 className: layerClass
398 }, /*#__PURE__*/React.createElement(RenderPolygon, props)), /*#__PURE__*/React.createElement(ActivePoints, {
399 points: points,
400 mainColor: getLegendItemColor(props.stroke, props.fill),
401 itemDataKey: props.dataKey,
402 activeDot: props.activeDot
403 }));
404}
405function RadarImpl(props) {
406 var isPanorama = useIsPanorama();
407 var radarPoints = useAppSelector(state => selectRadarPoints(state, props.radiusAxisId, props.angleAxisId, isPanorama, props.id));
408 if ((radarPoints === null || radarPoints === void 0 ? void 0 : radarPoints.points) == null) {
409 return null;
410 }
411 return /*#__PURE__*/React.createElement(RadarWithState, _extends({}, props, {
412 points: radarPoints === null || radarPoints === void 0 ? void 0 : radarPoints.points,
413 baseLinePoints: radarPoints === null || radarPoints === void 0 ? void 0 : radarPoints.baseLinePoints,
414 isRange: radarPoints === null || radarPoints === void 0 ? void 0 : radarPoints.isRange
415 }));
416}
417
418/**
419 * @consumes PolarChartContext
420 * @provides LabelListContext
421 */
422export function Radar(outsideProps) {
423 var props = resolveDefaultProps(outsideProps, defaultRadarProps);
424 return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
425 id: props.id,
426 type: "radar"
427 }, id => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetPolarGraphicalItem, {
428 type: "radar",
429 id: id,
430 data: undefined // Radar does not have data prop, why?
431 ,
432 dataKey: props.dataKey,
433 hide: props.hide,
434 angleAxisId: props.angleAxisId,
435 radiusAxisId: props.radiusAxisId
436 }), /*#__PURE__*/React.createElement(SetPolarLegendPayload, {
437 legendPayload: computeLegendPayloadFromRadarSectors(props)
438 }), /*#__PURE__*/React.createElement(SetRadarTooltipEntrySettings, {
439 dataKey: props.dataKey,
440 stroke: props.stroke,
441 strokeWidth: props.strokeWidth,
442 fill: props.fill,
443 name: props.name,
444 hide: props.hide,
445 tooltipType: props.tooltipType,
446 id: id
447 }), /*#__PURE__*/React.createElement(RadarImpl, _extends({}, props, {
448 id: id
449 }))));
450}
451Radar.displayName = 'Radar';
Note: See TracBrowser for help on using the repository browser.