-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
202 lines (179 loc) · 4.84 KB
/
script.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* global d3 */
/* eslint-disable max-len */
// eslint-disable-next-line no-unused-vars
var projectName = "scatter-plot";
// RETURN TO GIDCDN link once chances propogate
// coded by @yellowflash2041
var url =
"https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/cyclist-data.json";
var margin = {
top: 100,
right: 20,
bottom: 30,
left: 60,
},
width = 920 - margin.left - margin.right,
height = 630 - margin.top - margin.bottom;
var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleTime().range([0, height]);
var color = d3.scaleOrdinal(d3.schemeCategory10);
var timeFormat = d3.timeFormat("%M:%S");
var xAxis = d3.axisBottom(x).tickFormat(d3.format("d"));
var yAxis = d3.axisLeft(y).tickFormat(timeFormat);
// Define the div for the tooltip
var div = d3
.select("body")
.append("div")
.attr("class", "tooltip")
.attr("id", "tooltip")
.style("opacity", 0);
var svg = d3
.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("class", "graph")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json(url)
.then((data) => {
data.forEach(function (d) {
d.Place = +d.Place;
var parsedTime = d.Time.split(":");
d.Time = new Date(1970, 0, 1, 0, parsedTime[0], parsedTime[1]);
});
x.domain([
d3.min(data, function (d) {
return d.Year - 1;
}),
d3.max(data, function (d) {
return d.Year + 1;
}),
]);
y.domain(
d3.extent(data, function (d) {
return d.Time;
})
);
svg
.append("g")
.attr("class", "x axis")
.attr("id", "x-axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "x-axis-label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Year");
svg
.append("g")
.attr("class", "y axis")
.attr("id", "y-axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Best Time (minutes)");
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -160)
.attr("y", -44)
.style("font-size", 18)
.text("Time in Minutes");
svg
.selectAll(".dot")
.data(data)
.enter()
.append("circle")
.attr("class", "dot")
.attr("r", 6)
.attr("cx", function (d) {
return x(d.Year);
})
.attr("cy", function (d) {
return y(d.Time);
})
.attr("data-xvalue", function (d) {
return d.Year;
})
.attr("data-yvalue", function (d) {
return d.Time.toISOString();
})
.style("fill", function (d) {
return color(d.Doping !== "");
})
.on("mouseover", function (event, d) {
div.style("opacity", 0.9);
div.attr("data-year", d.Year);
div
.html(
d.Name +
": " +
d.Nationality +
"<br/>" +
"Year: " +
d.Year +
", Time: " +
timeFormat(d.Time) +
(d.Doping ? "<br/><br/>" + d.Doping : "")
)
.style("left", event.pageX + "px")
.style("top", event.pageY - 28 + "px");
})
.on("mouseout", function () {
div.style("opacity", 0);
});
// title
svg
.append("text")
.attr("id", "title")
.attr("x", width / 2)
.attr("y", 0 - margin.top / 2)
.attr("text-anchor", "middle")
.style("font-size", "30px")
.text("Doping in Professional Bicycle Racing");
// subtitle
svg
.append("text")
.attr("x", width / 2)
.attr("y", 0 - margin.top / 2 + 25)
.attr("text-anchor", "middle")
.style("font-size", "20px")
.text("35 Fastest times up Alpe d'Huez");
var legendContainer = svg.append("g").attr("id", "legend");
var legend = legendContainer
.selectAll("#legend")
.data(color.domain())
.enter()
.append("g")
.attr("class", "legend-label")
.attr("transform", function (d, i) {
return "translate(0," + (height / 2 - i * 20) + ")";
});
legend
.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend
.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function (d) {
if (d) {
return "Riders with doping allegations";
} else {
return "No doping allegations";
}
});
})
.catch((err) => console.log(err));