Created
April 8, 2014 16:58
-
-
Save jamesduncombe/10155963 to your computer and use it in GitHub Desktop.
CSV to HTML table
This file contains 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
data = """ | |
Make, Model | |
Jag, X-type | |
Merc, CL | |
""" | |
csv = d3.csv.parse(data) | |
columns = Object.keys(csv[0]) | |
# Create the actual body of the table | |
table = d3.select('body').append('table') | |
thead = table.append('thead') | |
tbody = table.append('tbody') | |
# Draw out the header | |
thead.append('tr') | |
.selectAll('th') | |
.data(columns).enter() | |
.append('th') | |
.text (d) -> d | |
# Add the rows | |
rows = tbody.selectAll('tr') | |
.data(csv).enter() | |
.append('tr') | |
# Add the cells | |
cells = rows.selectAll('td') | |
.data((row) -> | |
columns.map((column) -> row[column]) | |
).enter() | |
.append('td').text (d) -> d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment