|
|
|
@ -9,23 +9,26 @@ Visualizer.prototype.initItems = function(n) { |
|
|
|
|
|
|
|
|
|
// A swap on the dataset will not take effect until after transition is complete, so custom index is required.
|
|
|
|
|
var n = 0; |
|
|
|
|
for (i in shuffled) { |
|
|
|
|
for (var i in shuffled) { |
|
|
|
|
shuffled[i].index = n++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Items
|
|
|
|
|
this.groups = this.svg.selectAll('g').data(shuffled).enter().append('g') |
|
|
|
|
.attr('transform', `translate(0, ${Visualizer.itemY})`); |
|
|
|
|
|
|
|
|
|
this.groups.append('rect') |
|
|
|
|
.attr('class', 'item') |
|
|
|
|
.attr('height', Visualizer.itemH) |
|
|
|
|
.attr('width', Visualizer.itemW) |
|
|
|
|
.attr('fill', function doFill(d) { return `rgb(0, 0, ${d.value})`; }); |
|
|
|
|
.attr('fill', function doFill(d) { return d.fill; }); |
|
|
|
|
|
|
|
|
|
this.groups.transition(500) |
|
|
|
|
.attr('transform', function doTransform(d, i) { |
|
|
|
|
return `translate(${i * (Visualizer.itemW + Visualizer.spacerW)}, ${Visualizer.itemY})`; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Item labels
|
|
|
|
|
this.groups.append('text') |
|
|
|
|
.text(function t(d) { return d.value; }) |
|
|
|
|
.attr('fill', '#aaa') |
|
|
|
@ -35,24 +38,54 @@ Visualizer.prototype.initItems = function(n) { |
|
|
|
|
return `rotate(90 0,0), translate(5, -3)`; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Markers
|
|
|
|
|
var m1 = this.svg.append('g') |
|
|
|
|
.attr('class', 'marker') |
|
|
|
|
.attr('id', 'marker1'); |
|
|
|
|
|
|
|
|
|
m1.append('rect') |
|
|
|
|
.attr('height', Visualizer.itemW) |
|
|
|
|
.attr('width', Visualizer.itemW); |
|
|
|
|
|
|
|
|
|
m1.append('text') |
|
|
|
|
.attr('text-anchor', 'middle') |
|
|
|
|
.attr('x', (Visualizer.itemW / 2)) |
|
|
|
|
.attr('y', 10); |
|
|
|
|
|
|
|
|
|
var m2 = this.svg.append('g') |
|
|
|
|
//temp
|
|
|
|
|
.attr('transform', 'translate(19,0)') |
|
|
|
|
//temp
|
|
|
|
|
.attr('class', 'marker') |
|
|
|
|
.attr('id', 'marker2'); |
|
|
|
|
|
|
|
|
|
m2.append('rect') |
|
|
|
|
.attr('height', Visualizer.itemW) |
|
|
|
|
.attr('width', Visualizer.itemW); |
|
|
|
|
|
|
|
|
|
m2.append('text') |
|
|
|
|
.attr('text-anchor', 'middle') |
|
|
|
|
.attr('x', (Visualizer.itemW / 2)) |
|
|
|
|
.attr('y', 10); |
|
|
|
|
|
|
|
|
|
setTimeout(this.followInstruction.bind(this, 0), 500); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
Visualizer.prototype.initComments = function() { |
|
|
|
|
Visualizer.prototype.initMessages = function() { |
|
|
|
|
var container = document.createElement('div'); |
|
|
|
|
container.className = 'comment-container'; |
|
|
|
|
container.className = 'message-container'; |
|
|
|
|
|
|
|
|
|
var div1 = document.createElement('div'); |
|
|
|
|
div1.className = 'comment'; |
|
|
|
|
div1.className = 'message'; |
|
|
|
|
|
|
|
|
|
var div2 = document.createElement('div'); |
|
|
|
|
div2.className = 'comment'; |
|
|
|
|
div2.className = 'message'; |
|
|
|
|
|
|
|
|
|
var div3 = document.createElement('div'); |
|
|
|
|
div3.className = 'comment'; |
|
|
|
|
div3.className = 'message'; |
|
|
|
|
|
|
|
|
|
container.appendChild(div1); |
|
|
|
|
container.appendChild(div2); |
|
|
|
@ -65,6 +98,15 @@ Visualizer.prototype.initComments = function() { |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
Visualizer.prototype.initRange = function() { |
|
|
|
|
var rangeInput = function(label, event) { |
|
|
|
|
label.innerHTML = 'Number of items (n) = ' + event.target.value; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var rangeChange = function(event) { |
|
|
|
|
this.init.items(event.target.value); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var container = document.createElement('div'); |
|
|
|
|
container.className = 'range-container'; |
|
|
|
|
|
|
|
|
@ -77,14 +119,14 @@ Visualizer.prototype.initRange = function() { |
|
|
|
|
range.setAttribute('value', 40); |
|
|
|
|
range.setAttribute('min', 5); |
|
|
|
|
range.setAttribute('max', 80); |
|
|
|
|
range.addEventListener('change', this.rangeChange.bind(this)); |
|
|
|
|
range.addEventListener('input', this.rangeInput.bind(null, msg)); |
|
|
|
|
range.addEventListener('change', rangeChange.bind(this)); |
|
|
|
|
range.addEventListener('input', rangeInput.bind(null, msg)); |
|
|
|
|
|
|
|
|
|
container.appendChild(range); |
|
|
|
|
container.appendChild(msg); |
|
|
|
|
|
|
|
|
|
return container; |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
@ -110,13 +152,13 @@ Visualizer.prototype.initControls = function() { |
|
|
|
|
reset.title = 'Restart' |
|
|
|
|
reset.addEventListener('click', onclick); |
|
|
|
|
|
|
|
|
|
var msg1 = document.createElement('div'); |
|
|
|
|
msg1.className = 'msg'; |
|
|
|
|
msg1.innerHTML = 'Swaps: 999'; |
|
|
|
|
var stat1 = document.createElement('div'); |
|
|
|
|
stat1.className = 'stat'; |
|
|
|
|
stat1.innerHTML = 'Swaps: 999'; |
|
|
|
|
|
|
|
|
|
var msg2 = document.createElement('div'); |
|
|
|
|
msg2.className = 'msg'; |
|
|
|
|
msg2.innerHTML = 'Comparisons: 999'; |
|
|
|
|
var stat2 = document.createElement('div'); |
|
|
|
|
stat2.className = 'stat'; |
|
|
|
|
stat2.innerHTML = 'Comparisons: 999'; |
|
|
|
|
|
|
|
|
|
var container = document.createElement('div'); |
|
|
|
|
container.className = 'controls-container'; |
|
|
|
@ -125,8 +167,8 @@ Visualizer.prototype.initControls = function() { |
|
|
|
|
container.appendChild(back); |
|
|
|
|
container.appendChild(play); |
|
|
|
|
container.appendChild(forward); |
|
|
|
|
container.appendChild(msg1); |
|
|
|
|
container.appendChild(msg2); |
|
|
|
|
container.appendChild(stat1); |
|
|
|
|
container.appendChild(stat2); |
|
|
|
|
|
|
|
|
|
return container; |
|
|
|
|
}; |
|
|
|
|