-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-to-table.js
50 lines (48 loc) · 1.46 KB
/
csv-to-table.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
function data2table(divId, url) {
d3.csv(url, function(error, data) {
if (error) throw error;
//comes from: http://bl.ocks.org/AMDS/4a61497182b8fcb05906
var sortAscending = true;
var table = d3.select(divId);
var titles = d3.keys(data[0]);
var headers = table.append('thead').append('tr')
.selectAll('th')
.data(titles).enter()
.append('th').attr('scope', 'col')
.text(function (d) {
return d;
})
.on('click', function (d) {
headers.attr('class', 'header');
if (sortAscending) {
rows.sort(function(a, b) {
return b[d] < a[d] ? 1 : -1;
});
sortAscending = false;
this.className = 'aes';
} else {
rows.sort(function(a, b) {
return b[d] > a[d] ? 1 : -1;
});
sortAscending = true;
this.className = 'des';
}
});
var rows = table.append('tbody').selectAll('tr')
.data(data).enter()
.append('tr');
rows.selectAll('td')
.data(function (d) {
return titles.map(function (k) {
return { 'value': d[k], 'name': k};
});
}).enter()
.append('td')
.attr('data-th', function (d) {
return d.name;
})
.text(function (d) {
return d.value;
});
});
}