Time Complexity Graphed with D3.js

Developer
Size
3,030 Kb
Views
8,096

How do I make an time complexity graphed with d3.js?

What is a time complexity graphed with d3.js? How do you make a time complexity graphed with d3.js? This script and codes were developed by Laura Kelly on 29 January 2023, Sunday.

Time Complexity Graphed with D3.js Previews

Time Complexity Graphed with D3.js - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Time Complexity Graphed with D3.js</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="graph"> <h1>Time Complexity</h1> <div id="plot"> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://d3js.org/d3.v3.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Time Complexity Graphed with D3.js - Script Codes CSS Codes

body { font: 10px sans-serif;
}
h1 { font-size: 40px; text-align: center; margin-bottom: 40px;
}
#graph { width: 960px; margin: 0 auto; display: block;
}
#legend { font-size: 15px;
}
ul { list-style: none;
}
hr { padding: 0; margin-right: 5px; margin-bottom: 3px; width: 30px; display: inline-block; border: solid, 2px;
}
.O1 { stroke: #AEFF00; fill: #AEFF00;
}
.On { stroke: #E8AE3A; fill: #E8AE3A;
}
.Ologn { stroke: #8C7C6E; fill: #8C7C6E;
}
.Onlogn { stroke: #8BD3FF; fill: #8BD3FF;
}
.On2 { stroke: #3D60E8; fill: #3D60E8;
}
.factorial { stroke: #8352FF; fill: #8352FF;
}
.O2n { stroke: #FF6764; fill: #FF6764;
}
.axis path,
.axis line { fill: none; stroke: #000; shape-rendering: crispEdges;
}
.x.axis path { display: none;
}
.line { fill: none; stroke-width: 2px;
}

Time Complexity Graphed with D3.js - Script Codes JS Codes

(function () { function constant () { var nums = []; for (var i=0; i < 100; i++){ nums.push(1); } return nums; } function linear () { var nums = []; for (var i=0; i < 100; i++) { nums.push(i); } return nums; } function logn () { var nums = []; for (var i=0; i < 100; i++) { if (i === 0){ nums.push(0); } else { nums.push(Math.log(i)); } } return nums; } function nlogn() { var nums = []; for (var i=0; i < 100; i++) { if (i === 0){ nums.push(0); } else { nums.push(Math.log(i) * i); } } return nums; } function nexp2 () { var nums = []; for (var i=0; i < 100; i++) { nums.push(Math.pow(i, 2)); } return nums; } function twoexpn () { var nums = []; for (var i=0; i < 20; i++) { nums.push(Math.pow(2,i)); } return nums; } function factorial () { var nums = [], rval = 1; for (var i=1; i < 8; i++) { rval = rval * i; nums.push(rval) } nums.unshift(0) return nums; } function graph () { var margin = {top: 20, right: 80, bottom: 30, left: 50}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]) .domain([0, 100]); var y = d3.scale.linear() .range([height, 0]) .domain([0, 1000]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .interpolate("basis") .x(function(d,i) { return x(i); }) .y(function(d) { return y(d); }); var svg = d3.select("#plot").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var data = {}; data["O(1)"] = constant(); data["O(n)"] = linear(); data["O(logn)"] = logn(); data["O(nlogn)"] = nlogn(); data["O(n^2)"] = nexp2(); data["O(2^n)"] = twoexpn(); data["O(n!)"] = factorial(); var keys = Object.keys(data) var legend = svg.selectAll(".legend") .data(keys) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .attr("class", function (d) { if (d === 'O(n!)') d = "factorial"; return d.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"")} ); legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function (d) { return d; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("Number of Elements"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Number of Operations"); for (var i=0; i < keys.length; i++) { svg.append("path") .datum(data[keys[i]]) .attr("class", function () { var key = keys[i]; if (key === 'O(n!)') key = "factorial"; key = key.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,""); return "line " + key }) .attr("d", line); } }	$(document).ready(function (){ graph(); });
})();
Time Complexity Graphed with D3.js - Script Codes
Time Complexity Graphed with D3.js - Script Codes
Home Page Home
Developer Laura Kelly
Username laurakelly
Uploaded January 29, 2023
Rating 3
Size 3,030 Kb
Views 8,096
Do you need developer help for Time Complexity Graphed with D3.js?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Laura Kelly (laurakelly) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!