UNPKG

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