-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
438 lines (351 loc) · 12.7 KB
/
index.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title> Winner Statistics of Australian Open </title>
<link rel='stylesheet' href='D3Hw.css'>
<script src = "https://d3js.org/d3.v3.min.js"></script>
<h1>Statistics of the Australian Open Winners from the Last 10 years</h1>
</head>
<body>
<div id="option">
<input name="updateButton"
type="button"
value="All Years"
onclick="updateData()" />
</div>
<script>
// Variables declaration
var margin = { top: 30, right: 10, bottom: 10, left: 10},
width = 650 - margin.left - margin.right, //960
height = 400 - margin.top - margin.bottom; //500
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {},
dragging = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
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 + ")");
// Add the SVG for the second chart
var svg2 = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right - 60)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
updateData();
// Define the Div element variable to use for Tool tip
var div = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Fetch data from local machine
d3.csv("10yearAUSOpenMatches (version 1).xlsb.csv", function( error, tennis )
{
x.domain(dimensions = d3.keys(tennis[0]).filter(function(d)
{
return ((d == "year") || (d == "firstServe1" ) || (d == "ace1") || (d == "double1") || (d == "firstPointWon1") || (d == "fastServe1") || (d == "break1"))
&& (
y[d] = d3.scale.linear()
.domain(d3.extent(tennis, function(p) {
return +p[d];
}))
.range([height, 0]) ); }
));
var tennis_new = tennis.filter(function (d)
{
return d["round"] == "Final"; }
);
// Add colors for each of the years
// var color = d3.scale.category10();
background = svg.append("g")
.attr("class", "background")
.selectAll("path")
.data(tennis_new)
.enter().append("path")
.attr("d", path);
// Add blue foreground lines for focus.
foreground = svg.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(tennis_new)
.enter().append("path")
//.attr({'style': function(d)
// {return "stroke: " + color(d.year)} })
.attr("d", path);
// Add a group element for each dimension.
var g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return {x: x(d)}; })
.on("dragstart", function(d) {
dragging[d] = x(d);
background.attr("visibility", "hidden");
})
.on("drag", function(d) {
dragging[d] = Math.min(width, Math.max(0, d3.event.x));
foreground.attr("d", path);
dimensions.sort(function(a, b) { return position(a) - position(b); });
x.domain(dimensions);
g.attr("transform", function(d) { return "translate(" + position(d) + ")"; })
})
.on("dragend", function(d) {
delete dragging[d];
transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
transition(foreground).attr("d", path);
background
.attr("d", path)
.transition()
.delay(500)
.duration(0)
.attr("visibility", null);
}));
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(axis.scale(y[d])); })
.append("text")
.attr("class", "title")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d; });
//Rebind the axis
svg.select(".axis").selectAll("text:not(.title)")
.attr("class", "label")
.data(tennis, function(d){return d.year || d; });
var projection = svg.selectAll(".axis text:not(.title),.background path, .foreground path")
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.on("click", click)
.on();
function mouseover(d){
// Code to highlight
svg.classed("active", true);
projection.classed("inactive", function(p) {return p!== d});
projection.filter(function(p) {return p === d}).each(moveToFront);
// Code to add tool tip
addToolTip(d);
}
function mouseout(d) {
// Code to deavtivate highlighting
svg.classed("active", false);
projection.classed("inactive", false);
// Code to deactivate tooltip
div.transition()
.duration(500)
.style("opacity", 0);
}
function moveToFront() {
this.parentNode.appendChild(this);
}
// Add and store a brush for each axis.
g.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(y[d].brush = d3.svg.brush().y(y[d]).on("brushstart", brushstart).on("brush", brush));
})
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
// Mouse Click Event
function click(d)
{
svg2.selectAll("*").remove();
var width = 180,
height = 180
var force = d3.layout.force()
.gravity(.05)
.distance(40)
.charge(-180)
.size([width, height]);
var file = "data1.json";
switch(d.year)
{
case "2004": file = "data1.json"; break;
case "2005": file = "data2.json";break;
case "2006": file = "data3.json";break;
case "2007": file = "data4.json";break;
case "2008": file = "data5.json";break;
case "2009": file = "data6.json";break;
case "2010": file = "data7.json";break;
case "2011": file = "data8.json";break;
case "2012": file = "data9.json";break;
case "2013": file = "data10.json";break;
case "2014": file = "data11.json";break;
}
d3.json(file, function(json) {
force
.nodes(json.nodes)
.links(json.links)
//.linkStrength(0.05)
.size([width,height])
.start();
var linkNodes = [];
json.links.forEach(function(link) {
linkNodes.push({
source: json.nodes[link.source],
target: json.nodes[link.target]
});
});
var link = svg2.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", 2);
var node = svg2.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r",function(d){if (d.winner == 'yes'){return '10'}else {return "5"}})
.attr("fill", function(d) { if (d.winner == 'yes'){return "green";} else {return "blue"} });
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.id});
link.append("title")
.text(function(d){ return d.result })
var linkNode = svg.selectAll(".link-node")
.data(linkNodes)
.enter().append("circle")
.attr("class", "link-node")
.attr("r", 2)
.style("fill", "#ccc");
force.on("tick", function() {
var width = 600,
height = 400;
node
.attr("cx", function(d)
{ return d.x = Math.max(6, Math.min(width - 6, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(6, Math.min(height - 6, d.y));})
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
linkNode.attr("cx", function(d) { return d.x = (d.source.x + d.target.x) * 0.5; })
.attr("cy", function(d) { return d.y = (d.source.y + d.target.y) * 0.5; });
});
});
}
function position(d) {
var v = dragging[d];
return v == null ? x(d) : v;
}
function transition(g) {
return g.transition().duration(500);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) { return [position(p), y[p](d[p])]; }));
}
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
// Function to handle brush event
function brush() {
var actives = dimensions.filter(function(p) { return !y[p].brush.empty(); }),
extents = actives.map(function(p) { return y[p].brush.extent(); });
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
// Function to add tool tip
function addToolTip(d){
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d["player1"])
.style("right", (1200 - d3.event.pageX ) + "px")
.style("top",(d3.event.pageY) + "px");
}
})
function updateData(){
svg2.selectAll("*").remove();
var width = 180,
height = 180
var force = d3.layout.force()
.gravity(.01)
.distance(40)
.charge(-120)
.size([width, height]);
var file = "data_all.json";
d3.json(file, function(json) {
force
.nodes(json.nodes)
.links(json.links)
//.linkStrength(0.05)
.size([width,height])
.start();
var linkNodes = [];
json.links.forEach(function(link) {
linkNodes.push({
source: json.nodes[link.source],
target: json.nodes[link.target]
});
});
var link = svg2.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", 2);
var node = svg2.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r",function(d){if (d.winner == 'yes'){return '10'}else {return "5"}})
.attr("fill", function(d) { if (d.winner == 'yes'){return "green";} else {return "blue"} });
node.append("title")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.id});
var linkNode = svg.selectAll(".link-node")
.data(linkNodes)
.enter().append("circle")
.attr("class", "link-node")
.attr("r", 2)
.style("fill", "#ccc");
var linkNode = svg.selectAll(".link-node")
.data(linkNodes)
.enter().append("circle")
.attr("class", "link-node")
.attr("r", 2)
.style("fill", "#ccc");
// link.append("title")
// .text(function(d){ return d.result })
force.on("tick", function() {
var width = 600,
height = 400;
node
.attr("cx", function(d)
{ return d.x = Math.max(6, Math.min(width - 6, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(6, Math.min(height - 6, d.y));})
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
linkNode.attr("cx", function(d) { return d.x = (d.source.x + d.target.x) * 0.5; })
.attr("cy", function(d) { return d.y = (d.source.y + d.target.y) * 0.5; });
});
});
}
</script>
<p> The first graph shows the statistics of all the winners from the last 10 years using a parallel co-ordinate chart.
It showcases and compares the final match statistis of each of the winners, describing how they handled their respective victories.
the interactive functionalities of the first graph include: Dragging the axis, brushing along the axis to highlight, hovering over a line or year to highlight individual lines, a tooltip on hovering over the years to mention the winner of that year, click on line or year to navigate to the second graph.
The Second graph is a force directed layout graph (Click on year to get detail, by default the overview is shown) which displays the players that the winner has fought against (from the fourth round to Final) and by what margin he won against them. This graph aims to provide detail regarding how the Winner performed during that year. the functionalities of this graph include - Hover over the links to get the match result, pull or drag each of the nodes to move them around and rearange spatial position.
</p>
</body>
</html>