This is the code for Chapter 3, Figure 19 from D3.js in Action showing how to load external HTML content to populate an infobox, and also how to dynamically populate the contents of the infobox based on data bound to an element.
Last active
March 18, 2016 04:57
-
-
Save emeeks/4a84285a4b280f62e671 to your computer and use it in GitHub Desktop.
Ch. 3, Fig. 19 - D3.js in Action
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
<html> | |
<head> | |
<title>D3 in Action Chapter 3 - Example 9</title> | |
<meta charset="utf-8" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
</head> | |
<style> | |
svg { | |
height: 500px; | |
width: 500px; | |
border: 1px solid gray; | |
} | |
text { | |
font-size: 10px; | |
} | |
g > text.active { | |
font-size: 30px; | |
} | |
circle { | |
fill: pink; | |
stroke: black; | |
stroke-width: 1px; | |
} | |
circle.active { | |
fill: red; | |
} | |
circle.inactive { | |
fill: gray; | |
} | |
.highlight { | |
font-size: 24px; | |
} | |
#modal { | |
position:fixed; | |
left:20px; | |
top:20px; | |
z-index:1; | |
background: white; | |
border: 1px black solid; | |
box-shadow: 10px 10px 5px #888888; | |
} | |
tr { | |
border: 1px gray solid; | |
} | |
td { | |
font-size: 10px; | |
} | |
td.data { | |
font-weight: 900; | |
} | |
</style> | |
<body> | |
<div id="viz"> | |
<svg> | |
</svg> | |
</div> | |
<div id="controls" /> | |
</body> | |
<footer> | |
<script> | |
d3.csv("worldcup.csv", function(data) { | |
overallTeamViz(data); | |
}) | |
function overallTeamViz(incomingData) { | |
d3.select("svg") | |
.append("g") | |
.attr("id", "teamsG") | |
.attr("transform", "translate(50,300)") | |
.selectAll("g") | |
.data(incomingData) | |
.enter() | |
.append("g") | |
.attr("class", "overallG") | |
.attr("transform", function (d,i) {return "translate(" + (i * 50) + ", 0)"}); | |
var teamG = d3.selectAll("g.overallG") | |
.on("click", teamClick); | |
teamG | |
.append("circle") | |
.attr("r", 20); | |
teamG | |
.append("text") | |
.style("text-anchor", "middle") | |
.attr("y", 30) | |
.text(function(d) {return d.team}); | |
d3.selectAll("g.overallG").insert("image", "text") | |
.attr("xlink:href", function(d) { | |
return "" + d.team + ".png" | |
}) | |
.attr("width", "45px").attr("height", "20px").attr("x", "-22") | |
.attr("y", "-10") | |
d3.text("modal.html", function(data) {d3.select("body").append("div").attr("id", "modal").html(data)}); | |
function teamClick(d) { | |
d3.selectAll("td.data").data(d3.values(d)).html(function(p) {return p}) | |
} | |
} | |
</script> | |
</footer> | |
</html> |
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
<table> | |
<tr> | |
<th>Statistics</th> | |
</tr> | |
<tr><td>Team Name</td><td class="data"></td></tr> | |
<tr><td>Region</td><td class="data"></td></tr> | |
<tr><td>Wins</td><td class="data"></td></tr> | |
<tr><td>Losses</td><td class="data"></td></tr> | |
<tr><td>Draws</td><td class="data"></td></tr> | |
<tr><td>Points</td><td class="data"></td></tr> | |
<tr><td>Goals For</td><td class="data"></td></tr> | |
<tr><td>Goals Against</td><td class="data"></td></tr> | |
<tr><td>Clean Sheets</td><td class="data"></td></tr> | |
<tr><td>Yellow Cards</td><td class="data"></td></tr> | |
<tr><td>Red Cards</td><td class="data"></td></tr> | |
</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
team | region | win | loss | draw | points | gf | ga | cs | yc | rc | |
---|---|---|---|---|---|---|---|---|---|---|---|
Netherlands | UEFA | 6 | 0 | 1 | 18 | 12 | 6 | 2 | 23 | 1 | |
Spain | UEFA | 6 | 0 | 1 | 18 | 8 | 2 | 5 | 8 | 0 | |
Germany | UEFA | 5 | 0 | 2 | 15 | 16 | 5 | 3 | 10 | 1 | |
Argentina | CONMEBOL | 4 | 0 | 1 | 12 | 10 | 6 | 2 | 8 | 0 | |
Uruguay | CONMEBOL | 3 | 2 | 2 | 11 | 11 | 8 | 3 | 13 | 2 | |
Brazil | CONMEBOL | 3 | 1 | 1 | 10 | 9 | 4 | 2 | 9 | 2 | |
Ghana | CAF | 2 | 2 | 1 | 8 | 5 | 4 | 1 | 12 | 0 | |
Japan | AFC | 2 | 1 | 1 | 7 | 4 | 2 | 2 | 4 | 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment