| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.PolarAngleAxis = PolarAngleAxis;
|
|---|
| 7 | exports.PolarAngleAxisWrapper = void 0;
|
|---|
| 8 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 9 | var React = _react;
|
|---|
| 10 | var _clsx = require("clsx");
|
|---|
| 11 | var _Layer = require("../container/Layer");
|
|---|
| 12 | var _Dot = require("../shape/Dot");
|
|---|
| 13 | var _Polygon = require("../shape/Polygon");
|
|---|
| 14 | var _Text = require("../component/Text");
|
|---|
| 15 | var _types = require("../util/types");
|
|---|
| 16 | var _PolarUtils = require("../util/PolarUtils");
|
|---|
| 17 | var _polarAxisSlice = require("../state/polarAxisSlice");
|
|---|
| 18 | var _hooks = require("../state/hooks");
|
|---|
| 19 | var _polarScaleSelectors = require("../state/selectors/polarScaleSelectors");
|
|---|
| 20 | var _polarAxisSelectors = require("../state/selectors/polarAxisSelectors");
|
|---|
| 21 | var _defaultPolarAngleAxisProps = require("./defaultPolarAngleAxisProps");
|
|---|
| 22 | var _PanoramaContext = require("../context/PanoramaContext");
|
|---|
| 23 | var _svgPropertiesNoEvents = require("../util/svgPropertiesNoEvents");
|
|---|
| 24 | var _resolveDefaultProps = require("../util/resolveDefaultProps");
|
|---|
| 25 | var _ZIndexLayer = require("../zIndex/ZIndexLayer");
|
|---|
| 26 | var _chartLayoutContext = require("../context/chartLayoutContext");
|
|---|
| 27 | var _DataUtils = require("../util/DataUtils");
|
|---|
| 28 | var _getAxisTypeBasedOnLayout = require("../util/getAxisTypeBasedOnLayout");
|
|---|
| 29 | var _getClassNameFromUnknown = require("../util/getClassNameFromUnknown");
|
|---|
| 30 | var _excluded = ["children", "type"];
|
|---|
| 31 | function _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); }
|
|---|
| 32 | 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); }
|
|---|
| 33 | 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; }
|
|---|
| 34 | 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; }
|
|---|
| 35 | 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; }
|
|---|
| 36 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 37 | 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); }
|
|---|
| 38 | 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; }
|
|---|
| 39 | 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; }
|
|---|
| 40 | var eps = 1e-5;
|
|---|
| 41 | var COS_45 = Math.cos((0, _PolarUtils.degreeToRadian)(45));
|
|---|
| 42 | var AXIS_TYPE = 'angleAxis';
|
|---|
| 43 | function SetAngleAxisSettings(props) {
|
|---|
| 44 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 45 | var layout = (0, _chartLayoutContext.usePolarChartLayout)();
|
|---|
| 46 | var settings = (0, _react.useMemo)(() => {
|
|---|
| 47 | var {
|
|---|
| 48 | children,
|
|---|
| 49 | type: typeFromProps
|
|---|
| 50 | } = props,
|
|---|
| 51 | rest = _objectWithoutProperties(props, _excluded);
|
|---|
| 52 | var evaluatedType = (0, _getAxisTypeBasedOnLayout.getAxisTypeBasedOnLayout)(layout, 'angleAxis', typeFromProps);
|
|---|
| 53 | if (evaluatedType == null) {
|
|---|
| 54 | return undefined;
|
|---|
| 55 | }
|
|---|
| 56 | return _objectSpread(_objectSpread({}, rest), {}, {
|
|---|
| 57 | type: evaluatedType
|
|---|
| 58 | });
|
|---|
| 59 | }, [props, layout]);
|
|---|
| 60 | var synchronizedSettings = (0, _hooks.useAppSelector)(state => (0, _polarAxisSelectors.selectAngleAxis)(state, settings === null || settings === void 0 ? void 0 : settings.id));
|
|---|
| 61 | var settingsAreSynchronized = settings === synchronizedSettings;
|
|---|
| 62 | (0, _react.useEffect)(() => {
|
|---|
| 63 | if (settings == null) {
|
|---|
| 64 | return _DataUtils.noop;
|
|---|
| 65 | }
|
|---|
| 66 | dispatch((0, _polarAxisSlice.addAngleAxis)(settings));
|
|---|
| 67 | return () => {
|
|---|
| 68 | dispatch((0, _polarAxisSlice.removeAngleAxis)(settings));
|
|---|
| 69 | };
|
|---|
| 70 | }, [dispatch, settings]);
|
|---|
| 71 | if (settingsAreSynchronized) {
|
|---|
| 72 | return props.children;
|
|---|
| 73 | }
|
|---|
| 74 | return null;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Calculate the coordinate of line endpoint
|
|---|
| 79 | * @param data The data if there are ticks
|
|---|
| 80 | * @param props axis settings
|
|---|
| 81 | * @return (x1, y1): The point close to text,
|
|---|
| 82 | * (x2, y2): The point close to axis
|
|---|
| 83 | */
|
|---|
| 84 | var getTickLineCoord = (data, props) => {
|
|---|
| 85 | var {
|
|---|
| 86 | cx,
|
|---|
| 87 | cy,
|
|---|
| 88 | radius,
|
|---|
| 89 | orientation,
|
|---|
| 90 | tickSize
|
|---|
| 91 | } = props;
|
|---|
| 92 | var tickLineSize = tickSize || 8;
|
|---|
| 93 | var p1 = (0, _PolarUtils.polarToCartesian)(cx, cy, radius, data.coordinate);
|
|---|
| 94 | var p2 = (0, _PolarUtils.polarToCartesian)(cx, cy, radius + (orientation === 'inner' ? -1 : 1) * tickLineSize, data.coordinate);
|
|---|
| 95 | return {
|
|---|
| 96 | x1: p1.x,
|
|---|
| 97 | y1: p1.y,
|
|---|
| 98 | x2: p2.x,
|
|---|
| 99 | y2: p2.y
|
|---|
| 100 | };
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Get the text-anchor of each tick
|
|---|
| 105 | * @param data Data of ticks
|
|---|
| 106 | * @param orientation of the axis ticks
|
|---|
| 107 | * @return text-anchor
|
|---|
| 108 | */
|
|---|
| 109 | var getTickTextAnchor = (data, orientation) => {
|
|---|
| 110 | var cos = Math.cos((0, _PolarUtils.degreeToRadian)(-data.coordinate));
|
|---|
| 111 | if (cos > eps) {
|
|---|
| 112 | return orientation === 'outer' ? 'start' : 'end';
|
|---|
| 113 | }
|
|---|
| 114 | if (cos < -eps) {
|
|---|
| 115 | return orientation === 'outer' ? 'end' : 'start';
|
|---|
| 116 | }
|
|---|
| 117 | return 'middle';
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Get the text vertical anchor of each tick
|
|---|
| 122 | * @param data Data of a tick
|
|---|
| 123 | * @return text vertical anchor
|
|---|
| 124 | */
|
|---|
| 125 | var getTickTextVerticalAnchor = data => {
|
|---|
| 126 | var cos = Math.cos((0, _PolarUtils.degreeToRadian)(-data.coordinate));
|
|---|
| 127 | var sin = Math.sin((0, _PolarUtils.degreeToRadian)(-data.coordinate));
|
|---|
| 128 |
|
|---|
| 129 | // handle top and bottom sectors: 90±45deg and 270±45deg
|
|---|
| 130 | if (Math.abs(cos) <= COS_45) {
|
|---|
| 131 | // sin > 0: top sector, sin < 0: bottom sector
|
|---|
| 132 | return sin > 0 ? 'start' : 'end';
|
|---|
| 133 | }
|
|---|
| 134 | return 'middle';
|
|---|
| 135 | };
|
|---|
| 136 | var AxisLine = props => {
|
|---|
| 137 | var {
|
|---|
| 138 | cx,
|
|---|
| 139 | cy,
|
|---|
| 140 | radius,
|
|---|
| 141 | axisLineType,
|
|---|
| 142 | axisLine,
|
|---|
| 143 | ticks
|
|---|
| 144 | } = props;
|
|---|
| 145 | if (!axisLine) {
|
|---|
| 146 | return null;
|
|---|
| 147 | }
|
|---|
| 148 | var axisLineProps = _objectSpread(_objectSpread({}, (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(props)), {}, {
|
|---|
| 149 | fill: 'none'
|
|---|
| 150 | }, (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(axisLine));
|
|---|
| 151 | if (axisLineType === 'circle') {
|
|---|
| 152 | // @ts-expect-error wrong SVG element type
|
|---|
| 153 | return /*#__PURE__*/React.createElement(_Dot.Dot, _extends({
|
|---|
| 154 | className: "recharts-polar-angle-axis-line"
|
|---|
| 155 | }, axisLineProps, {
|
|---|
| 156 | cx: cx,
|
|---|
| 157 | cy: cy,
|
|---|
| 158 | r: radius
|
|---|
| 159 | }));
|
|---|
| 160 | }
|
|---|
| 161 | var points = ticks.map(entry => (0, _PolarUtils.polarToCartesian)(cx, cy, radius, entry.coordinate));
|
|---|
| 162 |
|
|---|
| 163 | // @ts-expect-error wrong SVG element type
|
|---|
| 164 | return /*#__PURE__*/React.createElement(_Polygon.Polygon, _extends({
|
|---|
| 165 | className: "recharts-polar-angle-axis-line"
|
|---|
| 166 | }, axisLineProps, {
|
|---|
| 167 | points: points
|
|---|
| 168 | }));
|
|---|
| 169 | };
|
|---|
| 170 | var TickItemText = _ref => {
|
|---|
| 171 | var {
|
|---|
| 172 | tick,
|
|---|
| 173 | tickProps,
|
|---|
| 174 | value
|
|---|
| 175 | } = _ref;
|
|---|
| 176 | if (!tick) {
|
|---|
| 177 | return null;
|
|---|
| 178 | }
|
|---|
| 179 | if (/*#__PURE__*/React.isValidElement(tick)) {
|
|---|
| 180 | return /*#__PURE__*/React.cloneElement(tick, tickProps);
|
|---|
| 181 | }
|
|---|
| 182 | if (typeof tick === 'function') {
|
|---|
| 183 | return tick(tickProps);
|
|---|
| 184 | }
|
|---|
| 185 | return /*#__PURE__*/React.createElement(_Text.Text, _extends({}, tickProps, {
|
|---|
| 186 | className: "recharts-polar-angle-axis-tick-value"
|
|---|
| 187 | }), value);
|
|---|
| 188 | };
|
|---|
| 189 | var Ticks = props => {
|
|---|
| 190 | var {
|
|---|
| 191 | tick,
|
|---|
| 192 | tickLine,
|
|---|
| 193 | tickFormatter,
|
|---|
| 194 | stroke,
|
|---|
| 195 | ticks
|
|---|
| 196 | } = props;
|
|---|
| 197 | var axisProps = (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(props);
|
|---|
| 198 | var customTickProps = (0, _svgPropertiesNoEvents.svgPropertiesNoEventsFromUnknown)(tick);
|
|---|
| 199 | var tickLineProps = _objectSpread(_objectSpread({}, axisProps), {}, {
|
|---|
| 200 | fill: 'none'
|
|---|
| 201 | }, (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(tickLine));
|
|---|
| 202 | var items = ticks.map((entry, i) => {
|
|---|
| 203 | var lineCoord = getTickLineCoord(entry, props);
|
|---|
| 204 | var textAnchor = getTickTextAnchor(entry, props.orientation);
|
|---|
| 205 | var verticalAnchor = getTickTextVerticalAnchor(entry);
|
|---|
| 206 | var tickProps = _objectSpread(_objectSpread(_objectSpread({}, axisProps), {}, {
|
|---|
| 207 | // @ts-expect-error customTickProps is contributing unknown props
|
|---|
| 208 | textAnchor,
|
|---|
| 209 | verticalAnchor,
|
|---|
| 210 | // @ts-expect-error customTickProps is contributing unknown props
|
|---|
| 211 | stroke: 'none',
|
|---|
| 212 | // @ts-expect-error customTickProps is contributing unknown props
|
|---|
| 213 | fill: stroke
|
|---|
| 214 | }, customTickProps), {}, {
|
|---|
| 215 | index: i,
|
|---|
| 216 | payload: entry,
|
|---|
| 217 | x: lineCoord.x2,
|
|---|
| 218 | y: lineCoord.y2
|
|---|
| 219 | });
|
|---|
| 220 | return /*#__PURE__*/React.createElement(_Layer.Layer, _extends({
|
|---|
| 221 | className: (0, _clsx.clsx)('recharts-polar-angle-axis-tick', (0, _getClassNameFromUnknown.getClassNameFromUnknown)(tick)),
|
|---|
| 222 | key: "tick-".concat(entry.coordinate)
|
|---|
| 223 | }, (0, _types.adaptEventsOfChild)(props, entry, i)), tickLine && /*#__PURE__*/React.createElement("line", _extends({
|
|---|
| 224 | className: "recharts-polar-angle-axis-tick-line"
|
|---|
| 225 | }, tickLineProps, lineCoord)), /*#__PURE__*/React.createElement(TickItemText, {
|
|---|
| 226 | tick: tick,
|
|---|
| 227 | tickProps: tickProps,
|
|---|
| 228 | value: tickFormatter ? tickFormatter(entry.value, i) : entry.value
|
|---|
| 229 | }));
|
|---|
| 230 | });
|
|---|
| 231 | return /*#__PURE__*/React.createElement(_Layer.Layer, {
|
|---|
| 232 | className: "recharts-polar-angle-axis-ticks"
|
|---|
| 233 | }, items);
|
|---|
| 234 | };
|
|---|
| 235 | var PolarAngleAxisWrapper = defaultsAndInputs => {
|
|---|
| 236 | var {
|
|---|
| 237 | angleAxisId
|
|---|
| 238 | } = defaultsAndInputs;
|
|---|
| 239 | var viewBox = (0, _hooks.useAppSelector)(_polarAxisSelectors.selectPolarViewBox);
|
|---|
| 240 | var scale = (0, _hooks.useAppSelector)(state => (0, _polarScaleSelectors.selectPolarAxisScale)(state, 'angleAxis', angleAxisId));
|
|---|
| 241 | var isPanorama = (0, _PanoramaContext.useIsPanorama)();
|
|---|
| 242 | var ticks = (0, _hooks.useAppSelector)(state => (0, _polarScaleSelectors.selectPolarAngleAxisTicks)(state, 'angleAxis', angleAxisId, isPanorama));
|
|---|
| 243 | if (viewBox == null || !ticks || !ticks.length || scale == null) {
|
|---|
| 244 | return null;
|
|---|
| 245 | }
|
|---|
| 246 | var props = _objectSpread(_objectSpread(_objectSpread({}, defaultsAndInputs), {}, {
|
|---|
| 247 | scale
|
|---|
| 248 | }, viewBox), {}, {
|
|---|
| 249 | radius: viewBox.outerRadius,
|
|---|
| 250 | ticks
|
|---|
| 251 | });
|
|---|
| 252 | return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
|
|---|
| 253 | zIndex: props.zIndex
|
|---|
| 254 | }, /*#__PURE__*/React.createElement(_Layer.Layer, {
|
|---|
| 255 | className: (0, _clsx.clsx)('recharts-polar-angle-axis', AXIS_TYPE, props.className)
|
|---|
| 256 | }, /*#__PURE__*/React.createElement(AxisLine, props), /*#__PURE__*/React.createElement(Ticks, props)));
|
|---|
| 257 | };
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * @provides PolarLabelContext
|
|---|
| 261 | * @consumes PolarViewBoxContext
|
|---|
| 262 | */
|
|---|
| 263 | exports.PolarAngleAxisWrapper = PolarAngleAxisWrapper;
|
|---|
| 264 | function PolarAngleAxis(outsideProps) {
|
|---|
| 265 | var props = (0, _resolveDefaultProps.resolveDefaultProps)(outsideProps, _defaultPolarAngleAxisProps.defaultPolarAngleAxisProps);
|
|---|
| 266 | return /*#__PURE__*/React.createElement(SetAngleAxisSettings, {
|
|---|
| 267 | id: props.angleAxisId,
|
|---|
| 268 | scale: props.scale,
|
|---|
| 269 | type: props.type,
|
|---|
| 270 | dataKey: props.dataKey,
|
|---|
| 271 | unit: undefined,
|
|---|
| 272 | name: props.name,
|
|---|
| 273 | allowDuplicatedCategory: false // Ignoring the prop on purpose because axis calculation behaves as if it was false and Tooltip requires it to be true.
|
|---|
| 274 | ,
|
|---|
| 275 | allowDataOverflow: false,
|
|---|
| 276 | reversed: props.reversed,
|
|---|
| 277 | includeHidden: false,
|
|---|
| 278 | allowDecimals: props.allowDecimals,
|
|---|
| 279 | tickCount: props.tickCount
|
|---|
| 280 | // @ts-expect-error the type does not match. Is RadiusAxis really expecting what it says?
|
|---|
| 281 | ,
|
|---|
| 282 | ticks: props.ticks,
|
|---|
| 283 | tick: props.tick,
|
|---|
| 284 | domain: props.domain
|
|---|
| 285 | }, /*#__PURE__*/React.createElement(PolarAngleAxisWrapper, props));
|
|---|
| 286 | }
|
|---|
| 287 | PolarAngleAxis.displayName = 'PolarAngleAxis'; |
|---|