UNPKG

4.74 kBJavaScriptView Raw
1import { getSectorPath } from './graphics';
2import { isBetween } from './helper';
3import { BBox } from './bbox';
4/**
5 * @ignore
6 * Gets x dimension length
7 * @param coordinate
8 * @returns x dimension length
9 */
10export function getXDimensionLength(coordinate) {
11 if (coordinate.isPolar && !coordinate.isTransposed) {
12 // 极坐标系下 width 为弧长
13 return (coordinate.endAngle - coordinate.startAngle) * coordinate.getRadius();
14 }
15 // 直角坐标系
16 var start = coordinate.convert({ x: 0, y: 0 });
17 var end = coordinate.convert({ x: 1, y: 0 });
18 // 坐标系有可能发生 transpose 等变换,所有通过两点之间的距离进行计算
19 return Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
20}
21/**
22 * @ignore
23 * Determines whether full circle is
24 * @param coordinate
25 * @returns true if full circle
26 */
27export function isFullCircle(coordinate) {
28 if (coordinate.isPolar) {
29 var startAngle = coordinate.startAngle, endAngle = coordinate.endAngle;
30 return endAngle - startAngle === Math.PI * 2;
31 }
32 return false;
33}
34/**
35 * @ignore
36 * 获取当前点到坐标系圆心的距离
37 * @param coordinate 坐标系
38 * @param point 当前点
39 * @returns distance to center
40 */
41export function getDistanceToCenter(coordinate, point) {
42 var center = coordinate.getCenter();
43 return Math.sqrt(Math.pow((point.x - center.x), 2) + Math.pow((point.y - center.y), 2));
44}
45/**
46 * @ignore
47 * 坐标点是否在坐标系中
48 * @param coordinate
49 * @param point
50 */
51export function isPointInCoordinate(coordinate, point) {
52 var result = false;
53 if (coordinate) {
54 if (coordinate.type === 'theta') {
55 var start = coordinate.start, end = coordinate.end;
56 result = isBetween(point.x, start.x, end.x) && isBetween(point.y, start.y, end.y);
57 }
58 else {
59 var invertPoint = coordinate.invert(point);
60 result = isBetween(invertPoint.x, 0, 1) && isBetween(invertPoint.y, 0, 1);
61 }
62 }
63 return result;
64}
65/**
66 * @ignore
67 * 获取点到圆心的连线与水平方向的夹角
68 */
69export function getAngleByPoint(coordinate, point) {
70 var center = coordinate.getCenter();
71 return Math.atan2(point.y - center.y, point.x - center.x);
72}
73/**
74 * @ignore
75 * 获取同坐标系范围相同的剪切区域
76 * @param coordinate
77 * @returns
78 */
79export function getCoordinateClipCfg(coordinate, margin) {
80 if (margin === void 0) { margin = 0; }
81 var start = coordinate.start, end = coordinate.end;
82 var width = coordinate.getWidth();
83 var height = coordinate.getHeight();
84 if (coordinate.isPolar) {
85 var startAngle_1 = coordinate.startAngle, endAngle_1 = coordinate.endAngle;
86 var center_1 = coordinate.getCenter();
87 var radius_1 = coordinate.getRadius();
88 return {
89 type: 'path',
90 startState: {
91 path: getSectorPath(center_1.x, center_1.y, radius_1 + margin, startAngle_1, startAngle_1),
92 },
93 endState: function (ratio) {
94 var diff = (endAngle_1 - startAngle_1) * ratio + startAngle_1;
95 var path = getSectorPath(center_1.x, center_1.y, radius_1 + margin, startAngle_1, diff);
96 return {
97 path: path,
98 };
99 },
100 attrs: {
101 path: getSectorPath(center_1.x, center_1.y, radius_1 + margin, startAngle_1, endAngle_1),
102 },
103 };
104 }
105 var endState;
106 if (coordinate.isTransposed) {
107 endState = {
108 height: height + margin * 2,
109 };
110 }
111 else {
112 endState = {
113 width: width + margin * 2,
114 };
115 }
116 return {
117 type: 'rect',
118 startState: {
119 x: start.x - margin,
120 y: end.y - margin,
121 width: coordinate.isTransposed ? width + margin * 2 : 0,
122 height: coordinate.isTransposed ? 0 : height + margin * 2,
123 },
124 endState: endState,
125 attrs: {
126 x: start.x - margin,
127 y: end.y - margin,
128 width: width + margin * 2,
129 height: height + margin * 2,
130 },
131 };
132}
133/**
134 * 获取坐标系范围的 BBox
135 * @param coordinate
136 * @param margin
137 */
138export function getCoordinateBBox(coordinate, margin) {
139 if (margin === void 0) { margin = 0; }
140 var start = coordinate.start, end = coordinate.end;
141 var width = coordinate.getWidth();
142 var height = coordinate.getHeight();
143 var minX = Math.min(start.x, end.x);
144 var minY = Math.min(start.y, end.y);
145 return BBox.fromRange(minX - margin, minY - margin, minX + width + margin, minY + height + margin);
146}
147//# sourceMappingURL=coordinate.js.map
\No newline at end of file