-
Notifications
You must be signed in to change notification settings - Fork 33
/
heatmap.ts
158 lines (134 loc) · 3.96 KB
/
heatmap.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {range, extent} from 'd3-array'
import {getContinuousColorScale} from './'
import {newTable} from '../utils/newTable'
import {X_MIN, X_MAX, Y_MIN, Y_MAX, COUNT} from '../constants/columnKeys'
import {resolveDomain} from '../utils/resolveDomain'
import {Table, RectLayerSpec} from '../types'
export const heatmapTransform = (
inputTable: Table,
xColumnKey: string,
yColumnKey: string,
xDomain: number[] | null,
yDomain: number[] | null,
width: number,
height: number,
binSize: number,
colors: string[]
): RectLayerSpec => {
const resolvedXDomain = resolveDomain(
inputTable.getColumn(xColumnKey, 'number'),
xDomain
)
const resolvedYDomain = resolveDomain(
inputTable.getColumn(yColumnKey, 'number'),
yDomain
)
const table = bin2d(
inputTable,
xColumnKey,
yColumnKey,
resolvedXDomain,
resolvedYDomain,
width,
height,
binSize
)
const countDomain = extent(table.getColumn(COUNT, 'number'))
const fillScale = getContinuousColorScale(countDomain, colors)
return {
type: 'rect',
inputTable,
table,
binDimension: 'xy',
xDomain: resolvedXDomain,
yDomain: resolvedYDomain,
xColumnKey,
yColumnKey,
xColumnType: inputTable.getColumnType(xColumnKey),
yColumnType: 'number',
scales: {fill: fillScale},
columnGroupMaps: {},
}
}
export const bin2d = (
table: Table,
xColKey: string,
yColKey: string,
xDomain: number[],
yDomain: number[],
width: number,
height: number,
binSize: number
): Table => {
const xColData = table.getColumn(xColKey, 'number')
const yColData = table.getColumn(yColKey, 'number')
const xColType = table.getColumnType(xColKey) as 'time' | 'number'
const yColType = table.getColumnType(yColKey) as 'time' | 'number'
const xBinCount = Math.floor(width / binSize)
const yBinCount = Math.floor(height / binSize)
// Count occurences in each bin in a `xBinCount` by `yBinCount` matrix
//
// 4th y bin
//
// │
// │
// v
// [
// [0, 1, 2, 0, 0],
// [0, 1, 0, 2, 0], <──── 2nd x bin
// [1, 0, 5, 7, 3]
// ]
//
const bins = range(xBinCount).map(__ => new Array(yBinCount).fill(0))
for (let i = 0; i < table.length; i++) {
const x = xColData[i]
const y = yColData[i]
const shouldSkipPoint =
!x ||
!y ||
x < xDomain[0] ||
x > xDomain[1] ||
y < yDomain[0] ||
y > yDomain[1]
if (shouldSkipPoint) {
continue
}
const xBinIndex = getBinIndex(x, xDomain, xBinCount)
const yBinIndex = getBinIndex(y, yDomain, yBinCount)
bins[xBinIndex][yBinIndex] += 1
}
// Now build a `Table` from that matrix
const xBinWidth = (xDomain[1] - xDomain[0]) / xBinCount
const yBinWidth = (yDomain[1] - yDomain[0]) / yBinCount
const xMinData: number[] = []
const xMaxData: number[] = []
const yMinData: number[] = []
const yMaxData: number[] = []
const countData: number[] = []
for (let i = 0; i < xBinCount; i++) {
for (let j = 0; j < yBinCount; j++) {
xMinData.push(xDomain[0] + i * xBinWidth)
xMaxData.push(xDomain[0] + (i + 1) * xBinWidth)
yMinData.push(yDomain[0] + j * yBinWidth)
yMaxData.push(yDomain[0] + (j + 1) * yBinWidth)
countData.push(bins[i][j])
}
}
const heatmapTable = newTable(xMinData.length)
.addColumn(X_MIN, xColType, xMinData)
.addColumn(X_MAX, xColType, xMaxData)
.addColumn(Y_MIN, yColType, yMinData)
.addColumn(Y_MAX, yColType, yMaxData)
.addColumn(COUNT, 'number', countData)
return heatmapTable
}
const getBinIndex = (val: number, domain: number[], binCount: number) => {
const domainWidth = domain[1] - domain[0]
const percentage = (val - domain[0]) / domainWidth
let binIndex = Math.floor(percentage * binCount)
if (binIndex === binCount) {
// Special case: last bin is inclusive
binIndex = binCount - 1
}
return binIndex
}