-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg-helpers.ts
92 lines (75 loc) · 2.29 KB
/
svg-helpers.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
import { Pair, Matrix, ChartOptions } from "./types.ts"
export const transformToTime = (str: string) => new Date(str).getTime()
export const createDataNormalizer = (
[minT, maxT]: Pair,
[minY, maxY]: Pair,
{ L, W, B, H }: ChartOptions
) => ([t, y]: Pair): Pair => [
L + ((t - minT) / (maxT - minT)) * W,
B + ((y - minY) / (maxY - minY)) * H,
]
export const pair = (i: number) => (values: string[]): Pair => [
transformToTime(values[0]),
parseFloat(values[i]),
]
export const createGetSeries = (matrix: Matrix) => (col: number) =>
matrix.reduce(
($: Pair[], values: string[]): Pair[] => [...$, pair(col)(values)],
[]
)
export const createSeriesIdList = ({ length }: any[]) =>
Array.from(Array(length - 1), (_, i) => i + 1)
export const getRange = (arr: number[]): Pair => [
Math.min(...arr),
Math.max(...arr),
]
export const getXRange = (matrix: Matrix): Pair => {
const dates = matrix.map(([value]: string[]) => transformToTime(value))
return getRange(dates)
}
// TODO: clean it up
export const extendRange = ([minY, maxY]: Pair, norm: number = 1): Pair => {
const avgY = (minY + maxY) / 2
const varY = maxY - minY
const k = Math.log(varY) / Math.log(10)
const kRound = Math.round(k)
const kCeil = Math.ceil(k)
const kFloor = Math.floor(k)
const step = norm * 10 ** kRound
const bottom = Math.floor(minY / step) * step
const up = Math.ceil(maxY / step) * step
console.log(
{
minY,
maxY,
avgY,
varY,
v: varY / avgY,
kFloor,
k,
kRound,
kCeil,
step,
bottom,
up,
// TODO: implement different scaling factor for each series
scaleFactor: 1,
},
[minY / step, maxY / step],
"-->",
[bottom, up]
)
return [bottom, up]
}
export const getYRange = (matrix: Matrix, seriesIdList: number[]): Pair => {
let getSeries = createGetSeries(matrix)
const rangeList = seriesIdList.map((col) => {
const values = getSeries(col).map(([_, y]) => y)
return getRange(values)
})
rangeList.forEach((range) => extendRange(range, 1)) // TODO: remove; implement different scaling for different sereis
return getRange(rangeList.flat())
}
export const createGetColor = ({ COLOR_LIST }: ChartOptions) => (
seriesId: number
): string => COLOR_LIST[seriesId % COLOR_LIST.length]