9951 explained code solutions for 126 technologies


javascript-d3How can I find a job using d3.js?


Using D3.js to find a job is a great way to stand out from the crowd. The library is becoming increasingly popular, and employers are looking for people with experience in it. Here is an example of how to use D3.js to create a simple bar chart:

<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
.bar {
  fill: steelblue;
}
</style>
</head>
<body>
<svg width="400" height="200"></svg>
<script>
var data = [12, 5, 8, 130, 44];
var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom;

var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
    y = d3.scaleLinear().rangeRound([height, 0]);

var g = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

x.domain(data.map(function(d) { return d; }));
y.domain([0, d3.max(data, function(d) { return d; })]);

g.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

g.append("g")
    .attr("class", "axis axis--y")
    .call(d3.axisLeft(y).ticks(10))
  .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", "0.71em")
    .attr("text-anchor", "end")
    .text("Frequency");

g.selectAll(".bar")
  .data(data)
  .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(d); })
    .attr("y", function(d) { return y(d); })
    .attr("width", x.bandwidth())
    .attr("height", function(d) { return height - y(d); });
</script>
</body>
</html>

Code explanation

  • <script src="https://d3js.org/d3.v5.min.js"></script> - This is the line that imports the D3.js library.
  • var data = [12, 5, 8, 130, 44]; - This is the data that will be used to create the bar chart.
  • var svg = d3.select("svg"), - This line creates a new SVG element.
  • x.domain(data.map(function(d) { return d; })); - This line sets the domain of the x-axis to the data.
  • y.domain([0, d3.max(data, function(d) { return d; })]); - This line sets the domain of the y-axis to the maximum value of the data.
  • g.append("g") - This line appends an SVG group element to the SVG element.
  • g.selectAll(".bar") - This line selects all elements with the class "bar".
  • .attr("x", function(d) { return x(d); }) - This line sets the x-coordinate of each bar to the value of the data.
  • .attr("y", function(d) { return y(d); }) - This line sets the y-coordinate of each bar to the value of the data.
  • .attr("width", x.bandwidth()) - This line sets the width of each bar to the value of the x-axis domain.
  • .attr("height", function(d) { return height - y(d); }); - This line sets the height of each bar to the difference between the height of the SVG element and the value of the data.

By using D3.js, you can create visuals that will help you stand out from other applicants. You can find more examples and tutorials online, such as this one from the official D3.js website: https://observablehq.com/@d3/bar-chart.

Edit this code on GitHub