-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseLogTicks.ts
More file actions
95 lines (81 loc) · 2.82 KB
/
useLogTicks.ts
File metadata and controls
95 lines (81 loc) · 2.82 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
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
import type { ScaleContinuousNumeric } from 'd3-scale';
import type { RefObject } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { textDimensions } from '../utils.js';
type Directions = 'horizontal' | 'vertical';
export interface PrimaryLogTicks {
label: string;
position: number;
value: number;
}
interface Options {
tickFormat?: (d: number) => string;
minSpace?: number;
}
const TEST_HEIGHT = '+1234567890';
function isMainTick(value: number): number {
const index = value / 10 ** Math.round(Math.log10(value));
return Math.floor(index < 1 ? index * 10 : index);
}
function formatTicks(
ticks: number[],
scale: ScaleContinuousNumeric<number, number>,
format: (d: number) => string,
maxWordSpace: number,
minSpace: number,
): PrimaryLogTicks[] {
const scaledTicks = ticks.filter((val) => isMainTick(val) === 1).map(scale);
const mainTickSpace = Math.abs(
// TODO: `scaledTicks` might not have a length >1
(scaledTicks[0] as number) - (scaledTicks[1] as number),
);
const mainTickRatio = (maxWordSpace + minSpace) / mainTickSpace;
const mainTicksStep = mainTickRatio >= 1 ? Math.ceil(mainTickRatio) : 1;
let mainTickCounter = 0;
return ticks.map((value) => {
const position = scale(value);
let label = '';
if (isMainTick(value) === 1) {
label = mainTickCounter === 0 ? format(value) : '';
mainTickCounter = (mainTickCounter + 1) % mainTicksStep;
}
return { label, position, value };
});
}
export function useLogTicks(
scale: ScaleContinuousNumeric<number, number>,
direction: Directions,
ref: RefObject<SVGGElement | null>,
options: Options = {},
): PrimaryLogTicks[] {
const [maxStrSize, setMaxStrSize] = useState(40);
const range = scale.range();
if (!range) throw new Error('Range needs to be specified');
const domain = scale.domain();
if (!domain) throw new Error('Domain needs to be specified');
const { minSpace = 8 } = options;
const format = options?.tickFormat;
const tickFormat = useCallback(
(x: number) => (format ? format(x) : String(x)),
[format],
);
const ticks = useMemo(() => scale.ticks(), [scale]);
// Calculates the word density
useEffect(() => {
if (ref.current) {
if (direction === 'horizontal') {
const maxLenWord = ticks
.filter((val) => isMainTick(val) === 1)
.map(tickFormat)
.reduce((acc, curr) => (acc.length < curr.length ? curr : acc), '');
const { width } = textDimensions(maxLenWord, ref);
setMaxStrSize(Math.ceil(width));
} else {
const { height } = textDimensions(TEST_HEIGHT, ref);
setMaxStrSize(Math.ceil(height));
}
}
}, [direction, domain, tickFormat, ref, ticks]);
// Calculates the first paint density
return formatTicks(ticks, scale, tickFormat, maxStrSize, minSpace);
}