source: node_modules/recharts/lib/polar/PolarRadiusAxis.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: 11.4 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.PolarRadiusAxis = PolarRadiusAxis;
7exports.PolarRadiusAxisWrapper = void 0;
8var _react = _interopRequireWildcard(require("react"));
9var React = _react;
10var _maxBy = _interopRequireDefault(require("es-toolkit/compat/maxBy"));
11var _minBy = _interopRequireDefault(require("es-toolkit/compat/minBy"));
12var _clsx = require("clsx");
13var _Text = require("../component/Text");
14var _Label = require("../component/Label");
15var _Layer = require("../container/Layer");
16var _PolarUtils = require("../util/PolarUtils");
17var _types = require("../util/types");
18var _polarAxisSlice = require("../state/polarAxisSlice");
19var _hooks = require("../state/hooks");
20var _polarScaleSelectors = require("../state/selectors/polarScaleSelectors");
21var _polarAxisSelectors = require("../state/selectors/polarAxisSelectors");
22var _defaultPolarRadiusAxisProps = require("./defaultPolarRadiusAxisProps");
23var _svgPropertiesNoEvents = require("../util/svgPropertiesNoEvents");
24var _resolveDefaultProps = require("../util/resolveDefaultProps");
25var _ZIndexLayer = require("../zIndex/ZIndexLayer");
26var _chartLayoutContext = require("../context/chartLayoutContext");
27var _DataUtils = require("../util/DataUtils");
28var _getAxisTypeBasedOnLayout = require("../util/getAxisTypeBasedOnLayout");
29var _getClassNameFromUnknown = require("../util/getClassNameFromUnknown");
30var _excluded = ["type"],
31 _excluded2 = ["cx", "cy", "angle", "axisLine"],
32 _excluded3 = ["angle", "tickFormatter", "stroke", "tick"];
33function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
34function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
35function _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); }
36function 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; }
37function _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; }
38function _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; }
39function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
40function _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); }
41function _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; }
42function _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; }
43var AXIS_TYPE = 'radiusAxis';
44function SetRadiusAxisSettings(props) {
45 var dispatch = (0, _hooks.useAppDispatch)();
46 var layout = (0, _chartLayoutContext.usePolarChartLayout)();
47 var settings = (0, _react.useMemo)(() => {
48 var {
49 type: typeFromProps
50 } = props,
51 rest = _objectWithoutProperties(props, _excluded);
52 var evaluatedType = (0, _getAxisTypeBasedOnLayout.getAxisTypeBasedOnLayout)(layout, 'radiusAxis', typeFromProps);
53 if (evaluatedType == null) {
54 return undefined;
55 }
56 return _objectSpread(_objectSpread({}, rest), {}, {
57 type: evaluatedType
58 });
59 }, [props, layout]);
60 (0, _react.useEffect)(() => {
61 if (settings == null) {
62 return _DataUtils.noop;
63 }
64 dispatch((0, _polarAxisSlice.addRadiusAxis)(settings));
65 return () => {
66 dispatch((0, _polarAxisSlice.removeRadiusAxis)(settings));
67 };
68 }, [dispatch, settings]);
69 return null;
70}
71
72/**
73 * Calculate the coordinate of tick
74 * @param coordinate The radius of tick
75 * @param angle from props
76 * @param cx from chart
77 * @param cy from chart
78 * @return (x, y)
79 */
80var getTickValueCoord = (_ref, angle, cx, cy) => {
81 var {
82 coordinate
83 } = _ref;
84 return (0, _PolarUtils.polarToCartesian)(cx, cy, coordinate, angle);
85};
86var getTickTextAnchor = orientation => {
87 var textAnchor;
88 switch (orientation) {
89 case 'left':
90 textAnchor = 'end';
91 break;
92 case 'right':
93 textAnchor = 'start';
94 break;
95 default:
96 textAnchor = 'middle';
97 break;
98 }
99 return textAnchor;
100};
101var getViewBox = (angle, cx, cy, ticks) => {
102 var maxRadiusTick = (0, _maxBy.default)(ticks, entry => entry.coordinate || 0);
103 var minRadiusTick = (0, _minBy.default)(ticks, entry => entry.coordinate || 0);
104 return {
105 cx,
106 cy,
107 startAngle: angle,
108 endAngle: angle,
109 innerRadius: (minRadiusTick === null || minRadiusTick === void 0 ? void 0 : minRadiusTick.coordinate) || 0,
110 outerRadius: (maxRadiusTick === null || maxRadiusTick === void 0 ? void 0 : maxRadiusTick.coordinate) || 0,
111 clockWise: false
112 };
113};
114var renderAxisLine = (props, ticks) => {
115 var {
116 cx,
117 cy,
118 angle,
119 axisLine
120 } = props,
121 others = _objectWithoutProperties(props, _excluded2);
122 var extent = ticks.reduce((result, entry) => [Math.min(result[0], entry.coordinate), Math.max(result[1], entry.coordinate)], [Infinity, -Infinity]);
123 var point0 = (0, _PolarUtils.polarToCartesian)(cx, cy, extent[0], angle);
124 var point1 = (0, _PolarUtils.polarToCartesian)(cx, cy, extent[1], angle);
125 var axisLineProps = _objectSpread(_objectSpread(_objectSpread({}, (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(others)), {}, {
126 fill: 'none'
127 }, (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(axisLine)), {}, {
128 x1: point0.x,
129 y1: point0.y,
130 x2: point1.x,
131 y2: point1.y
132 });
133
134 // @ts-expect-error wrong SVG element type
135 return /*#__PURE__*/React.createElement("line", _extends({
136 className: "recharts-polar-radius-axis-line"
137 }, axisLineProps));
138};
139var renderTickItem = (option, tickProps, value) => {
140 var tickItem;
141 if (/*#__PURE__*/React.isValidElement(option)) {
142 tickItem = /*#__PURE__*/React.cloneElement(option, tickProps);
143 } else if (typeof option === 'function') {
144 tickItem = option(tickProps);
145 } else {
146 tickItem = /*#__PURE__*/React.createElement(_Text.Text, _extends({}, tickProps, {
147 className: "recharts-polar-radius-axis-tick-value"
148 }), value);
149 }
150 return tickItem;
151};
152var renderTicks = (props, ticks) => {
153 var {
154 angle,
155 tickFormatter,
156 stroke,
157 tick
158 } = props,
159 others = _objectWithoutProperties(props, _excluded3);
160 var textAnchor = getTickTextAnchor(props.orientation);
161 var axisProps = (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(others);
162 var customTickProps = (0, _svgPropertiesNoEvents.svgPropertiesNoEventsFromUnknown)(tick);
163 var items = ticks.map((entry, i) => {
164 var coord = getTickValueCoord(entry, props.angle, props.cx, props.cy);
165 var tickProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
166 textAnchor,
167 transform: "rotate(".concat(90 - angle, ", ").concat(coord.x, ", ").concat(coord.y, ")")
168 }, axisProps), {}, {
169 stroke: 'none',
170 fill: stroke
171 }, customTickProps), {}, {
172 index: i
173 }, coord), {}, {
174 payload: entry
175 });
176 return /*#__PURE__*/React.createElement(_Layer.Layer, _extends({
177 className: (0, _clsx.clsx)('recharts-polar-radius-axis-tick', (0, _getClassNameFromUnknown.getClassNameFromUnknown)(tick)),
178 key: "tick-".concat(entry.coordinate)
179 }, (0, _types.adaptEventsOfChild)(props, entry, i)), renderTickItem(tick, tickProps, tickFormatter ? tickFormatter(entry.value, i) : entry.value));
180 });
181 return /*#__PURE__*/React.createElement(_Layer.Layer, {
182 className: "recharts-polar-radius-axis-ticks"
183 }, items);
184};
185var PolarRadiusAxisWrapper = defaultsAndInputs => {
186 var {
187 radiusAxisId
188 } = defaultsAndInputs;
189 var viewBox = (0, _hooks.useAppSelector)(_polarAxisSelectors.selectPolarViewBox);
190 var scale = (0, _hooks.useAppSelector)(state => (0, _polarScaleSelectors.selectPolarAxisScale)(state, 'radiusAxis', radiusAxisId));
191 var ticks = (0, _hooks.useAppSelector)(state => (0, _polarScaleSelectors.selectPolarAxisTicks)(state, 'radiusAxis', radiusAxisId, false));
192 if (viewBox == null || !ticks || !ticks.length || scale == null) {
193 return null;
194 }
195 var props = _objectSpread(_objectSpread({}, defaultsAndInputs), {}, {
196 scale
197 }, viewBox);
198 var {
199 tick,
200 axisLine
201 } = props;
202 return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
203 zIndex: props.zIndex
204 }, /*#__PURE__*/React.createElement(_Layer.Layer, {
205 className: (0, _clsx.clsx)('recharts-polar-radius-axis', AXIS_TYPE, props.className)
206 }, axisLine && renderAxisLine(props, ticks), tick && renderTicks(props, ticks), /*#__PURE__*/React.createElement(_Label.PolarLabelContextProvider, getViewBox(props.angle, props.cx, props.cy, ticks), /*#__PURE__*/React.createElement(_Label.PolarLabelFromLabelProp, {
207 label: props.label
208 }), props.children)));
209};
210
211/**
212 * @provides PolarLabelContext
213 * @consumes PolarViewBoxContext
214 */
215exports.PolarRadiusAxisWrapper = PolarRadiusAxisWrapper;
216function PolarRadiusAxis(outsideProps) {
217 var props = (0, _resolveDefaultProps.resolveDefaultProps)(outsideProps, _defaultPolarRadiusAxisProps.defaultPolarRadiusAxisProps);
218 return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetRadiusAxisSettings, {
219 domain: props.domain,
220 id: props.radiusAxisId,
221 scale: props.scale,
222 type: props.type,
223 dataKey: props.dataKey,
224 unit: undefined,
225 name: props.name,
226 allowDuplicatedCategory: props.allowDuplicatedCategory,
227 allowDataOverflow: props.allowDataOverflow,
228 reversed: props.reversed,
229 includeHidden: props.includeHidden,
230 allowDecimals: props.allowDecimals
231 // @ts-expect-error the type does not match. Is RadiusAxis really expecting what it says?
232 ,
233 ticks: props.ticks,
234 tickCount: props.tickCount,
235 tick: props.tick
236 }), /*#__PURE__*/React.createElement(PolarRadiusAxisWrapper, props));
237}
238PolarRadiusAxis.displayName = 'PolarRadiusAxis';
Note: See TracBrowser for help on using the repository browser.