block by emeeks ea5760799fce0b3a2e7c

Ch. 3, Fig. 17 - D3.js in Action

Full Screen

This is the code for Chapter 3, Figure 17 from D3.js in Action demonstrating the colorbrewer.js color scales paired with d3.scale.quantize() for discrete categories of quantitative data.

index.html

<html>
<head>
  <title>D3 in Action Chapter 3 - Example 7</title>
  <meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://d3js.org/colorbrewer.v1.min.js" type="text/JavaScript"></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(d) {
 var maxValue = d3.max(incomingData, function(el) {return parseFloat(el[d])});
 var colorQuantize = d3.scale.quantize().domain([0,maxValue]).range(colorbrewer.Reds[3]);
 var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]);
 d3.selectAll("g.overallG").select("circle").transition().duration(1000).style("fill", function(p) {return colorQuantize(p[d])}).attr("r", function(p) {return radiusScale(p[d])})
}

teamG.on("mouseover", highlightRegion);
teamG.on("mouseout", unHighlight);

function highlightRegion(d,i) {
 var teamColor = d3.rgb("pink")
 d3.select(this).select("text").classed("highlight", true).attr("y", 10)
 d3.selectAll("g.overallG").select("circle").style("fill", function(p) {return p.region == d.region ? teamColor.darker(.75) : teamColor.brighter(.5)})
 this.parentElement.appendChild(this);

}

function unHighlight() {
  d3.selectAll("g.overallG").select("circle").style("fill", "pink");
  d3.selectAll("g.overallG").select("text").attr("y", 30).classed("highlight", false);
}


  }

</script>
  </footer>

</html>

worldcup.csv

"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