UNPKG

6.36 kBJavaScriptView Raw
1import { deepMix, get, isBoolean } from '@antv/util';
2import { DIRECTION } from '../constant';
3import { getName } from './scale';
4import { vec2 } from '@antv/matrix-util';
5/**
6 * @ignore
7 * get axis relative region ( 0 ~ 1) by direction when coordinate is rect
8 * @param direction
9 * @returns axis coordinate region
10 */
11export function getLineAxisRelativeRegion(direction) {
12 var start;
13 var end;
14 switch (direction) {
15 case DIRECTION.TOP:
16 start = { x: 0, y: 1 };
17 end = { x: 1, y: 1 };
18 break;
19 case DIRECTION.RIGHT:
20 start = { x: 1, y: 0 };
21 end = { x: 1, y: 1 };
22 break;
23 case DIRECTION.BOTTOM:
24 start = { x: 0, y: 0 };
25 end = { x: 1, y: 0 };
26 break;
27 case DIRECTION.LEFT:
28 start = { x: 0, y: 0 };
29 end = { x: 0, y: 1 };
30 break;
31 default:
32 start = end = { x: 0, y: 0 };
33 }
34 return { start: start, end: end };
35}
36/**
37 * @ignore
38 * get axis relative region ( 0 ~ 1) by direction when coordinate is polar
39 * @param coordinate
40 * @returns axis coordinate region
41 */
42export function getCircleAxisRelativeRegion(coordinate) {
43 var start;
44 var end;
45 if (coordinate.isTransposed) {
46 start = {
47 x: 0,
48 y: 0,
49 };
50 end = {
51 x: 1,
52 y: 0,
53 };
54 }
55 else {
56 start = {
57 x: 0,
58 y: 0,
59 };
60 end = {
61 x: 0,
62 y: 1,
63 };
64 }
65 return { start: start, end: end };
66}
67/**
68 * @ignore
69 * get the axis region from coordinate
70 * @param coordinate
71 * @param direction
72 * @returns the axis region (start point, end point)
73 */
74export function getAxisRegion(coordinate, direction) {
75 var region = { start: { x: 0, y: 0 }, end: { x: 0, y: 0 } };
76 if (coordinate.isRect) {
77 region = getLineAxisRelativeRegion(direction);
78 }
79 else if (coordinate.isPolar) {
80 region = getCircleAxisRelativeRegion(coordinate);
81 }
82 var start = region.start, end = region.end;
83 return {
84 start: coordinate.convert(start),
85 end: coordinate.convert(end),
86 };
87}
88/**
89 * @ignore
90 * get axis factor
91 * @param coordinate
92 * @param direction
93 * @returns factor
94 */
95export function getAxisFactor(coordinate, direction) {
96 // rect coordinate, by direction
97 if (coordinate.isRect) {
98 return coordinate.isTransposed
99 ? [DIRECTION.RIGHT, DIRECTION.BOTTOM].includes(direction)
100 ? 1
101 : -1
102 : [DIRECTION.BOTTOM, DIRECTION.RIGHT].includes(direction)
103 ? -1
104 : 1;
105 }
106 // polar y axis, by angle
107 if (coordinate.isPolar) {
108 var startAngle = coordinate.x.start;
109 return startAngle < 0 ? -1 : 1;
110 }
111 return 1;
112}
113/**
114 * @ignore
115 * whether the axis isVertical
116 * @param region
117 * @returns isVertical
118 */
119export function isVertical(region) {
120 var start = region.start, end = region.end;
121 return start.x === end.x;
122}
123/**
124 * @ignore
125 * get factor by region (real position)
126 * @param region
127 * @param center
128 * @returns factor
129 */
130export function getAxisFactorByRegion(region, center) {
131 var start = region.start, end = region.end;
132 var isAxisVertical = isVertical(region);
133 // 垂直
134 if (isAxisVertical) {
135 // 左方,从下到上、右方,从上到下
136 if ((start.y - end.y) * (center.x - start.x) > 0) {
137 return 1;
138 }
139 else {
140 return -1;
141 }
142 }
143 else {
144 // 下方,从左到右、上方,从右到做
145 if ((end.x - start.x) * (start.y - center.y) > 0) {
146 return -1;
147 }
148 else {
149 return 1;
150 }
151 }
152}
153/**
154 * @ignore
155 * get the axis cfg from theme, will mix the common cfg of legend theme
156 *
157 * @param theme view theme object
158 * @param direction axis direction
159 * @returns axis theme cfg
160 */
161export function getAxisThemeCfg(theme, direction) {
162 var axisTheme = get(theme, ['components', 'axis'], {});
163 return deepMix({}, get(axisTheme, ['common'], {}), deepMix({}, get(axisTheme, [direction], {})));
164}
165/**
166 * get the options of axis title,mix the cfg from theme, avoid common themeCfg not work
167 * @param theme
168 * @param direction
169 * @param axisOptions
170 * @returns axis title options
171 */
172export function getAxisTitleOptions(theme, direction, axisOptions) {
173 var axisTheme = get(theme, ['components', 'axis'], {});
174 return deepMix({}, get(axisTheme, ['common', 'title'], {}), deepMix({}, get(axisTheme, [direction, 'title'], {})), axisOptions);
175}
176/**
177 * @ignore
178 * get circle axis center and radius
179 * @param coordinate
180 */
181export function getCircleAxisCenterRadius(coordinate) {
182 // @ts-ignore
183 var x = coordinate.x, y = coordinate.y, center = coordinate.circleCenter;
184 var isReflectY = y.start > y.end;
185 var start = coordinate.isTransposed
186 ? coordinate.convert({
187 x: isReflectY ? 0 : 1,
188 y: 0,
189 })
190 : coordinate.convert({
191 x: 0,
192 y: isReflectY ? 0 : 1,
193 });
194 var startVector = [start.x - center.x, start.y - center.y];
195 var normalVector = [1, 0];
196 var startAngle = start.y > center.y ? vec2.angle(startVector, normalVector) : vec2.angle(startVector, normalVector) * -1;
197 var endAngle = startAngle + (x.end - x.start);
198 var radius = Math.sqrt(Math.pow((start.x - center.x), 2) + Math.pow((start.y - center.y), 2));
199 return {
200 center: center,
201 radius: radius,
202 startAngle: startAngle,
203 endAngle: endAngle,
204 };
205}
206/**
207 * @ignore
208 * 从配置中获取单个字段的 axis 配置
209 * @param axes
210 * @param field
211 * @returns the axis option of field
212 */
213export function getAxisOption(axes, field) {
214 if (isBoolean(axes)) {
215 return axes === false ? false : {};
216 }
217 return get(axes, [field]);
218}
219/**
220 * @ignore
221 * 如果配置了 position,则使用配置
222 * @param axisOption
223 * @param def
224 */
225export function getAxisDirection(axisOption, def) {
226 return get(axisOption, 'position', def);
227}
228/**
229 * 获取 axis 的 title 文本
230 * @param scale
231 * @param axisOption
232 */
233export function getAxisTitleText(scale, axisOption) {
234 return get(axisOption, ['title', 'text'], getName(scale));
235}
236//# sourceMappingURL=axis.js.map
\No newline at end of file