-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.html
124 lines (106 loc) · 4.88 KB
/
parallel.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Data Visualization</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<div id="dataviz"></div>
<script>
var margin = {top: 50, right: 10, bottom: 10, left: 20},
width = 600 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var svg = d3.select("#dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform","translate(" + margin.left + "," + margin.top + ")");
d3.json("/exercise2-olympics.json").then(function(data) {
var countries = {};
for (var i = 0; i < data.length; i++) {
var c = data[i]['Country'];
var m = data[i]['Medal'];
if(countries[c]) {
var medal_count = countries[c];
medal_count[m] = (medal_count[m] + 1);
}
else {
var medal_count = {};
medal_count['Gold'] = 0;
medal_count['Silver'] = 0;
medal_count['Bronze'] = 0;
countries[c] = medal_count;
var medal_count = countries[c];
medal_count[m] = (medal_count[m] + 1);
}
}
const country_list = d3.keys(countries);
var data = [];
for(key in countries) {
var d = {"Country" : key,
"Gold": countries[key]['Gold'],
"Silver" : countries[key]['Silver'],
"Bronze" : countries[key]['Bronze']};
data.push(d);
}
const medal_list = ['Country', 'Gold', 'Silver', 'Bronze'];
var y = {};
var country_scale = d3.scalePoint()
.padding(2)
.domain(country_list)
.range([0, height]);
for(m in medal_list) {
y[medal_list[m]] = d3.scaleSqrt()
.domain(d3.extent(data, function(d) {return +d[medal_list[m]]}))
.range([height, 0]);
}
y["Country"] = country_scale;
var x = d3.scalePoint()
.range([0, width])
.padding(0.5)
.domain(medal_list);
function path(d) {
return d3.line()(medal_list.map(function(p) { return [x(p), y[p](d[p])]; }));
}
var highlight = function(d){
selected_specie = d.Country;
d3.selectAll("." + selected_specie)
.transition().duration(200)
.style("stroke", "#eb4034")
.style("opacity", "1")
}
var doNotHighlight = function(d){
d3.selectAll(".line")
.transition().duration(200).delay(1000)
.style("stroke", "#69b3a2" )
.style("opacity", "0.5")
}
svg.selectAll("myPath")
.data(data)
.enter().append("path")
.attr("d", path)
.attr("class", function (d) { return "line " + d.Country } )
.style("fill", "none")
.style("stroke", "#69b3a2")
.style("opacity", 0.5)
.on("mouseover", highlight)
.on("mouseleave", doNotHighlight );
svg.selectAll("myAxis")
.data(medal_list).enter()
.append("g")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; })
.each(function(d) { d3.select(this).call(d3.axisLeft().scale(y[d])); })
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d; })
.style("fill", "black");
svg.append("text").attr("x", 150).attr("y", -30)
.text("Number of medals by country")
.style("font-size", "15px").attr("alignment-baseline","left")
});
</script>
</body>
</html>