|
| 1 | +// class for sorting |
| 2 | +class sortData { |
| 3 | + constructor(data) { |
| 4 | + this.data = data; |
| 5 | + } |
| 6 | + selectionSort() {} |
| 7 | + bubbleSort() {} |
| 8 | + stopSort() { |
| 9 | + return false; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +//get selected algorithm |
| 14 | +function getAlgo() { |
| 15 | + var algo = document.getElementsByName("algo-name"); |
| 16 | + for (var i = 0; i < algo.length; i++) { |
| 17 | + if (algo[i].checked) { |
| 18 | + return algo[i].value; |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// function for changing specific bar color filtering with data |
| 24 | +// d => single data, color => hexa color code |
| 25 | +function changeBarColor(d, color) { |
| 26 | + var smi = heightScale(d); |
| 27 | + svg.selectAll("rect").each(function (d, i) { |
| 28 | + if (smi == d3.select(this).attr("height")) { |
| 29 | + d3.select(this).style("fill", color); |
| 30 | + } |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +// function for generating random data |
| 35 | +function randomData(max, range) { |
| 36 | + data = []; |
| 37 | + n = 0; |
| 38 | + while (n < max) { |
| 39 | + d = Math.floor(Math.random() * range) + 1; |
| 40 | + if (data.includes(d) != true) { |
| 41 | + data.push(d); |
| 42 | + n++; |
| 43 | + } |
| 44 | + } |
| 45 | + return data; |
| 46 | +} |
| 47 | + |
| 48 | +//function for creating chart. Note: data is an array of integer value |
| 49 | +function createChart(data) { |
| 50 | + svg = d3.select("#chart").append("svg"); |
| 51 | + |
| 52 | + bandScale = d3.scaleBand().domain(data).range([0, areaWidth]).padding(0.1); |
| 53 | + |
| 54 | + svg.attr("width", areaWidth).attr("height", areaHeight); |
| 55 | + |
| 56 | + svg |
| 57 | + .selectAll("rect") |
| 58 | + .data(data) |
| 59 | + .enter() |
| 60 | + .append("rect") |
| 61 | + .attr("x", function (d, i) { |
| 62 | + return bandScale(d); |
| 63 | + }) |
| 64 | + .attr("y", function (d) { |
| 65 | + return areaHeight - heightScale(d); |
| 66 | + }) |
| 67 | + .attr("width", function () { |
| 68 | + return bandScale.bandwidth(); |
| 69 | + }) |
| 70 | + .attr("height", function (d) { |
| 71 | + return heightScale(d); |
| 72 | + }) |
| 73 | + .style("fill", "rgb(173, 216, 230)"); |
| 74 | + |
| 75 | + svg |
| 76 | + .selectAll("text") |
| 77 | + .data(data) |
| 78 | + .enter() |
| 79 | + .append("text") |
| 80 | + .text(function (d) { |
| 81 | + return d; |
| 82 | + }) |
| 83 | + .attr("x", function (d, i) { |
| 84 | + return bandScale(d) + 10; |
| 85 | + }) |
| 86 | + .attr("y", function (d) { |
| 87 | + return areaHeight - 15; |
| 88 | + }) |
| 89 | + .style("width", bandScale.bandwidth()) |
| 90 | + .style("fill", "black") |
| 91 | + .style("font-size", areaWidth / data.length / 3) |
| 92 | + .style("font-family", "sans-serif") |
| 93 | + .style("z-index", 1); |
| 94 | +} |
| 95 | + |
| 96 | +// bar visualization while sorting. Bar Swapping |
| 97 | +function swapBar(data) { |
| 98 | + bandScale.domain(data); |
| 99 | + svg |
| 100 | + .transition() |
| 101 | + .duration(750) |
| 102 | + .selectAll("rect") |
| 103 | + .attr("x", function (d) { |
| 104 | + return bandScale(d); |
| 105 | + }); |
| 106 | + svg |
| 107 | + .transition() |
| 108 | + .duration(750) |
| 109 | + .selectAll("text") |
| 110 | + .attr("x", function (d) { |
| 111 | + return bandScale(d) + 10; |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +function togglePlay() { |
| 116 | + var sortElement = document.getElementById("sort"); |
| 117 | + var stopElement = document.getElementById("stop"); |
| 118 | + if (isSorted) { |
| 119 | + sortElement.classList.add("none"); |
| 120 | + stopElement.classList.add("none"); |
| 121 | + } else { |
| 122 | + sortElement.classList.toggle("none"); |
| 123 | + stopElement.classList.toggle("none"); |
| 124 | + } |
| 125 | +} |
0 commit comments