-
Notifications
You must be signed in to change notification settings - Fork 13
/
LinkLayer.tsx
257 lines (216 loc) · 7.82 KB
/
LinkLayer.tsx
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import React, { useState, useMemo, useEffect } from 'react'
import DeckGL from '@deck.gl/react'
import { LineOffsetLayer, OFFSET_DIRECTION } from '@/layers/LineOffsetLayer'
import { scaleThreshold, scaleOrdinal } from 'd3-scale'
import { StaticMap } from 'react-map-gl'
import { rgb } from 'd3-color'
import {
MAPBOX_TOKEN,
REACT_VIEW_HANDLES,
DataTableColumn,
LookupDataset,
DataType,
} from '@/Globals'
import globalStore from '@/store'
export default function Component({
links = { source: new Float32Array(), dest: new Float32Array() },
colors = ['#0099ee'],
colorRampType = -1, // -1 undefined, 0 categorical, 1 diffs, 2 sequential
dark = false,
scaleWidth = 1,
build = {} as LookupDataset,
base = {} as LookupDataset,
widths = {} as LookupDataset,
showDiffs = false,
viewId = 0,
}) {
// ------- draw frame begins here -----------------------------
const [viewState, setViewState] = useState(globalStore.state.viewState)
const { dataTable, activeColumn, csvRowFromLinkRow } = build
const buildColumn: DataTableColumn = dataTable[activeColumn] || { values: [] }
const widthValues = widths.dataTable[widths.activeColumn]
const widthRowLookup = widths.csvRowFromLinkRow
// deck.gl colors must be in rgb[] or rgba[] format
const colorsAsRGB: any = colors.map(hexcolor => {
const c = rgb(hexcolor)
return [c.r, c.g, c.b]
})
// Build breakpoints between 0.0 - 1.0 to match the number of color swatches
// e.g. If there are five colors, then we need 4 breakpoints: 0.2, 0.4, 0.6, 0.8.
// An exponent reduces visual dominance of very large values at the high end of the scale
const exponent = 4.0
const domain = new Array(colors.length - 1)
.fill(0)
.map((v, i) => Math.pow((1 / colors.length) * (i + 1), exponent))
// *scaleOrdinal* is the d3 function that maps categorical variables to colors.
// *scaleThreshold* is the d3 function that maps numerical values from [0.0,1.0) to the color buckets
// *range* is the list of colors;
// *domain* is the list of breakpoints in the 0-1.0 continuum; it is auto-created from data for categorical.
// *colorRampType* is 0 if a categorical color ramp is chosen
const isCategorical = colorRampType === 0 || buildColumn.type == DataType.STRING
const setColorBasedOnValue: any = isCategorical
? scaleOrdinal().range(colorsAsRGB)
: scaleThreshold().range(colorsAsRGB).domain(domain)
// this assumes that zero means hide the link. This may not be generic enough
const colorPaleGrey = dark ? [80, 80, 80, 96] : [212, 212, 212]
const colorInvisible = [0, 0, 0, 0]
// register setViewState in global view updater
// so we can respond to external map motion
REACT_VIEW_HANDLES[viewId] = () => {
setViewState(globalStore.state.viewState)
}
const numCsvRows = csvRowFromLinkRow.length
// --- LINE COLORS -----------------------------------------------
const getLineColor = (
feature: any,
objectInfo: { index: number; data: any; target: number[] }
) => {
if (!dataTable[activeColumn]) return colorPaleGrey
// use the csvRowLookup if we have it; if now then just use the link row number.
const csvRow = numCsvRows ? csvRowFromLinkRow[objectInfo.index] : objectInfo.index
let value = dataTable[activeColumn].values[csvRow]
if (!value) return colorInvisible
if (colors.length === 1) return colorsAsRGB[0]
// categorical?
if (isCategorical) {
return setColorBasedOnValue(value)
}
// comparison?
if (showDiffs) {
const baseValue = base.dataTable[base.activeColumn].values[csvRow]
const diff = value - baseValue
if (diff === 0) return colorPaleGrey // setColorBasedOnValue(0.5)
return diff > 0 ? [255, 0, 0] : [0, 0, 255] // red vs. blue
} else {
// don't use log scale if numbers are below 1.0
let ratio = value / (buildColumn.max || 1.0)
// if (ratio < 0.0001) return colorPaleGrey
return setColorBasedOnValue(ratio)
}
}
// --- LINE WIDTHS -----------------------------------------------
const getLineWidth = (
feature: any,
objectInfo: { index: number; data: any; target: number[] }
) => {
if (!widthValues) return 0
const csvRow = widthRowLookup.length ? widthRowLookup[objectInfo.index] : objectInfo.index
const value = widthValues.values[csvRow]
if (showDiffs) {
const baseValue = base.dataTable[base.activeColumn].values[csvRow]
const diff = Math.abs(value - baseValue)
return diff / scaleWidth
} else {
return value / scaleWidth
}
}
function handleClick() {
console.log('click!')
}
function handleViewState(view: any) {
setViewState(view)
view.center = [view.longitude, view.latitude]
globalStore.commit('setMapCamera', view)
}
function precise(x: number) {
return x.toPrecision(5)
}
function buildTooltipHtml(column: DataTableColumn, index: number) {
try {
if (!column) return null
let value = column.values[index]
let baseValue = base ? base.dataTable[column.name].values[index] : null
if (isCategorical) {
if (!Number.isFinite(value)) return null
return `<b>${column.name}</b><p>${value}</p>`
}
let html = null
if (Number.isFinite(value)) html = `<b>${column.name}</b><p>Value: ${precise(value)}</p>`
let diff = value - baseValue
if (Number.isFinite(baseValue)) {
html += `<p>Base: ${precise(baseValue)}</p>`
html += `<p>+/- Base: ${precise(diff)}</p>`
}
return html
} catch (e) {
return null
}
}
function getTooltip({ object, index }: { object: any; index: number }) {
// tooltip will show values for color settings and for width settings.
// if there is base data, it will also show values and diff vs. base for both color and width.
try {
// tooltip color valuess------------
const csvRow = numCsvRows ? csvRowFromLinkRow[index] : index
let tooltip = buildTooltipHtml(buildColumn, csvRow)
// tooltip widths------------
if (widthValues && widthValues.name !== buildColumn.name) {
const csvRow = widthRowLookup.length ? widthRowLookup[index] : index
const widthTip = buildTooltipHtml(widthValues, csvRow)
if (widthTip) tooltip = tooltip ? tooltip + widthTip : widthTip
}
if (!tooltip) return null
return {
html: tooltip,
style: { color: dark ? '#ccc' : '#223', backgroundColor: dark ? '#2a3c4f' : 'white' },
}
} catch (e) {
console.warn(e)
return null
}
}
//@ts-ignore
const layer = new LineOffsetLayer({
id: 'linkLayer',
data: {
length: links.source.length / 2,
attributes: {
getSourcePosition: { value: links.source, size: 2 },
getTargetPosition: { value: links.dest, size: 2 },
},
},
getColor: getLineColor,
getWidth: getLineWidth,
widthUnits: 'pixels',
widthMinPixels: 1,
widthMaxPixels: 50,
pickable: true,
opacity: 0.97,
autoHighlight: true,
highlightColor: [255, 0, 224],
offsetDirection: OFFSET_DIRECTION.RIGHT,
updateTriggers: {
getColor: [showDiffs, dark, colors, activeColumn],
getWidth: [showDiffs, scaleWidth, widths],
},
transitions: {
getColor: 200,
getWidth: 200,
},
parameters: {
depthTest: false,
},
})
return (
/*
//@ts-ignore */
<DeckGL
layers={[layer]}
viewState={viewState}
controller={true}
pickingRadius={5}
getTooltip={getTooltip}
getCursor={({ isDragging, isHovering }: any) =>
isDragging ? 'grabbing' : isHovering ? 'pointer' : 'grab'
}
onClick={handleClick}
onViewStateChange={(e: any) => handleViewState(e.viewState)}
>
{
/*
// @ts-ignore */
<StaticMap mapStyle={globalStore.getters.mapStyle} mapboxApiAccessToken={MAPBOX_TOKEN} />
}
</DeckGL>
)
}