-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
409 lines (359 loc) · 12.8 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
399
400
401
402
403
404
405
406
407
408
409
import d3 from 'd3';
import style from 'PVWStyle/InfoVizNative/FieldSelector.mcss';
import CompositeClosureHelper from 'paraviewweb/src/Common/Core/CompositeClosureHelper';
import template from 'paraviewweb/src/InfoViz/Native/FieldSelector/template.html';
// ----------------------------------------------------------------------------
// Global
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Field Selector
// ----------------------------------------------------------------------------
function fieldSelector(publicAPI, model) {
// private variables
const hideField = {
minMax: false,
hist: false,
minMaxWidth: 0,
histWidth: 0,
};
// storage for 1d histograms
if (!model.histograms) {
model.histograms = {};
}
// public API
publicAPI.resize = () => {
publicAPI.render();
};
publicAPI.setContainer = (el) => {
if (model.container) {
while (model.container.firstChild) {
model.container.removeChild(model.container.firstChild);
}
model.container = null;
}
model.container = el;
if (el) {
d3.select(model.container).html(template);
d3.select(model.container)
.select('.fieldSelector')
.classed(style.fieldSelector, true);
model.fieldShowHistogram =
model.fieldShowHistogram && model.provider.isA('Histogram1DProvider');
// append headers for histogram columns
if (model.fieldShowHistogram) {
const header = d3
.select(model.container)
.select('thead')
.select('tr');
header
.append('th')
.text('Min')
.classed(style.jsHistMin, true);
header
.append('th')
.text('Histogram')
.classed(style.jsSparkline, true);
header
.append('th')
.text('Max')
.classed(style.jsHistMax, true);
}
publicAPI.render();
}
};
publicAPI.render = () => {
if (!model.container) {
return;
}
const legendSize = 15;
// Apply style
d3.select(model.container)
.select('thead')
.classed(style.thead, true);
d3.select(model.container)
.select('tbody')
.classed(style.tbody, true);
d3.select(model.container)
.select('th.field-selector-mode')
.on('click', (d) => {
model.displayUnselected = !model.displayUnselected;
publicAPI.render();
})
.select('i')
// apply class - 'false' should come first to not remove common base class.
.classed(
!model.displayUnselected
? style.allFieldsIcon
: style.selectedFieldsIcon,
false
)
.classed(
model.displayUnselected
? style.allFieldsIcon
: style.selectedFieldsIcon,
true
);
const data = model.displayUnselected
? model.provider.getFieldNames()
: model.provider.getActiveFieldNames();
const totalNum = model.displayUnselected
? data.length
: model.provider.getFieldNames().length;
// Update header label
d3.select(model.container)
.select('th.field-selector-label')
.style('text-align', 'left')
.text(
model.displayUnselected
? `Only Selected (${data.length} total)`
: `Only Selected (${data.length} / ${totalNum} total)`
)
.on('click', (d) => {
model.displayUnselected = !model.displayUnselected;
publicAPI.render();
});
// test for too-long rows
const hideMore = model.container.scrollWidth > model.container.clientWidth;
if (hideMore) {
if (!hideField.minMax) {
hideField.minMax = true;
hideField.minMaxWidth = model.container.scrollWidth;
// if we hide min/max, we may also need to hide hist, so trigger another resize
setTimeout(publicAPI.resize, 0);
} else if (!hideField.hist) {
hideField.hist = true;
hideField.histWidth = model.container.scrollWidth;
}
} else if (hideField.minMax) {
// if we've hidden something, see if we can re-show it.
if (hideField.hist) {
if (model.container.scrollWidth - hideField.histWidth > 0) {
hideField.hist = false;
hideField.histWidth = 0;
// if we show hist, we may also need to show min/max, so trigger another resize
setTimeout(publicAPI.resize, 0);
}
} else if (hideField.minMax) {
if (model.container.scrollWidth - hideField.minMaxWidth > 0) {
hideField.minMax = false;
hideField.minMaxWidth = 0;
}
}
}
const header = d3
.select(model.container)
.select('thead')
.select('tr');
header
.selectAll(`.${style.jsHistMin}`)
.style('display', hideField.minMax ? 'none' : null);
header
.selectAll(`.${style.jsSparkline}`)
.style('display', hideField.hist ? 'none' : null);
header
.selectAll(`.${style.jsHistMax}`)
.style('display', hideField.minMax ? 'none' : null);
// Handle variables
const variablesContainer = d3
.select(model.container)
.select('tbody.fields')
.selectAll('tr')
.data(data);
variablesContainer.enter().append('tr');
variablesContainer.exit().remove();
// Apply on each data item
function renderField(fieldName, index) {
const field = model.provider.getField(fieldName);
const fieldContainer = d3.select(this);
let legendCell = fieldContainer.select(`.${style.jsLegend}`);
let fieldCell = fieldContainer.select(`.${style.jsFieldName}`);
// Apply style to row (selected/unselected)
fieldContainer
.classed(!field.active ? style.selectedRow : style.unselectedRow, false)
.classed(field.active ? style.selectedRow : style.unselectedRow, true)
.on('click', (name) => {
model.provider.toggleFieldSelection(name);
});
// Create missing DOM element if any
if (legendCell.empty()) {
legendCell = fieldContainer.append('td').classed(style.legend, true);
fieldCell = fieldContainer.append('td').classed(style.fieldName, true);
}
// Apply legend
if (model.provider.isA('LegendProvider')) {
const { color, shape } = model.provider.getLegend(fieldName);
legendCell.html(`<svg class='${
style.legendSvg
}' width='${legendSize}' height='${legendSize}' viewBox='${
shape.viewBox
}'
fill='${color}' stroke='black'><use xlink:href='#${
shape.id
}'/></svg>`);
} else {
legendCell
.html('<i></i>')
.select('i')
.classed(
!field.active ? style.selectedRow : style.unselectedRow,
false
)
.classed(
field.active ? style.selectedRow : style.unselectedRow,
true
);
}
// Apply field name
fieldCell.text(fieldName);
if (model.fieldShowHistogram) {
let minCell = fieldContainer.select(`.${style.jsHistMin}`);
let histCell = fieldContainer.select(`.${style.jsSparkline}`);
let maxCell = fieldContainer.select(`.${style.jsHistMax}`);
if (histCell.empty()) {
minCell = fieldContainer.append('td').classed(style.jsHistMin, true);
histCell = fieldContainer.append('td').classed(style.sparkline, true);
maxCell = fieldContainer.append('td').classed(style.jsHistMax, true);
histCell
.append('svg')
.classed(style.sparklineSvg, true)
.attr('width', model.fieldHistWidth)
.attr('height', model.fieldHistHeight);
}
// make sure our data is ready. If not, render will be called when loaded.
const hobj = model.histograms ? model.histograms[fieldName] : null;
if (hobj) {
histCell.style('display', hideField.hist ? 'none' : null);
// only do work if histogram is displayed.
if (!hideField.hist) {
const cmax = 1.0 * d3.max(hobj.counts);
const hsize = hobj.counts.length;
const hdata = histCell
.select('svg')
.selectAll(`.${style.jsHistRect}`)
.data(hobj.counts);
hdata.enter().append('rect');
// changes apply to both enter and update data join:
hdata
.attr(
'class',
(d, i) => (i % 2 === 0 ? style.histRectEven : style.histRectOdd)
)
.attr('pname', fieldName)
.attr('y', (d) => model.fieldHistHeight * (1.0 - d / cmax))
.attr('x', (d, i) => (model.fieldHistWidth / hsize) * i)
.attr('height', (d) => model.fieldHistHeight * (d / cmax))
.attr('width', model.fieldHistWidth / hsize);
hdata.exit().remove();
if (model.provider.isA('HistogramBinHoverProvider')) {
histCell
.select('svg')
.on('mousemove', function inner(d, i) {
const mCoords = d3.mouse(this);
const binNum = Math.floor(
(mCoords[0] / model.fieldHistWidth) * hsize
);
const state = {};
state[fieldName] = [binNum];
model.provider.setHoverState({ state });
})
.on('mouseout', (d, i) => {
const state = {};
state[fieldName] = [-1];
model.provider.setHoverState({ state });
});
}
}
const formatter = d3.format('.3s');
minCell
.text(formatter(hobj.min))
.style('display', hideField.minMax ? 'none' : null);
maxCell
.text(formatter(hobj.max))
.style('display', hideField.minMax ? 'none' : null);
}
}
}
// Render all fields
variablesContainer.each(renderField);
};
function handleHoverUpdate(data) {
const svg = d3.select(model.container);
Object.keys(data.state).forEach((pName) => {
const binList = data.state[pName];
svg
.selectAll(`rect[pname='${pName}']`)
.classed(style.histoHilite, (d, i) => binList.indexOf(-1) === -1)
.classed(style.binHilite, (d, i) => binList.indexOf(i) >= 0);
});
}
// Make sure default values get applied
publicAPI.setContainer(model.container);
model.subscriptions.push({ unsubscribe: publicAPI.setContainer });
model.subscriptions.push(
model.provider.onFieldChange(() => {
publicAPI.render();
model.histogram1DDataSubscription.update(model.provider.getFieldNames(), {
numberOfBins: model.numberOfBins,
partial: true,
});
})
);
if (model.fieldShowHistogram) {
if (model.provider.isA('Histogram1DProvider')) {
model.histogram1DDataSubscription = model.provider.subscribeToHistogram1D(
(allHistogram1d) => {
// Below, we're asking for partial updates, so we just update our
// cache with anything that came in.
Object.keys(allHistogram1d).forEach((paramName) => {
model.histograms[paramName] = allHistogram1d[paramName];
});
publicAPI.render();
},
model.provider.getFieldNames(),
{
numberOfBins: model.numberOfBins,
partial: true,
}
);
model.subscriptions.push(model.histogram1DDataSubscription);
}
}
if (model.provider.isA('HistogramBinHoverProvider')) {
model.subscriptions.push(
model.provider.onHoverBinChange(handleHoverUpdate)
);
}
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
container: null,
provider: null,
displayUnselected: true,
fieldShowHistogram: true,
fieldHistWidth: 120,
fieldHistHeight: 15,
numberOfBins: 32,
};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
CompositeClosureHelper.destroy(publicAPI, model);
CompositeClosureHelper.isA(publicAPI, model, 'VizComponent');
CompositeClosureHelper.get(publicAPI, model, [
'provider',
'container',
'fieldShowHistogram',
'numberOfBins',
]);
CompositeClosureHelper.set(publicAPI, model, [
'fieldShowHistogram',
'numberOfBins',
]);
fieldSelector(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = CompositeClosureHelper.newInstance(extend);
// ----------------------------------------------------------------------------
export default { newInstance, extend };