-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment02-AlomaLopes.html
More file actions
207 lines (162 loc) · 5.59 KB
/
Assignment02-AlomaLopes.html
File metadata and controls
207 lines (162 loc) · 5.59 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<body>
<div class="years_dropdown">
<b>Select Option : </b>
<select id = "opts" >
<option value="FirstPointWin">FirstPointWin percentages</option>
<option value="Aces">Number of Aces</option>
</select>
</div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
function update(selection){
var margin = {top: 70, right: 20, bottom: 90, left: 40},
width = 1300 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var color1 = d3.scale.ordinal()
.range(["#006400", "#90EE90"]);
var color2 = d3.scale.ordinal()
.range(["#8B0000", "#FA8072"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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("modifiedDf.csv", function(error, data) {
if (error) throw error;
var FirstPointWin = ["Winner First Point","Opponent First Point"];
var Aces = ["Winner Ace", "Loser Ace"];
data.forEach(function(d) {
d.xplot = FirstPointWin.map(function(name) { return {name: name, value: +d[name]}; }),
d.xplotAces = Aces.map(function(name) { return {name: name, value: +d[name]}; });
});
x0.domain(data.map(function(d) { return d.winner; }));
if(selection == "FirstPointWin"){
x1.domain(FirstPointWin).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.xplot, function(d){ return d.value}); })]);
}
else if(selection == "Aces"){
x1.domain(Aces).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.xplotAces, function(d){ return d.value}); })]);
}
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("font-size", "10px")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-1em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(selection);
var winner = svg.selectAll(".winner")
.data(data)
.enter().append("g")
.attr("class", "winner")
.attr("transform", function(d) { return "translate(" + x0(d.winner) + ",0)"; });
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "17px")
.style("text-decoration", "underline")
.text("FirstPointWin - Driving force for Aces and recognises best servers");
if(selection == "FirstPointWin"){
winner.selectAll("rect")
.data(function(d) { return d.xplot; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("margin", "1em")
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color1(d.name); });
var legend = svg.selectAll(".legend")
.data(FirstPointWin.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color1);
}
else if(selection == "Aces"){
winner.selectAll("rect")
.data(function(d) { return d.xplotAces; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("margin", "1em")
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color2(d.name); });
var legend = svg.selectAll(".legend")
.data(Aces.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color2);
}
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
}
update("FirstPointWin");
d3.select('#opts')
.on('change', function() {
var selected = this.value;
d3.select("svg").remove();
d3.select("#drop").html("");
update(selected);
});
</script>
</body>