-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_user.html
234 lines (188 loc) · 8.26 KB
/
single_user.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
<!DOCTYPE html>
<html>
<head>
<title>D3 experiment</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js"></script>
<!--
<script type="text/javascript" src="d3/d3.v2.js"></script>
-->
<style type="text/css">
#vis {
font: 10px sans-serif;
font-color: black;
}
</style>
</head>
<body>
<select id="userSelect" onchange="updateSelectedUser(this)">
</select>
<div id="vis">
</div>
<script type="text/javascript">
var labelWidth = 70;
var visWidth = 400;
var visHeight = 300;
var userData = []; // [UserObj, ...]
var usersByTag = new Object();
var selGamerTag = "neowallace";
var vis = d3.select("#vis");
document.getElementById('vis').style.top = '0px';
document.getElementById('vis').style.left = '0px';
document.getElementById('vis').style.width = visWidth + 'px';
document.getElementById('vis').style.padding = "10px 0px 0px 10px";
var svg = d3.select("#vis")
.append("svg")
.attr("width", labelWidth + visWidth)
.attr("height", visHeight);
var labelSVG = svg.append("svg:g")
.attr("width", labelWidth)
.attr("height", visHeight);
var chart = svg.append("svg:g")
.attr("width", visWidth)
.attr("height", visHeight)
.attr("transform", "translate(" + labelWidth + ", 0)");
var cmpFields = [
"Total XP",
"Quest XP",
"Active",
"Completed",
"Dropped",
"Reward XP",
"Badges",
"Achievements",
"Awards",
"Activity",
"Ratings",
"Avg. Rating",
"Avg. Time",
"Comments",
];
var yScale;
var fieldStats = new Object();
var fieldScales = new Object();
function draw() {
var selUser = usersByTag[selGamerTag];
var bandSize = yScale.rangeBand();
var bars = chart.selectAll("rect")
.data(cmpFields);
var meanLines = chart.selectAll(".mean_line")
.data(cmpFields);
//console.debug(fieldStats);
bars.enter()
.append("rect")
.attr("x", 0)
.attr("y", function(d) { return yScale(d); })
.attr("width", function(d) { return fieldScales[d](selUser[d]); })
.attr("height", bandSize)
.style("fill", "gray");
bars.transition()
.duration(1000)
.attr("width", function(d) { return fieldScales[d](selUser[d]); });
meanLines.enter()
.append("line")
.attr("class", "mean_line")
.attr("x1", function(d) { return fieldScales[d](fieldStats[d].mean); })
.attr("x2", function(d) { return fieldScales[d](fieldStats[d].mean); })
.attr("y1", function(d) { return yScale(d); })
.attr("y2", function(d) { return yScale(d) + bandSize; })
.attr("stroke", "#555");
var labels = labelSVG.selectAll("text")
.data(cmpFields);
labels.enter()
.append("text")
.attr("text-anchor", "end")
.attr("x", labelWidth)
.attr("y", function(d) { return yScale(d) + bandSize / 2; })
.attr("dx", -4) // padding right
.attr("dy", ".35em") // valign middle
.text(String);
}
function updateScales() {
yScale = d3.scale.ordinal()
.domain(cmpFields)
.rangeRoundBands([0, visHeight], 0.25);
//.rangeRoundBands([0, visHeight]);
for (var i=0; i<cmpFields.length; ++i) {
var fname = cmpFields[i];
fieldScales[fname] = d3.scale.linear()
.domain([fieldStats[fname].min, fieldStats[fname].max])
.rangeRound([0, visWidth]);
}
}
function updateFieldStats() {
// this is an inefficient design to calculate min, max, mean, etc...
// from the whole data set every time. A better design would be to
// do this in a batch job and pass in the numbers directly
for (var i=0; i<cmpFields.length; ++i) {
var fname = cmpFields[i];
var field = new Object();
field.min = d3.min(userData, function(d) { return d[fname]; });
field.max = d3.max(userData, function(d) { return d[fname]; });
field.mean = d3.mean(userData, function(d) { return d[fname]; });
fieldStats[fname] = field;
}
}
/**
* convert a single record with string values
* into a user object with proper types
*/
function createUserObj(namedRec) {
var user = new Object();
user["GamerTag"] = namedRec["GamerTag"];
user["Name"] = namedRec["Name"];
user["Last Initial"] = namedRec["Last Initial"];
user["Rank/Grade"] = namedRec["Rank/Grade"]; // ordinal field...
user["Total XP"] = parseInt(namedRec["Total XP"]);
user["Quest XP"] = parseInt(namedRec["Quest XP"]);
user["Active"] = parseInt(namedRec["Active"]);
user["Completed"] = parseInt(namedRec["Completed"]);
user["Dropped"] = parseInt(namedRec["Dropped"]);
user["Reward XP"] = parseInt(namedRec["Reward XP"]);
user["Badges"] = parseInt(namedRec["Badges"]);
user["Achievements"] = parseInt(namedRec["Achievements"]);
user["Awards"] = parseInt(namedRec["Awards"]);
user["Activity"] = parseInt(namedRec["Activity"]);
user["Persistence"] = namedRec["Persistence"];
user["Ratings"] = parseInt(namedRec["Ratings"]);
user["Avg. Rating"] = parseFloat(namedRec["Avg. Rating"]);
user["Avg. Time"] = parseFloat(namedRec["Avg. Time"]);
user["Comments"] = parseInt(namedRec["Comments"]);
return user;
}
function preprocessUserData(data) {
if (data == null) {
console.error("null data passed into preprocessUserData");
return null;
}
var newUserData = new Array();
for (var i=0; i < data.length; i++) {
var user = createUserObj(data[i]);
newUserData.push(user);
usersByTag[user["GamerTag"]] = user;
}
return newUserData;
}
function updateUserControl() {
var users = d3.select("#userSelect").selectAll("option")
.data(userData, function(d) { return d["GamerTag"]; });
users.enter()
.append("option")
.text(function(d) { return d["GamerTag"]; });
users.exit().remove();
}
function updateSelectedUser() {
var sel = document.getElementById("userSelect");
selGamerTag = sel.options[sel.selectedIndex].text;
draw();
}
var fname = "user_report.csv";
d3.csv(fname, function(data) {
userData = preprocessUserData(data);
updateUserControl();
updateFieldStats();
updateScales();
draw();
});
</script>
</body>
</html>