-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
398 lines (331 loc) · 8.89 KB
/
index.js
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
'use strict'
const createScatter = require('regl-scatter2d')
const pick = require('pick-by-alias')
const getBounds = require('array-bounds')
const raf = require('raf')
const arrRange = require('array-range')
const rect = require('parse-rect')
const flatten = require('flatten-vertex-data')
module.exports = SPLOM
// @constructor
function SPLOM (regl, options) {
if (!(this instanceof SPLOM)) return new SPLOM(regl, options)
// render passes
this.traces = []
// passes for scatter, combined across traces
this.passes = {}
this.regl = regl
// main scatter drawing instance
this.scatter = createScatter(regl)
this.canvas = this.scatter.canvas
}
// update & draw passes once per frame
SPLOM.prototype.render = function (...args) {
if (args.length) {
this.update(...args)
}
if (this.regl.attributes.preserveDrawingBuffer) return this.draw()
// make sure draw is not called more often than once a frame
if (this.dirty) {
if (this.planned == null) {
this.planned = raf(() => {
this.draw()
this.dirty = true
this.planned = null
})
}
}
else {
this.draw()
this.dirty = true
raf(() => {
this.dirty = false
})
}
return this
}
// update passes
SPLOM.prototype.update = function (...args) {
if (!args.length) return
for (let i = 0; i < args.length; i++) {
this.updateItem(i, args[i])
}
// remove nulled passes
this.traces = this.traces.filter(Boolean)
// FIXME: update passes independently
let passes = []
let offset = 0
for (let i = 0; i < this.traces.length; i++) {
let trace = this.traces[i]
let tracePasses = this.traces[i].passes
for (let j = 0; j < tracePasses.length; j++) {
passes.push(this.passes[tracePasses[j]])
}
// save offset of passes
trace.passOffset = offset
offset += trace.passes.length
}
this.scatter.update(...passes)
return this
}
// update trace by index, not supposed to be called directly
SPLOM.prototype.updateItem = function (i, options) {
let { regl } = this
// remove pass if null
if (options === null) {
this.traces[i] = null
return this
}
if (!options) return this
let o = pick(options, {
data: 'data items columns rows values dimensions samples x',
snap: 'snap cluster',
size: 'sizes size radius',
color: 'colors color fill fill-color fillColor',
opacity: 'opacity alpha transparency opaque',
borderSize: 'borderSizes borderSize border-size bordersize borderWidth borderWidths border-width borderwidth stroke-width strokeWidth strokewidth outline',
borderColor: 'borderColors borderColor bordercolor stroke stroke-color strokeColor',
marker: 'markers marker shape',
range: 'range ranges databox dataBox',
viewport: 'viewport viewBox viewbox',
domain: 'domain domains area areas',
padding: 'pad padding paddings pads margin margins',
transpose: 'transpose transposed',
diagonal: 'diagonal diag showDiagonal',
upper: 'upper up top upperhalf upperHalf showupperhalf showUpper showUpperHalf',
lower: 'lower low bottom lowerhalf lowerHalf showlowerhalf showLowerHalf showLower'
})
// we provide regl buffer per-trace, since trace data can be changed
let trace = (this.traces[i] || (this.traces[i] = {
id: i,
buffer: regl.buffer({
usage: 'dynamic',
type: 'float',
data: new Uint8Array()
}),
color: 'black',
marker: null,
size: 12,
borderColor: 'transparent',
borderSize: 1,
viewport: rect([regl._gl.drawingBufferWidth, regl._gl.drawingBufferHeight]),
padding: [0, 0, 0, 0],
opacity: 1,
diagonal: true,
upper: true,
lower: true
}))
// save styles
if (o.color != null) {
trace.color = o.color
}
if (o.size != null) {
trace.size = o.size
}
if (o.marker != null) {
trace.marker = o.marker
}
if (o.borderColor != null) {
trace.borderColor = o.borderColor
}
if (o.borderSize != null) {
trace.borderSize = o.borderSize
}
if (o.opacity != null) {
trace.opacity = o.opacity
}
if (o.viewport) {
trace.viewport = rect(o.viewport)
}
if (o.diagonal != null) trace.diagonal = o.diagonal
if (o.upper != null) trace.upper = o.upper
if (o.lower != null) trace.lower = o.lower
// put flattened data into buffer
if (o.data) {
trace.buffer(flatten(o.data))
trace.columns = o.data.length
trace.count = o.data[0].length
// detect bounds per-column
trace.bounds = []
for (let i = 0; i < trace.columns; i++) {
trace.bounds[i] = getBounds(o.data[i], 1)
}
}
// add proper range updating markers
let multirange
if (o.range) {
trace.range = o.range
multirange = trace.range && typeof trace.range[0] !== 'number'
}
if (o.domain) {
trace.domain = o.domain
}
let multipadding = false
if (o.padding != null) {
// multiple paddings
if (Array.isArray(o.padding) && o.padding.length === trace.columns && typeof o.padding[o.padding.length - 1] === 'number') {
trace.padding = o.padding.map(getPad)
multipadding = true
}
// single padding
else {
trace.padding = getPad(o.padding)
}
}
// create passes
let m = trace.columns
let n = trace.count
let w = trace.viewport.width
let h = trace.viewport.height
let left = trace.viewport.x
let top = trace.viewport.y
let iw = w / m
let ih = h / m
trace.passes = []
for (let i = 0; i < m; i++) {
for (let j = 0; j < m; j++) {
if (!trace.diagonal && j === i) continue
if (!trace.upper && i > j) continue
if (!trace.lower && i < j) continue
let key = passId(trace.id, i, j)
let pass = this.passes[key] || (this.passes[key] = {})
if (o.data) {
if (o.transpose) {
pass.positions = {
x: {buffer: trace.buffer, offset: j, count: n, stride: m},
y: {buffer: trace.buffer, offset: i, count: n, stride: m}
}
}
else {
pass.positions = {
x: {buffer: trace.buffer, offset: j * n, count: n},
y: {buffer: trace.buffer, offset: i * n, count: n}
}
}
pass.bounds = getBox(trace.bounds, i, j)
}
if (o.domain || o.viewport || o.data) {
let pad = multipadding ? getBox(trace.padding, i, j) : trace.padding
if (trace.domain) {
let [lox, loy, hix, hiy] = getBox(trace.domain, i, j)
pass.viewport = [
left + lox * w + pad[0],
top + loy * h + pad[1],
left + hix * w - pad[2],
top + hiy * h - pad[3]
]
}
// consider auto-domain equipartial
else {
pass.viewport = [
left + j * iw + iw * pad[0],
top + i * ih + ih * pad[1],
left + (j + 1) * iw - iw * pad[2],
top + (i + 1) * ih - ih * pad[3]
]
}
}
if (o.color) pass.color = trace.color
if (o.size) pass.size = trace.size
if (o.marker) pass.marker = trace.marker
if (o.borderSize) pass.borderSize = trace.borderSize
if (o.borderColor) pass.borderColor = trace.borderColor
if (o.opacity) pass.opacity = trace.opacity
if (o.range) {
pass.range = multirange ? getBox(trace.range, i, j) : trace.range || pass.bounds
}
trace.passes.push(key)
}
}
return this
}
// draw all or passed passes
SPLOM.prototype.draw = function (...args) {
if (!args.length) {
this.scatter.draw()
}
else {
let idx = []
for (let i = 0; i < args.length; i++) {
// draw(0, 2, 5) - draw traces
if (typeof args[i] === 'number' ) {
let { passes, passOffset } = this.traces[args[i]]
idx.push(...arrRange(passOffset, passOffset + passes.length))
}
// draw([0, 1, 2 ...], [3, 4, 5]) - draw points
else if (args[i].length) {
let els = args[i]
let { passes, passOffset } = this.traces[i]
passes = passes.map((passId, i) => {
idx[passOffset + i] = els
})
}
}
this.scatter.draw(...idx)
}
return this
}
// dispose resources
SPLOM.prototype.destroy = function () {
this.traces.forEach(trace => {
if (trace.buffer && trace.buffer.destroy) trace.buffer.destroy()
})
this.traces = null
this.passes = null
this.scatter.destroy()
return this
}
// return pass corresponding to trace i- j- square
function passId (trace, i, j) {
let id = (trace.id != null ? trace.id : trace)
let n = i
let m = j
let key = id << 16 | (n & 0xff) << 8 | m & 0xff
return key
}
// return bounding box corresponding to a pass
function getBox (items, i, j) {
let ilox, iloy, ihix, ihiy, jlox, jloy, jhix, jhiy
let iitem = items[i], jitem = items[j]
if (iitem.length > 2) {
ilox = iitem[0]
ihix = iitem[2]
iloy = iitem[1]
ihiy = iitem[3]
}
else if (iitem.length) {
ilox = iloy = iitem[0]
ihix = ihiy = iitem[1]
}
else {
ilox = iitem.x
iloy = iitem.y
ihix = iitem.x + iitem.width
ihiy = iitem.y + iitem.height
}
if (jitem.length > 2) {
jlox = jitem[0]
jhix = jitem[2]
jloy = jitem[1]
jhiy = jitem[3]
}
else if (jitem.length) {
jlox = jloy = jitem[0]
jhix = jhiy = jitem[1]
}
else {
jlox = jitem.x
jloy = jitem.y
jhix = jitem.x + jitem.width
jhiy = jitem.y + jitem.height
}
return [ jlox, iloy, jhix, ihiy ]
}
function getPad (arg) {
if (typeof arg === 'number') return [arg, arg, arg, arg]
else if (arg.length === 2) return [arg[0], arg[1], arg[0], arg[1]]
else {
let box = rect(arg)
return [box.x, box.y, box.x + box.width, box.y + box.height]
}
}