Last active
May 12, 2019 23:38
-
-
Save AdamMescher/5736b4b2546d8ebf4c8e223ed6bc7670 to your computer and use it in GitHub Desktop.
D3 Table Creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://www.d3noob.org/2013/02/add-html-table-to-your-d3js-graph.html | |
| const tabulate = (data, columns) => { | |
| const table = d3.select("body").append("table"); | |
| const thead = table.append("thead"); | |
| const tbody = table.append("tbody"); | |
| // append the header row | |
| thead | |
| .append("tr") | |
| .selectAll("th") | |
| .data(columns) | |
| .enter() | |
| .append("th") | |
| .text(column => column); | |
| // create a row for each object in the data | |
| const rows = tbody | |
| .selectAll("tr") | |
| .data(data) | |
| .enter() | |
| .append("tr"); | |
| // create a cell in each row for each column | |
| const cells = rows | |
| .selectAll("td") | |
| .data(row => columns.map(column => ({ column, value: row[column] }))) | |
| .enter() | |
| .append("td") | |
| .html(d => d.value); | |
| return table; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment