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