-
Notifications
You must be signed in to change notification settings - Fork 33
/
lineData.ts
70 lines (59 loc) · 1.65 KB
/
lineData.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
import {DomainLabel, LineData, Scale, Table} from '../types'
import {simplify} from '../utils/simplify'
import {FILL} from '../constants/columnKeys'
export const collectLineData = (
table: Table,
xColKey: string,
yColKey: string,
fillScale: Scale<string, string>
): LineData => {
const xCol = table.getColumn(xColKey, 'number')
const yCol = table.getColumn(yColKey, 'number')
const groupCol = table.getColumn(FILL, 'string')
const result = {}
for (let i = 0; i < table.length; i++) {
const groupKey = groupCol[i]
if (!result[groupKey]) {
result[groupKey] = {
xs: [],
ys: [],
fill: fillScale(groupKey),
}
}
result[groupKey].xs.push(xCol[i])
result[groupKey].ys.push(yCol[i])
}
return result
}
export const simplifyLineData = (
lineData: LineData,
xScale,
yScale
): LineData => {
const result = {}
for (const [k, {xs, ys, fill}] of Object.entries(lineData)) {
const [simplifedXs, simplifiedYs] = simplify(
xs.map(x => xScale(x || 0)),
ys.map(y => yScale(y || 0)),
0.5
)
result[k] = {xs: simplifedXs, ys: simplifiedYs, fill}
}
return result
}
export const getDomainDataFromLines = (
lineData: LineData,
domainLabel: DomainLabel
): number[] => {
const result = []
const numberOfLines = Object.keys(lineData).length
for (let lineIndex = 0; lineIndex < numberOfLines; lineIndex += 1) {
const line = lineData[lineIndex] ? lineData[lineIndex][domainLabel] : []
if (Array.isArray(line)) {
for (let domainIndex = 0; domainIndex < line.length; domainIndex += 1) {
result.push(line[domainIndex])
}
}
}
return result
}