|
<html> |
|
<head> |
|
<title>D3 in Action Chapter 3 - Example 10</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> |
|
</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"); |
|
|
|
teamG.append("circle").attr("r", 20); |
|
|
|
teamG.append("text").style("text-anchor", "middle").attr("y", 30).text(function(d) { |
|
return d.team |
|
}); |
|
|
|
d3.html("icon.svg", loadSVG); |
|
|
|
function loadSVG(svgData) { |
|
d3.selectAll("g.overallG").each(function() { |
|
var gParent = this; |
|
d3.select(svgData).selectAll("path").each(function() { |
|
gParent.appendChild(this.cloneNode(true)) |
|
}) |
|
}) |
|
|
|
recolorFootballs(); |
|
} |
|
|
|
function recolorFootballs() { |
|
d3.selectAll("g.overallG").each(function(d) { |
|
d3.select(this).selectAll("path").datum(d) |
|
}) |
|
|
|
var tenColorScale = d3.scale.category10([ "UEFA", "CONMEBOL", "CAF", "AFC" ]); |
|
|
|
d3.selectAll("path").style("fill", function(p) { |
|
return tenColorScale(p.region) |
|
}).style("stroke", "black").style("stroke-width", "2px"); |
|
|
|
} |
|
|
|
} |
|
</script> |
|
</footer> |
|
|
|
</html> |