forked from sruthysrajan/Aquanims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPositionSort.html
406 lines (337 loc) · 12.5 KB
/
PositionSort.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bar Chart</title>
<script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
</head>
<body>
<input type ="button" onclick = "positionSort()" value = "Position Sort"/>
<div id="vis"></div>
<p id="demo"></p>
<script>
var myData = [10,3,35,23,9,40,15,50];
//var myData = prompt("Enter the initial data: ").split(",");
var max = d3.max(myData);
/*Specify height, width, margins*/
var width = 600;
var height = 450;
var margin = {
top: 20,
left: 20,
right: 30,
bottom: 20
};
/*Create an svg canvass*/
var svg = d3.select("#vis")
.append("svg")
.attr('width', width)
.attr('height', height)
.append("g")
.attr("transform", "translate(" + margin.top + "," + margin.left + ")");
width = width - margin.left - margin.right;
height = height - margin.top - margin.bottom;
/*Specify scales to be used*/
var yscaleinitial = d3.scaleLinear()
.range([height, 0])
.domain([0, max]);
var yscalefinal = d3.scaleLinear()
.range([height/2-10, 0])
.domain([0, max]);
var yscale2initial = d3.scaleLinear()
.range([height, height])
.domain([0, 0]);
var yscale2final = d3.scaleLinear()
.range([height, height/2+10])
.domain([0, max]);
var yscale3final = d3.scaleLinear()
.range([0, 0])
.domain([0, 0]);
var xscale = d3.scaleBand()
.range([0, width])
.padding(0.1);
xscale.domain(d3.range(myData.length));
/*Set default duration*/
var duration = 1000;
/*Create axes*/
var xaxis = d3.axisBottom(xscale);
var yaxis = d3.axisLeft(yscaleinitial);
svg.append('g')
.attr('transform', 'translate(0, ' + (height) + ')')
.attr('class', 'x axis');
svg.append('g')
.attr('class', 'y axis');
/*Initial bars and outlines*/
var bars = svg.selectAll(".bar")
.data(myData);
bars
.enter()
.append('rect')
.attr('class', 'bar')
.attr('id',function(d,i){return "" +i;})
.attr("fill", "steelblue")
.merge(bars)
.attr("height", function(d, i) {
return height - yscaleinitial(d);
})
.attr("y", function(d, i) {
return yscaleinitial(d);
})
.attr("width", xscale.bandwidth())
.attr("x", function(d, i) {
return xscale(i);
})
var barsOutline = svg.selectAll(".barOutline")
.data(myData);
barsOutline
.enter()
.append('rect')
.attr('class', 'barOutline')
.attr('id',function(d,i){return "" +i;})
.attr("fill", "none")
.style("stroke-width", "0.5")
.style("stroke", "steelblue")
.merge(barsOutline)
.attr("height", function(d, i) {
return height - yscaleinitial(d);
})
.attr("y", function(d, i) {
return yscaleinitial(d);
})
.attr("width", xscale.bandwidth())
.attr("x", function(d, i) {
return xscale(i);
})
/*Selections for bars and outlines*/
var rectangle = svg.selectAll("rect[class='bar']");
var rectangleOutline = svg.selectAll("rect[class='barOutline']");
//console.log(rectangle);
/*Calling axes*/
svg.select('.x.axis')
.call(xaxis);
svg.select('.y.axis')
.call(yaxis);
/*Function to perform position sort*/
function positionSort()
{
var flag = 0;
/*Specifying new positions and creating arrays*/
var newPos = prompt("Enter the new order: ").split(",");
// var newPos = [2,1,5,6,3,7,4,0];
/*Converting from string to integer*/
for(i=0;i<myData.length;i++)
{
newPos[i] = +newPos[i];
}
/*Setting a flag if order doesn't change */
for(i=0;i<myData.length;i++)
{
if(i !== newPos[i])
{
flag = 1;
}
}
// console.log(flag)
/*New tick label order for x axis*/
var formatAxis = function(d, i) {
return newPos[i];
}
xaxis = d3.axisBottom(xscale).tickFormat(formatAxis);
/*If position doesn't change, execute*/
if(flag === 1)
{
/*Scale existing y axis to upper half*/
var yaxisUp = d3.axisLeft(yscalefinal).ticks(5);
svg.select('.y.axis')
.transition()
.duration(duration)
.call(yaxisUp);
/*Specify attributes for y axis on the lower half*/
var yaxisDownInitial = d3.axisLeft(yscale2initial).ticks(5);
var yaxisDownFinal = d3.axisLeft(yscale2final).ticks(5);
svg.append('g')
.attr('class', 'y axis new');
svg.select('.y.axis.new')
.call(yaxisDownInitial)
.transition()
.duration(duration)
.call(yaxisDownFinal);
/*Translate x axis to upper half*/
svg.selectAll(".x.axis")
.transition()
.duration(duration)
.attr("transform", "translate(0," + (height/2-10) +")");
/*Create new axis for bottom half*/
svg.append('g')
.attr('transform', 'translate(0, ' + (height) + ')')
.attr('class', 'x axis new');
svg.select('.x.axis.new')
.call(xaxis);
/*Scale existing bar chart to upper half*/
svg.selectAll("rect")
.transition()
.duration(duration)
.attr("height", function(d, i){
return (height/2 - 10) - yscalefinal(d);
}
)
.attr("y", function(d, i){
return yscalefinal(d);
}
)
.on("end", function(){
/*Selections for individual rectangles of initial bar chart*/
var rectAtIndex = [];
for(i=0;i<myData.length;i++)
{
rectAtIndex[i] = svg.select("rect[id = '" + i + "']").node();
}
/*Array of heights of initial bar charts*/
var heightAtIndex = [];
for(i=0;i<myData.length;i++)
{
heightAtIndex[i] = +rectAtIndex[i].getAttribute("height");
}
// console.log(rectAtIndex);
// console.log(heightAtIndex);
/*Array of reordered values*/
var newBars = [];
for(i=0;i<myData.length;i++)
{
newBars[i] = myData[newPos[i]];
}
// console.log(myData);
// console.log(newPos);
// console.log(newBars);
/*Variable for linear ease*/
var t = d3.easeLinear;
var delays = [];
/*Setting delays for transitions*/
for(i=0;i<myData.length;i++)
{
if(i==0)
delays[newPos[i]] = 2000;
else
delays[newPos[i]] = delays[newPos[i-1]] + heightAtIndex[newPos[i-1]]*20;
}
/*Create new bar chart with reordered values*/
var bars1 = svg.selectAll(".newbar")
.data(newBars);
/*Fill in transition for new chart*/
bars1
.enter()
.append('rect')
.attr('class', 'newbar')
.attr("fill", "steelblue")
.attr("height", 0)
.attr("y", height)
.attr("width", xscale.bandwidth())
.attr("x", function(d,i) {
return xscale(i);
})
.merge(bars1)
.transition()
.ease(t)
.delay(function(d,i){
return delays[newPos[i]];
})
.duration(function(d,i){
return heightAtIndex[newPos[i]]*20;
})
.attr("height", function(d, i) {
return height - yscale2final(d);
})
.attr("y", function(d, i) {
return yscale2final(d);
});
/*Create outlines for new bars*/
var bars1Outline = svg.selectAll(".newbarOutline")
.data(newBars);
bars1Outline
.enter()
.append('rect')
.attr('class', 'newbarOutline')
.attr("fill", "none")
.style("stroke-width", "0.5")
.style("stroke", "steelblue")
.style("stroke-opacity", 0)
.attr("width", xscale.bandwidth())
.attr("height", function(d, i) {
return height - yscale2final(d);
})
.attr("y", function(d, i) {
return yscale2final(d);
})
.attr("x", function(d,i) {
return xscale(i);
})
.merge(bars1Outline)
.transition()
.delay(1500)
.duration(duration)
.style("stroke-opacity", 1);
/*Fill out transition for initial bars*/
rectangle
.transition()
.ease(t)
.delay(function(d,i){
return delays[i];
})
.duration(function(d,i){
return heightAtIndex[i]*20;
})
.attr("height", 0)
.attr("y", height/2-10)
.transition()
.remove();
/*Set outlines to disappear*/
rectangleOutline
.transition()
.delay(function(d,i){
return delays[i]+heightAtIndex[i]*20;
})
.duration(500)
.style("stroke-opacity", 0)
.transition()
.remove();
/*Translate x axis of the upper half and remove it*/
svg.select(".x.axis")
.transition()
.delay(delays[newPos[myData.length-1]] + heightAtIndex[newPos[myData.length-1]]*20 + 200)
.duration(duration)
.attr("transform", "translate(0,0)")
.remove();
/*Translate y axis of the upper half and remove it*/
var yaxisEnd = d3.axisLeft(yscale3final);
svg.select('.y.axis')
.transition()
.delay(delays[newPos[myData.length-1]] + heightAtIndex[newPos[myData.length-1]]*20 + 200)
.duration(duration)
.call(yaxisEnd)
.remove();
/*Scale y axis of lower half to full height*/
yaxisDownFinal = d3.axisLeft(yscaleinitial);
svg.select('.y.axis.new')
.transition()
.delay(delays[newPos[myData.length-1]] + heightAtIndex[newPos[myData.length-1]]*20 +200)
.duration(duration)
.call(yaxisDownFinal);
/*Scale new bars to full height*/
bars1
.transition()
.delay(delays[newPos[myData.length-1]] + heightAtIndex[newPos[myData.length-1]]*20 + 200)
.duration(duration)
.attr("height", function(d, i){
return height- yscaleinitial(d);
}
)
.attr("y", function(d, i){
return yscaleinitial(d);
}
);
});
}
}
</script>
</body>
</html>