-
Notifications
You must be signed in to change notification settings - Fork 33
/
getTicks.ts
48 lines (43 loc) · 1.18 KB
/
getTicks.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
import {scaleUtc} from 'd3-scale'
import {ticks} from 'd3-array'
import {Formatter, FormatterType} from '../types'
export const getTicks = (
domain: number[],
rangeLength: number,
charLength: number,
formatter?: Formatter
): number[] => {
const sampleTick = formatter(domain[1])
const numTicks = getNumTicks(sampleTick, rangeLength, charLength)
switch (formatter._GIRAFFE_FORMATTER_TYPE) {
case FormatterType.Time:
return getTimeTicks(domain, rangeLength, numTicks)
default:
return ticks(domain[0], domain[1], numTicks)
}
}
const getNumTicks = (
sampleTick: string,
length: number,
charLength: number
): number => {
const sampleTickWidth = sampleTick.length * charLength
return Math.round(length / sampleTickWidth)
}
const getTimeTicks = (
[d0, d1]: number[],
length: number,
numTicks: number
): number[] => {
const results = scaleUtc()
.domain([d0, d1])
.range([0, length])
.ticks(numTicks)
.map(d => d.getTime())
if (results.length > numTicks) {
// added this to force D3 to use the numTicks since
// D3 treats the numTicks as a suggestion rather than a source
return results.slice(0, numTicks)
}
return results
}