source: node_modules/recharts/es6/cartesian/getCartesianPosition.js@ ba17441

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

Added visualizations

  • Property mode set to 100644
File size: 7.5 KB
Line 
1function 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; }
2function _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; }
3function _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; }
4function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5function _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); }
6import { getPercentValue, isNumber, isPercent } from '../util/DataUtils';
7import { cartesianViewBoxToTrapezoid } from '../context/chartLayoutContext';
8/**
9 * Calculates the position and alignment for a generic element in a Cartesian coordinate system.
10 *
11 * @param options - The options including viewBox, position, and offset.
12 * @returns The calculated x, y, alignment and size.
13 */
14export var getCartesianPosition = options => {
15 var {
16 viewBox,
17 position,
18 offset = 0,
19 parentViewBox: parentViewBoxFromOptions,
20 clamp
21 } = options;
22 var {
23 x,
24 y,
25 height,
26 upperWidth,
27 lowerWidth
28 } = cartesianViewBoxToTrapezoid(viewBox);
29
30 // Funnel.tsx provides a viewBox where `x` is the top-left of the trapezoid shape.
31 var upperX = x;
32 // The trapezoid is centered, so we can calculate the other corners from the top-left.
33 var lowerX = x + (upperWidth - lowerWidth) / 2;
34 // middleX is the x-coordinate of the left edge at the vertical midpoint of the trapezoid.
35 var middleX = (upperX + lowerX) / 2;
36 // The width of the trapezoid at its vertical midpoint.
37 var midHeightWidth = (upperWidth + lowerWidth) / 2;
38 // The center x-coordinate is constant for the entire height of the trapezoid.
39 var centerX = upperX + upperWidth / 2;
40
41 // Define vertical offsets and position inverts based on the value being positive or negative.
42 // This allows labels to be positioned correctly for bars with negative height.
43 var verticalSign = height >= 0 ? 1 : -1;
44 var verticalOffset = verticalSign * offset;
45 var verticalEnd = verticalSign > 0 ? 'end' : 'start';
46 var verticalStart = verticalSign > 0 ? 'start' : 'end';
47
48 // Define horizontal offsets and position inverts based on the value being positive or negative.
49 // This allows labels to be positioned correctly for bars with negative width.
50 var horizontalSign = upperWidth >= 0 ? 1 : -1;
51 var horizontalOffset = horizontalSign * offset;
52 var horizontalEnd = horizontalSign > 0 ? 'end' : 'start';
53 var horizontalStart = horizontalSign > 0 ? 'start' : 'end';
54
55 // We assume parentViewBox is generic if provided.
56 // The user has asserted that parentViewBox will be CartesianViewBoxRequired if present.
57 var parentViewBox = parentViewBoxFromOptions;
58 if (position === 'top') {
59 var result = {
60 x: upperX + upperWidth / 2,
61 y: y - verticalOffset,
62 horizontalAnchor: 'middle',
63 verticalAnchor: verticalEnd
64 };
65 if (clamp && parentViewBox) {
66 result.height = Math.max(y - parentViewBox.y, 0);
67 result.width = upperWidth;
68 }
69 return result;
70 }
71 if (position === 'bottom') {
72 var _result = {
73 x: lowerX + lowerWidth / 2,
74 y: y + height + verticalOffset,
75 horizontalAnchor: 'middle',
76 verticalAnchor: verticalStart
77 };
78 if (clamp && parentViewBox) {
79 _result.height = Math.max(parentViewBox.y + parentViewBox.height - (y + height), 0);
80 _result.width = lowerWidth;
81 }
82 return _result;
83 }
84 if (position === 'left') {
85 var _result2 = {
86 x: middleX - horizontalOffset,
87 y: y + height / 2,
88 horizontalAnchor: horizontalEnd,
89 verticalAnchor: 'middle'
90 };
91 if (clamp && parentViewBox) {
92 _result2.width = Math.max(_result2.x - parentViewBox.x, 0);
93 _result2.height = height;
94 }
95 return _result2;
96 }
97 if (position === 'right') {
98 var _result3 = {
99 x: middleX + midHeightWidth + horizontalOffset,
100 y: y + height / 2,
101 horizontalAnchor: horizontalStart,
102 verticalAnchor: 'middle'
103 };
104 if (clamp && parentViewBox) {
105 _result3.width = Math.max(parentViewBox.x + parentViewBox.width - _result3.x, 0);
106 _result3.height = height;
107 }
108 return _result3;
109 }
110 var sizeAttrs = clamp && parentViewBox ? {
111 width: midHeightWidth,
112 height
113 } : {};
114 if (position === 'insideLeft') {
115 return _objectSpread({
116 x: middleX + horizontalOffset,
117 y: y + height / 2,
118 horizontalAnchor: horizontalStart,
119 verticalAnchor: 'middle'
120 }, sizeAttrs);
121 }
122 if (position === 'insideRight') {
123 return _objectSpread({
124 x: middleX + midHeightWidth - horizontalOffset,
125 y: y + height / 2,
126 horizontalAnchor: horizontalEnd,
127 verticalAnchor: 'middle'
128 }, sizeAttrs);
129 }
130 if (position === 'insideTop') {
131 return _objectSpread({
132 x: upperX + upperWidth / 2,
133 y: y + verticalOffset,
134 horizontalAnchor: 'middle',
135 verticalAnchor: verticalStart
136 }, sizeAttrs);
137 }
138 if (position === 'insideBottom') {
139 return _objectSpread({
140 x: lowerX + lowerWidth / 2,
141 y: y + height - verticalOffset,
142 horizontalAnchor: 'middle',
143 verticalAnchor: verticalEnd
144 }, sizeAttrs);
145 }
146 if (position === 'insideTopLeft') {
147 return _objectSpread({
148 x: upperX + horizontalOffset,
149 y: y + verticalOffset,
150 horizontalAnchor: horizontalStart,
151 verticalAnchor: verticalStart
152 }, sizeAttrs);
153 }
154 if (position === 'insideTopRight') {
155 return _objectSpread({
156 x: upperX + upperWidth - horizontalOffset,
157 y: y + verticalOffset,
158 horizontalAnchor: horizontalEnd,
159 verticalAnchor: verticalStart
160 }, sizeAttrs);
161 }
162 if (position === 'insideBottomLeft') {
163 return _objectSpread({
164 x: lowerX + horizontalOffset,
165 y: y + height - verticalOffset,
166 horizontalAnchor: horizontalStart,
167 verticalAnchor: verticalEnd
168 }, sizeAttrs);
169 }
170 if (position === 'insideBottomRight') {
171 return _objectSpread({
172 x: lowerX + lowerWidth - horizontalOffset,
173 y: y + height - verticalOffset,
174 horizontalAnchor: horizontalEnd,
175 verticalAnchor: verticalEnd
176 }, sizeAttrs);
177 }
178 if (!!position && typeof position === 'object' && (isNumber(position.x) || isPercent(position.x)) && (isNumber(position.y) || isPercent(position.y))) {
179 // TODO: This is not quite right. The width of the trapezoid changes with y.
180 // A percentage-based x should be relative to the width at that y.
181 // For now, we use the mid-height width as a reasonable approximation.
182 return _objectSpread({
183 x: x + getPercentValue(position.x, midHeightWidth),
184 y: y + getPercentValue(position.y, height),
185 horizontalAnchor: 'end',
186 verticalAnchor: 'end'
187 }, sizeAttrs);
188 }
189 return _objectSpread({
190 x: centerX,
191 y: y + height / 2,
192 horizontalAnchor: 'middle',
193 verticalAnchor: 'middle'
194 }, sizeAttrs);
195};
Note: See TracBrowser for help on using the repository browser.