-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisDisplay.ts
117 lines (106 loc) · 3.77 KB
/
VisDisplay.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
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Vis, ColumnInfo, EColumnTypes, VisColumn } from './lib/vis';
import {
LineUp,
CategoricalColumn,
Column,
IDataRow,
NumberColumn,
ValueColumn
} from 'lineupjs';
import FerretColumn from './FerretColumn';
import ExcelColumn from './ExcelColumn';
export class VisDisplay {
constructor(container: HTMLElement) {
this._container = container;
}
private _container: HTMLElement;
public get container(): HTMLElement {
return this._container;
}
private _lineup: LineUp;
public get lineup(): LineUp {
return this._lineup;
}
public async SetData(lineup: LineUp): Promise<void> {
const ranking = lineup.data.getFirstRanking();
this._lineup = lineup;
const getColumnInfo = (column: Column): ColumnInfo => {
return {
// This regex strips any html off of the label and summary, leaving only the center text. For example, <div><span>Hello</span></div> would be hello.
name: column.getMetaData().label.replace(/(<([^>]+)>)/gi, ''),
description: 'column_description_placeholder',
id: column.fqid
};
};
const mapData = <T extends ValueColumn<any>>(
data: IDataRow[],
column: T
) => {
return data.map((d, i) => ({
id: i,
val: column.getRaw(d)
}));
};
const getColumnValue = async <T extends ValueColumn<any>>(
column: T
) => {
const data: IDataRow[] = [];
const indices = ranking.getOrder();
for (let i of indices) {
data[i] = await lineup.data.getRow(i);
}
if (column.isLoaded()) {
return mapData(data, column);
}
return new Promise<{ id: number; val: T }[]>((resolve, reject) => {
//times out if we take longer than 60 seconds to load the columns.
const timeout = setTimeout(() => {
reject('Timeout');
}, 60000);
column.on(ValueColumn.EVENT_DATA_LOADED, () => {
clearTimeout(timeout);
resolve(mapData(data, column));
});
});
};
const cols: VisColumn[] = [];
for (const c of ranking.flatColumns) {
if (c instanceof FerretColumn || c instanceof ExcelColumn) {
cols.push({
info: getColumnInfo(c),
values: () => getColumnValue(c),
type: EColumnTypes.NUMERICAL
});
} else if (c instanceof CategoricalColumn) {
cols.push({
info: getColumnInfo(c),
values: () =>
getColumnValue(c).then(res =>
res.map(v =>
v.val
? v
: {
...v,
val: '--'
}
)
),
type: EColumnTypes.CATEGORICAL
});
}
}
const someReactThing = React.createElement(Vis, {
columns: cols,
// selected: selectedMap,
selectionCallback: (s: number[]) => {
console.log('selectionCallback', s);
},
filterCallback: (s: string) => {
console.log('filterCallback', s);
}
});
ReactDOM.render(someReactThing, this.container);
}
}