-
Notifications
You must be signed in to change notification settings - Fork 346
/
size.axis.ts
175 lines (146 loc) 路 4.74 KB
/
size.axis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {isNumber} from "../../module/util";
import type {AxisType} from "../../../types/types";
export default {
/**
* Get Axis size according its position
* @param {string} id Axis id value - x, y or y2
* @returns {number} size Axis size value
* @private
*/
getAxisSize(id: AxisType): number {
const $$ = this;
const isRotated = $$.config.axis_rotated;
return (isRotated && id === "x") || (!isRotated && /y2?/.test(id)) ?
$$.getAxisWidthByAxisId(id, true) :
$$.getHorizontalAxisHeight(id);
},
getAxisWidthByAxisId(id: AxisType, withoutRecompute?: boolean): number {
const $$ = this;
if ($$.axis) {
const position = $$.axis?.getLabelPositionById(id);
const {width} = $$.axis.getMaxTickSize(id, withoutRecompute);
const gap = width === 0 ? 0.5 : 0;
return width + (
$$.config.padding?.mode === "fit" ?
position.isInner ? (10 + gap) : 10 :
position.isInner ? (20 + gap) : 40
);
} else {
return 40;
}
},
getHorizontalAxisHeight(id: AxisType): number {
const $$ = this;
const {config, state} = $$;
const {current, rotatedPadding, isLegendRight, isLegendInset} = state;
const isRotated = config.axis_rotated;
const isFitPadding = config.padding?.mode === "fit";
const isInner = config[`axis_${id}_inner`];
const hasLabelText = config[`axis_${id}_label`].text;
const defaultHeight = 13;
let h = config.padding?.mode === "fit" ? (isInner && !hasLabelText ? (id === "y" ? 1 : 0) : 20) : 30;
if (id === "x" && !config.axis_x_show) {
return 8;
}
if (id === "x" && isNumber(config.axis_x_height)) {
return config.axis_x_height;
}
if (id === "y" && !config.axis_y_show) {
return config.legend_show &&
!isLegendRight &&
!isLegendInset ? 10 : 1;
}
if (id === "y2" && !config.axis_y2_show) {
return isFitPadding ? 0 : rotatedPadding.top;
}
const maxtickSize = $$.axis.getMaxTickSize(id);
const rotate = $$.getAxisTickRotate(id);
// Calculate x/y axis height when tick rotated
if (
((id === "x" && !isRotated) || (/y2?/.test(id) && isRotated)) && rotate
) {
h += maxtickSize.width *
Math.cos(Math.PI * (90 - Math.abs(rotate)) / 180);
if (!config.axis_x_tick_multiline && current.height) {
if (h > current.height / 2) {
h = current.height / 2;
}
}
} else if (maxtickSize.height > defaultHeight && config.legend_show) {
h += maxtickSize.height - defaultHeight;
}
return h +
($$.axis.getLabelPositionById(id).isInner ? 0 : 10) +
(id === "y2" && !isRotated ? -10 : 0);
},
getEventRectWidth(): number {
const $$ = this;
const {config, axis} = $$;
const isInverted = config.axis_x_inverted;
const tickInterval = axis.x.tickInterval();
return Math.max(0, isInverted ? Math.abs(tickInterval) : tickInterval);
},
/**
* Get axis tick test rotate value
* @param {string} id Axis id
* @returns {number} rotate value
* @private
*/
getAxisTickRotate(id: AxisType): number {
const $$ = this;
const {axis, config, state, $el} = $$;
let rotate = config[`axis_${id}_tick_rotate`];
if (id === "x") {
const allowedXAxisTypes = axis.isCategorized() || axis.isTimeSeries();
if (config.axis_x_tick_fit && allowedXAxisTypes) {
const xTickCount = config.axis_x_tick_count;
const currentXTicksLength = state.current.maxTickSize.x.ticks.length;
let tickCount = 0;
if (xTickCount) {
tickCount = xTickCount > currentXTicksLength ? currentXTicksLength : xTickCount;
} else if (currentXTicksLength) {
tickCount = currentXTicksLength;
}
if (tickCount !== state.axis.x.tickCount) {
const {targets} = $$.data;
state.axis.x.padding = $$.getXDomainPadding([
$$.getXDomainMinMax(targets, "min"),
$$.getXDomainMinMax(targets, "max")
], tickCount);
}
state.axis.x.tickCount = tickCount;
}
if ($el.svg &&
config.axis_x_tick_fit &&
!config.axis_x_tick_multiline &&
!config.axis_x_tick_culling &&
config.axis_x_tick_autorotate &&
allowedXAxisTypes
) {
rotate = $$.needToRotateXAxisTickTexts() ?
config.axis_x_tick_rotate : 0;
}
}
return rotate;
},
/**
* Check weather axis tick text needs to be rotated
* @returns {boolean}
* @private
*/
needToRotateXAxisTickTexts(): boolean {
const $$ = this;
const {state: {axis, current}} = $$;
const xAxisLength = current.width -
$$.getCurrentPaddingByDirection("left") - $$.getCurrentPaddingByDirection("right");
const tickCountWithPadding = axis.x.tickCount +
axis.x.padding.left + axis.x.padding.right;
const {width} = $$.axis.getMaxTickSize("x");
const tickLength = tickCountWithPadding ? xAxisLength / tickCountWithPadding : 0;
return width > tickLength;
}
};