forked from jalapic/jalapic.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtennis1.html
More file actions
216 lines (155 loc) · 5.53 KB
/
tennis1.html
File metadata and controls
216 lines (155 loc) · 5.53 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
fill:none;
stroke:#000;
shape-rendering: crispEdges;
}
.axis text {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 14px;
}
.line {
fill: none;
stroke-width: 2px;
}
h1{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 22px;
font-style: bold;
}
p{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12px;
}
div.tooltip {
position: absolute;
text-align: left;
width: 60px;
height: 28px;
padding: 2px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 14px;
font-style: bold;
color: darkblue;
background: none;
border: none;
pointer-events: none;
}
</style>
<h1> Cumulative Grand Slams by Female Tennis Players</h1>
<br>
<br>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 20, right: 80, bottom: 40, left: 50},
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0,width]);
var y = d3.scale.linear()
.range([height,0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.format("d"));;
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.titles); });
var svg = d3.select("body").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.csv("data/tennis1.csv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key == "player"; }));
// get data into format
data = data.map( function (d) {
return {
player: d.player,
date: +d.date,
titles: +d.titles };
});
// nest data
data = d3.nest().key(function(d) { return d.player; }).entries(data);
x.domain([d3.min(data, function(d) { return d3.min(d.values, function (d) { return d.date; }); }),
d3.max(data, function(d) { return d3.max(d.values, function (d) { return d.date; }); })]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.values, function (d) { return d.titles; }); })]);
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var cities = svg.selectAll(".player")
.data(data, function(d) { return d.key; })
.enter().append("g")
.attr("class", "player");
cities.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.key); })
//;
.on("mouseover", function(d) {
d3.select(this)
.style("stroke", 'darkblue')
.style("stroke-width", '4px');
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.key + "<br/>")
.style("left", (d3.event.pageX+12) + "px")
.style("top", (d3.event.pageY-60) + "px");
})
.on("mouseout", function(d) {
d3.select(this)
.style("stroke", function(d) { return color(d.key); })
.style("stroke-width", '2px')
div.transition()
.duration(500)
.style("opacity", 0);
});
// text label for the x axis
svg.append("text")
.attr("x", width / 2 )
.attr("y", height + (margin.bottom/1))
// .attr("style","font-size:16px;")
.attr("style","font-size:16px; font-weight: bold;") // to bold title
.style("text-anchor", "middle")
.text("Year");
// Add the text label for the Y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
// .attr("style","font-size:16px;")
.attr("style","font-size:16px; font-weight: bold;") // to bold title
.style("text-anchor", "middle")
.text("Cumulative Grand Slams");
});
</script>
<br>
<p>A quick <a href="http://d3js.org/">d3.js</a> visualization of the cumulative increase in grand slams by female tennis players. Once a player starts to dominate their rise in dominance over other players is incredible. </p>
<p> If I was to invest more time in this, I would certainly improve the tooltip to include more information about tournaments won and nationality. I could also make dots on the line to give more information about the grand slam won (e.g. opponent, score). This quick visualization gives a good impression though of the data trends. Code and data are <a href="https://github.com/jalapic/jalapic.github.io">available here </a>. Please get in touch via twitter - <a href="https://twitter.com/jalapic">@jalapic</a> </p>