A Pen by PJ Trainor on CodePen.
Last active
November 27, 2017 16:30
-
-
Save trainorpj/13084b9c6ec17bee3f0db7cc23ff73f3 to your computer and use it in GitHub Desktop.
D3 Table (IBM Carbon)
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
<div id="title" class="bx--title"> | |
<h1>D3 + ES6</h1> | |
<h2>IBM Carbon Components</h2> | |
<h3>🏅 Olympic Medal Data 🏅</h3> | |
</div> | |
<div id="viz"></div> |
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
const url = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1245865/summer-olympics.csv"; | |
// use es6 template literals to populate rows | |
const rowTemplate = (d) => { | |
return ` | |
<td>${d["No."]}</td> | |
<td>${d.Nation}</td> | |
<td>${d.Games}</td> | |
<td>${d.Gold}</td> | |
<td>${d.Silver}</td> | |
<td>${d.Bronze}</td> | |
<td>${d.Total}</td> | |
`; | |
}; | |
// read data from the url | |
d3.csv(url, (err, data) => { | |
// select viz and append table | |
// append Carbon Components data table class | |
const table = d3.select("#viz").append("table") | |
.attr("class", "bx--data-table-v2 bx--data-table-v2--zebra"); | |
// append headers | |
const header = table.append("thead") | |
.selectAll('.header') | |
.data(data.columns) | |
.enter() | |
.append('th') | |
.text(d => d); | |
// append rows with rowTemplate | |
const rows = table.append("tbody") | |
.selectAll(".row") | |
.data(data) | |
.enter() | |
.append("tr") | |
.html(rowTemplate); | |
}) |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script> |
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
#title { | |
margin: 2em; | |
} | |
th { | |
padding: 1em; | |
} |
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
<link href="https://unpkg.com/carbon-components@latest/css/carbon-components.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment