-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
271 lines (225 loc) · 7.89 KB
/
script.js
File metadata and controls
271 lines (225 loc) · 7.89 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
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
var dataset;
var current;
// Load JSON from file
function load(filename){
d3.json(filename, function(error, json) {
if (error) return console.warn(error);
dataset = json
d3.select("#snapshot-slider").attr("max",
dataset["snapshots"].length - 1);
current = -1;
next(true);
});
}
/* Max func: http://ejohn.org/blog/fast-javascript-maxmin/ */
Array.max = function( array ){
return Math.max.apply( Math, array );
};
// Dimension variables
var box_size = {w: 70, h:50};
var array_padding = 8;
var text_padding = {x: 8, y:8};
var width = 500;
var height = 250;
var playing = false;
var myInterval;
// Color variables
var defaultColor = d3.rgb("rgb(135,206,235)");
var orange = d3.rgb("rgb(250,198,85)");
var textColor = d3.rgb("rgb(255,255,255)");
var weightedColors = false;
//Create SVG container
var svg = d3.select("#canvas")
.append("svg")
.attr("width", width)
.attr("height", height);
// Visualise array function
function visualise(num){
// Remove all SVG elements from container
svg.selectAll("*").remove();
// Snapshot variable
var snapshot = dataset.snapshots[num];
// Show snapshot number in corner
d3.select("#snapshot-counter")
.html("type: " + snapshot.type + "<Br />snapshot: " + num);
// Update snapshot progress slider
d3.select("#snapshot-slider").property("value", num);
// Check if the snapshot is of type ARRAY
switch (snapshot.type) {
case "ARRAY":
case "2D-ARRAY":
// Get max array size
var firstd = snapshot.data.length;
var twoddata = snapshot.data.slice();
var secondd = (snapshot.type == "2D-ARRAY" ? twoddata.map(function(s){return s.length;}) : null);
// Set width of canvas dependent on size
d3.select("#canvas svg")
.attr("width", (array_padding +
((secondd ? Array.max(secondd) : firstd) * (box_size.w +array_padding))));
// Set height of canvas dependent on 2D size
var twodheight = (array_padding + (firstd * (box_size.h + array_padding)));
d3.select("#canvas svg")
.attr("height", (secondd && twodheight > height ? twodheight : height));
for(var k = 0; k < (secondd ? firstd : 1); k++){
var data = (secondd ? snapshot.data[k] : snapshot.data);
// Create SVG grouping element for each data item
var elem = svg.selectAll(".array-node" + k)
.data(data)
.enter().append("g")
.attr("class", "array-node" + k + " array-node")
.attr("transform", function(d, i) {
return "translate(" + (array_padding + (i * (box_size.w +array_padding)))
+ "," + (array_padding + (k * (box_size.h +array_padding))) + ")";
});
// Set range for weighted colors
if(weightedColors)
range = getRange(data);
else
range = [0,0]
// Add main box to group
elem.append("rect")
.attr("width", box_size.w)
.attr("height", box_size.h)
.attr("class", function(d){
return d.highlighted ? "highlighted" : null;
})
.attr("fill", function(d) {
return getColor(d, range);
});
// Add text to group
elem.append("text")
.attr("dy", box_size.h / 2 + array_padding)
.attr("dx", box_size.w / 2)
.attr("style", function(d){
return d.highlighted ? "font-weight: bold" : null;
})
.attr("fill", textColor)
.attr("text-anchor", "middle")
.text(function(d) { return d.value; });
}
break;
case "BINARY-TREE":
// Initialise tree variables
var tree = d3.layout.tree()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var treecontainer = svg.append("g")
.attr("transform", "translate(40,0)");
// Initialise tree nodes and links from data set
var nodes = tree.nodes(snapshot.data),
links = tree.links(nodes);
// Set width of canvas dependent on size
d3.select("#canvas svg")
.attr("width", (array_padding +
(nodes.length * (box_size.w + array_padding))));
tree.size([height, (array_padding +
(nodes.length * (box_size.w + array_padding)))]);
// Create link elements from link array
var link = treecontainer.selectAll("path.link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
// Create node elements from nodes array
var node = treecontainer.selectAll("g.tree-node")
.data(nodes)
.enter().append("g")
.attr("class", "tree-node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
})
// Add circle with radius 4.5 to node elements
node.append("circle")
.attr("r", 4.5);
// Add text to node elements
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.attr("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.value; });
break;
}
}
function next(forward){
// Change current counter to desired number
current = current + (forward ? 1 : -1);
// Disable buttons if at the end of the snapshots array
d3.selectAll(".direction").attr("disabled", null);
if(current >= dataset.snapshots.length - 1){
d3.select("#next").attr("disabled", "disabled");
d3.select("#play").html("<b>↺</b>");
playing = false;
stopInterval();
}
else if(current == 0)
d3.select("#previous").attr("disabled", "disabled");
// Visualise desired number
visualise(current);
}
function play(){
if(current >= dataset.snapshots.length - 1)
reset();
// set playing button value
d3.select("#play").html(!playing ? "▮▮" : "▶");
//get the speed from the input slider
speed = d3.select("#speed").property("value");
//call next with the desired interval
if(!playing && current != dataset.snapshots.length - 1)
myInterval = setInterval(function () {next(true)}, speed);
else
stopInterval();
playing = !playing;
}
function changeSpeed(speed){
stopInterval();
if(playing && current != dataset.snapshots.length - 1)
myInterval = setInterval(function () {next(true)}, speed);
d3.select("#speed-value").html(speed + "ms");
}
function reset(){
//reset current to the first snapshot
current = 0;
playing = false;
stopInterval();
//enable/disable relevant buttons
d3.select("#play").html("▶");
d3.selectAll(".direction").attr("disabled", null);
d3.select("#previous").attr("disabled", "disabled");
//visualise first snapshot
visualise(current);
}
function stopInterval(){
clearInterval(myInterval);
}
function getColor(d, range){
if (!weightedColors)
return defaultColor;
else {
c = d3.scale.linear().domain([range[0],range[1]]).range([defaultColor, orange]);
return c(d.value);
}
}
function getRange(data) {
min = Infinity;
max = -Infinity;
for(i = 0; i < data.length; i++){
if(data[i].value > max)
max = data[i].value;
if(data[i].value < min)
min = data[i].value;
}
return [min, max]
}
function toggleWeightedColors(){
weightedColors = !weightedColors;
if(weightedColors)
d3.select("#colors").text("Disable Weighted Colors");
else
d3.select("#colors").text("Enable Weighted Colors");
visualise(current);
}
function sliderMove(slider){
current = +slider.value;
visualise(slider.value);
}