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