-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.js
109 lines (100 loc) · 2.49 KB
/
base.js
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
//get selected algorithm
function getAlgo() {
algo = document.getElementById("get-algo").value;
return algo;
}
// function for changing specific bar color filtering with data
// d => single data, color => hexa color code
function changeBarColor(d, color) {
var smi = heightScale(d);
svg.selectAll("rect").each(function (d, i) {
if (smi == d3.select(this).attr("height")) {
d3.select(this).style("fill", color);
}
});
}
// function for generating random data
function randomData(max, range) {
data = [];
n = 0;
while (n < max) {
d = Math.floor(Math.random() * range) + 1;
if (data.includes(d) != true) {
data.push(d);
n++;
}
}
return data;
}
//function for creating chart. Note: data is an array of integer value
function createChart(data) {
svg = d3.select("#chart").append("svg");
bandScale = d3.scaleBand().domain(data).range([0, areaWidth]).padding(0.1);
svg.attr("width", areaWidth).attr("height", areaHeight);
svg
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", function (d, i) {
return bandScale(d);
})
.attr("y", function (d) {
return areaHeight - heightScale(d);
})
.attr("width", function () {
return bandScale.bandwidth();
})
.attr("height", function (d) {
return heightScale(d);
})
.style("fill", "rgb(173, 216, 230)");
svg
.selectAll("text")
.data(data)
.enter()
.append("text")
.text(function (d) {
return d;
})
.attr("x", function (d, i) {
return bandScale(d) + 10;
})
.attr("y", function (d) {
return areaHeight - 15;
})
.style("width", bandScale.bandwidth())
.style("fill", "black")
.style("font-size", areaWidth / data.length / 3)
.style("font-family", "sans-serif")
.style("z-index", 1);
}
// bar visualization while sorting. Bar Swapping
function swapBar(data) {
bandScale.domain(data);
svg
.transition()
.duration(750)
.selectAll("rect")
.attr("x", function (d) {
return bandScale(d);
});
svg
.transition()
.duration(750)
.selectAll("text")
.attr("x", function (d) {
return bandScale(d) + 10;
});
}
function togglePlay() {
var sortElement = document.getElementById("sort");
var stopElement = document.getElementById("stop");
if (isFound) {
sortElement.classList.add("none");
stopElement.classList.add("none");
} else {
sortElement.classList.toggle("none");
stopElement.classList.toggle("none");
}
}