-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuse-axis-title-adjustments.ts
More file actions
53 lines (45 loc) · 1.52 KB
/
use-axis-title-adjustments.ts
File metadata and controls
53 lines (45 loc) · 1.52 KB
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
import { TITLE_VPADDING } from "@/charts/combo/combo-line-container";
import { TICK_PADDING } from "@/charts/shared/axis-height-linear";
import { TICK_FONT_SIZE } from "@/charts/shared/use-chart-theme";
import { getTextHeight, getTextWidth } from "@/utils/get-text-width";
export interface AxisTitleAdjustments {
axisTitleAdjustment: number;
topMarginAxisTitleAdjustment: number;
isOverlapping: boolean;
overlapAmount: number;
}
interface UseAxisTitleAdjustmentsProps {
leftAxisTitle: string;
rightAxisTitle: string;
containerWidth: number;
fontSize?: number;
}
export const useAxisTitleAdjustments = ({
leftAxisTitle,
rightAxisTitle,
containerWidth,
fontSize = TICK_FONT_SIZE,
}: UseAxisTitleAdjustmentsProps): AxisTitleAdjustments => {
const axisTitleWidthLeft =
getTextWidth(leftAxisTitle, { fontSize }) + TICK_PADDING;
const axisTitleWidthRight =
getTextWidth(rightAxisTitle, { fontSize }) + TICK_PADDING;
const isOverlapping =
axisTitleWidthLeft + axisTitleWidthRight > containerWidth;
const overlapAmount =
(axisTitleWidthLeft + axisTitleWidthRight) / containerWidth;
const axisLabelHeight = getTextHeight(leftAxisTitle, { fontSize });
const axisTitleAdjustment =
(isOverlapping
? axisLabelHeight * Math.ceil(overlapAmount)
: axisLabelHeight + TITLE_VPADDING) *
2 -
axisLabelHeight * 2;
const topMarginAxisTitleAdjustment = 60 + axisTitleAdjustment;
return {
axisTitleAdjustment,
topMarginAxisTitleAdjustment,
isOverlapping,
overlapAmount,
};
};