-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatterplot.html
138 lines (117 loc) · 4.94 KB
/
scatterplot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Data Visualization</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
</head>
<body>
<div id="dataviz"></div>
<script>
var margin = { top: 10, right: 100, bottom: 30, left: 30 },
width = 800 - margin.left - margin.right,
height = 400 - 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 (filedata) {
var allGroup = ["United States", "Russia", "Canada"]
var diversity = {};
for (var i = 0; i < filedata.length; i++) {
var y = filedata[i]['Year'];
var c = filedata[i]['Country'];
if(c === 'United States' || c === 'Russia' || c === 'Canada') {
if (diversity[y]) {
diversity[y][c] = diversity[y][c] + 1;
}
else {
if (c === 'United States') {
diversity[y] = { "United States": 1, "Russia": 0, "Canada" : 0};
}
else if (c === 'Russia') {
diversity[y] = { "United States": 0, "Russia": 1, "Canada" : 0};
}
else {
diversity[y] = { "United States": 0, "Russia": 0, "Canada" : 1};
}
}
}
}
var data = [];
for(d in diversity) {
data.push({"Year" : d,
"United States" : diversity[d]['United States'],
"Russia" : diversity[d]['Russia'],
"Canada" : diversity[d]['Canada']});
}
var dataReady = allGroup.map(function (grpName) {
return {
name: grpName,
values: data.map(function (d) {
return { year: d.Year, value: +d[grpName] };
})
};
});
var myColor = d3.scaleOrdinal()
.domain(allGroup)
.range(d3.schemeSet1);
var x = d3.scaleLinear()
.domain(d3.extent(data, function (d) { return d.Year; }))
.range([0, width]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickFormat(d3.format("d")));
var y = d3.scaleLinear()
.domain([0, 120])
.range([height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
var line = d3.line()
.x(function (d) { return x(+d.year) })
.y(function (d) { return y(+d.value) })
svg.selectAll("myLines")
.data(dataReady)
.enter()
.append("path")
.attr("d", function (d) { return line(d.values) })
.attr("stroke", function (d) { return myColor(d.name) })
.style("stroke-width", 2)
.style("fill", "none")
svg
.selectAll("myDots")
.data(dataReady)
.enter()
.append('g')
.style("fill", function (d) { return myColor(d.name) })
.selectAll("myPoints")
.data(function (d) { return d.values })
.enter()
.append("circle")
.attr("cx", function (d) { return x(d.year) })
.attr("cy", function (d) { return y(d.value) })
.attr("r", 4)
.attr("stroke", "white")
svg
.selectAll("myLabels")
.data(dataReady)
.enter()
.append('g')
.append("text")
.datum(function (d) { return { name: d.name, value: d.values[d.values.length - 1] }; })
.attr("transform", function (d) { return "translate(" + x(d.value.year) + "," + y(d.value.value) + ")"; })
.attr("x", 12)
.text(function (d) { return d.name; })
.style("fill", function (d) { return myColor(d.name) })
.style("font-size", 12)
svg.append("text").attr("x", 200).attr("y", 20)
.text("Winter Olympics Medals trend by Year")
.style("font-size", "15px").attr("alignment-baseline","left")
})
</script>
</body>
</html>