forked from emeeks/d3_in_action_2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
3_16.html
95 lines (84 loc) · 2.68 KB
/
3_16.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<script src="../js/colorbrewer.js" type="text/javascript"></script>
<style>
text {
font-size: 10px;
text-anchor: middle;
fill: #4f442b;
}
g > text.active {
font-size: 30px;
}
circle {
fill: #75739F;
stroke: black;
stroke-width: 1px;
}
circle.active {
fill: #FE9922;
}
circle.inactive {
fill: #C4B9AC;
}
</style>
</head>
<body>
<div>Click a button to color by region using schemeCategory10</div>
<div id="viz">
<svg style="width:500px;height:500px;" ></svg>
</div>
<div id="controls" ></div>
<script>
d3.csv("../data/worldcup.csv", 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", (d, i) =>`translate(${(i * 50)}, 0)`);
var teamG = d3.selectAll("g.overallG");
teamG
.append("circle").attr("r", 0)
.transition()
.delay((d,i) => i * 100)
.duration(500)
.attr("r", 40)
.transition()
.duration(500)
.attr("r", 20);
teamG
.append("text")
.style("text-anchor", "middle")
.attr("y", 30)
.text(d => d.team);
var dataKeys = Object.keys(incomingData[0])
.filter(d => d !== "team" && d !== "region");
d3.select("#controls").selectAll("button.teams")
.data(dataKeys).enter()
.append("button")
.on("click", buttonClick)
.html(d => d);
function buttonClick(datapoint) {
var maxValue = d3.max(incomingData, function(el) {
return parseFloat(el[datapoint]);
});
var tenColorScale = d3.scaleOrdinal()
.domain(["UEFA", "CONMEBOL", "CAF", "AFC"])
.range(d3.schemeCategory10);
var radiusScale = d3.scaleLinear().domain([0,maxValue]).range([2,20]);
d3.selectAll("g.overallG").select("circle").transition().duration(1000)
.style("fill", p => tenColorScale(p.region))
.attr("r", p => radiusScale(p[datapoint ]));
}
}
</script>
</body>
</html>