-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.html
139 lines (111 loc) · 3.94 KB
/
table.html
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
<!DOCTYPE html>
<!-- vim: set shiftwidth=4: -->
<html lang="en">
<!-- =========================================================================================== -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<title>D3 Table Example</title>
<style type="text/css">
body { margin: 1em 5% 0 5%; }
.enter { fill: green; }
.update { fill: #333; }
.exit { fill: red; }
p { max-width: 36em; }
table {
/*border: solid 4px blue;*/
border-collapse: collapse;
}
th, td {
border: solid 2pt #888888;
padding: 0.1em 0.5em;
}
th {
background: #d0d0f0;
text-align: left;
}
</style>
</head>
<!-- =========================================================================================== -->
<body>
<h1>Simple D3-driven Table</h1>
<div id="target"></div>
<p>An example of using D3 to create an HTML table from a set of arbitrary columns and rows. This
approach supports any subset of columns, in any order, with possible duplicates.</p>
<script>
// Since this script relies on page structure, it should be the last thing
// in the <body>.
//----------------------------------------------------------------------------
// Description of columns, and indirect specification of row elements
var columnData = [
{ id: 'id1', name: 'Who', type: 'string' },
{ id: 'id2', name: 'How Many', type: 'number' },
{ id: 'id3', name: 'Ok', type: 'boolean' }
];
// Data points, with one datum per column.
var rowData = [
["Aaron", 1, true ],
["Benjamin", 2, false],
["Caruthers", 3, true ],
["Django", 4, false],
["Edwin", 5, true ]
];
// Order and selection of desired columns from source data
var columnSelections = [ 'id3', 'id1', 'id2', 'id1' ];
// Set up parent elements
var target = d3.select('div');
var table = target.append('table');
var tableHead = table.append('thead');
var tableBody = table.append('tbody');
// Set up swizzle of column indices
var columnSwizzle = [];
for (var i=0; i < columnSelections.length; ++i) {
var columnIndex = 0;
while ((columnIndex < columnData.length) && (columnSelections[i] !== columnData[columnIndex].id))
++columnIndex;
if (columnIndex < columnData.length)
columnSwizzle.push (columnIndex);
}
// Create the headers of selected columns.
var headers = [];
for (var columnIndex=0; columnIndex < columnSelections.length; ++columnIndex) {
headers.push (columnData[columnSwizzle[columnIndex]].name);
}
// Render the table headers
var columns = tableHead.append('tr')
.selectAll('th')
.data(headers);
columns.enter()
.append('th')
.text(function(d){return d});
columns.exit()
.remove();
// Render the table rows
function update(data) {
// Create the filtered data from the original source and column selection
var filteredData = [];
for (var rowIndex=0; rowIndex < data.length; ++rowIndex) {
var sourceRow = data[rowIndex];
var filteredRow = [];
for (var i=0; i < columnSwizzle.length; ++i)
filteredRow[i] = sourceRow[columnSwizzle[i]];
filteredData.push (filteredRow);
}
var rows = tableBody.selectAll('tr')
.data(filteredData);
rows.enter()
.append('tr')
.selectAll('td')
.data(function(d){return d})
.enter()
.append('td')
.text(function(d){return d});
rows.exit()
.remove();
}
// The initial display.
update(rowData);
//----------------------------------------------------------------------------
</script>
</body>
</html>