-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
145 lines (117 loc) · 3.25 KB
/
test.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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<script src="js/d3.js"></script>
<script src="js/d3.layout.cloud.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
</head>
<body>
<select class="drop-down" id="classes">
</select>
<select class="drop-down" id="comparison">
<option>All Classes</option>
<option>Department</option>
<option>Class Family</option>
</select>
<div id="cloud"></div>
<script>
var BY_ALL;
var BY_DEPT;
var BY_FAM;
var SELECTED_DATA;
var SVG_SIZE = [500, 500];
var course_data = "";
var fill = d3.scale.category20();
var COURSE_NAME = "EXPOS 20.241: Paradox in Public Health";
var max_size;
var wordScale;
var classList = function(data) {
var CLASSES = [];
data.forEach(function(d) {
CLASSES.push(d.course);
});
var selected = document.getElementById("classes");
for(i = 0; i < CLASSES.length; i++) {
var opt = document.createElement('option');
// TODO: if class == cs50 make default
opt.innerHTML = CLASSES[i];
opt.value = CLASSES[i];
selected.appendChild(opt);
}
};
var updateData = function() {
// TODO: change these values based on drop-down
COURSE_NAME = $("#classes").val();
SELECTED_DATA = BY_ALL;
course_data = "";
if(SELECTED_DATA != undefined) {
SELECTED_DATA.forEach(function(d) {
if(d.course == COURSE_NAME) {
course_data = d;
}
});
};
max_size = 0;
course_data.reviews.forEach(function(d) {
if(d.size > max_size) {
max_size = d.size + .01
}
});
wordScale = d3.scale.linear().domain([0,max_size]).range([10,160]);
layout_cloud(course_data.reviews);
}
var layout_cloud = function(words) {
d3.layout.cloud()
.size(SVG_SIZE)
.words(words)
.fontSize(function(d) { return wordScale(d.size); })
.on("end", draw)
.start();
function draw(words) {
var cloud_svg = d3.select("#cloud").select("#cloud_svg");
cloud_svg.selectAll("g").remove();
cloud_svg
.append("g")
.attr("transform", "translate(" + SVG_SIZE[0] / 2 + "," + SVG_SIZE[1] / 2 + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
}
var startHere = function() {
queue()
.defer(d3.json, "data/all_courses_all.json")
.defer(d3.json, "data/all_courses_dept.json")
.defer(d3.json, "data/all_courses_fam.json")
.await(initVis);
function initVis(error, all, dept, fam) {
if (!error) {
BY_ALL = all;
BY_DEPT = dept;
BY_FAM = fam;
classList(BY_ALL);
d3.select("#cloud").append("svg")
.attr("id", "cloud_svg")
.attr("width", SVG_SIZE[0])
.attr("height", SVG_SIZE[1])
updateData();
} else {
console.log("error loading data");
}
}
}
startHere();
// listener for select item change
$(".drop-down").change(updateData);
</script>
</body>
</html>