-
Notifications
You must be signed in to change notification settings - Fork 0
/
linechart.html
108 lines (91 loc) · 3.85 KB
/
linechart.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
<!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: 30, right: 10, bottom: 40, left: 90 },
width = 600 - margin.left - margin.right,
height = 300 - 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 diversity = {};
for (var i = 0; i < data.length; i++) {
var y = data[i]['Year'];
var g = data[i]['Gender'];
if (diversity[y]) {
diversity[y][g] = diversity[y][g] + 1;
}
else {
if (g === 'Men') {
diversity[y] = { "Men": 1, "Women": 0 };
}
else {
diversity[y] = { "Men": 0, "Women": 1 };
}
}
}
var dd = [];
for(d in diversity) {
dd.push({"Year" : d, "Gender" : "Men", "n" : diversity[d]['Men']});
dd.push({"Year" : d, "Gender" : "Women", "n" : diversity[d]['Women']});
}
var sumstat = d3.nest()
.key(function (d) { return d.Gender; })
.entries(dd);
console.log(sumstat);
// Add X axis --> it is a date format
var x = d3.scaleLinear()
.domain(d3.extent(dd, function (d) { return d.Year; }))
.range([0, width]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)
.ticks(22)
.tickFormat(d3.format("d")));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, d3.max(dd, function (d) { return +d.n; })])
.range([height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
// color palette
var res = sumstat.map(function (d) { return d.key }) // list of group names
var color = d3.scaleOrdinal()
.domain(res)
.range(['#e41a1c', '#377eb8']);
console.log(sumstat);
// Draw the line
svg.selectAll(".line")
.data(sumstat)
.enter()
.append("path")
.attr("fill", "none")
.attr("stroke", function (d) { return color(d.key) })
.attr("stroke-width", 1.5)
.attr("d", function (d) {
return d3.line()
.x(function (d) { return x(d.Year); })
.y(function (d) { return y(+d.n); })
(d.values)
})
svg.append("circle").attr("cx",100).attr("cy",28).attr("r", 4).style("fill", "#e41a1c")
svg.append("circle").attr("cx",100).attr("cy",48).attr("r", 4).style("fill", "#377eb8")
svg.append("text").attr("x", 110).attr("y", 30).text("Men").style("font-size", "12px").attr("alignment-baseline","middle")
svg.append("text").attr("x", 110).attr("y", 50).text("Women").style("font-size", "12px").attr("alignment-baseline","middle")
svg.append("text").attr("x", 150).attr("y", 0)
.text("Participation by Gender in Winter Olympics")
.style("font-size", "15px").attr("alignment-baseline","left")
});
</script>
</body>
</html>