This is the code for Chapter 3, Figure 10 from D3.js in Action demonstrating how to use the .classed() function to highlight elements and change font-size by changing the CSS class of an element.
xxxxxxxxxx
<html>
<head>
<title>D3 in Action Chapter 3 - Example 6</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;
}
</style>
<body>
<div id="controls"></div>
<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});
var dataKeys = d3.keys(incomingData[0])
.filter(function (el) {return el != "team" && el != "region"})
d3.select("#controls").selectAll("button.teams").data(dataKeys).enter().append("button")
.on("click", buttonClick)
.html(function(d) {return d});
function buttonClick(datapoint) {
var maxValue = d3.max(incomingData,
function(d) {return parseFloat(d[datapoint])
});
var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]);
d3.selectAll("g.overallG").select("circle").transition().duration(1000).attr("r", function(d) {return radiusScale(d[datapoint])})
}
teamG.on("mouseover", highlightRegion);
teamG.on("mouseout", unHighlight);
function highlightRegion(d,i) {
d3.select(this).select("text").attr("y", 10).classed("highlight", true);
d3.selectAll("g.overallG")
.each(function(p,i) {
p.region == d.region ?
d3.select(this).select("circle").classed("active",true) :
d3.select(this).select("circle").classed("inactive",true)
})
this.parentElement.appendChild(this);
}
function unHighlight() {
d3.selectAll("g.overallG").select("circle").classed("active",false).classed("inactive",false);
d3.selectAll("g.overallG").select("text").attr("y", 30).classed("highlight", false);
}
}
</script>
</footer>
</html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js