9951 explained code solutions for 126 technologies


javascript-d3How do I create an organizational chart using d3.js?


Creating an organizational chart using d3.js is a relatively straightforward process. To begin, you'll need to have a basic understanding of HTML, CSS, and JavaScript.

To create an organizational chart, you'll need to first create a basic HTML page, and then add a <div> element to the page. This will be the container for your organizational chart.

Next, you'll need to include the d3.js library in your HTML page. This can be done by adding the following script tag to the page:

<script src="https://d3js.org/d3.v5.min.js"></script>

After that, you'll need to create a JavaScript function that will generate the organizational chart. This function will need to read the data that will be used to generate the chart, and then use the d3.js library to create the chart. Here is an example of a basic organizational chart function:

function generateChart(data) {
  var chart = d3.select("#chart")
    .append("svg")
    .attr("width", 500)
    .attr("height", 500);

  var nodes = chart.selectAll("g")
    .data(data)
    .enter()
    .append("g")
    .attr("transform", function(d, i) {
      return "translate(" + i * 100 + ", 0)";
    });

  nodes.append("rect")
    .attr("width", 50)
    .attr("height", 50)
    .attr("fill", "red");

  nodes.append("text")
    .text(function(d) {
      return d.name;
    })
    .attr("x", 25)
    .attr("y", 25);
}

The function above reads the data and creates a <svg> element, and then uses the .data(), .enter(), and .append() methods to create a <rect> and <text> element for each node in the data.

Once the chart has been generated, it can be displayed by calling the generateChart() function with the data as an argument.

Code explanation

  • d3.select("#chart"): selects the element with the id chart
  • .append("svg"): appends an <svg> element to the selected element
  • .attr("width", 500): sets the width of the <svg> element to 500
  • .data(data): sets the data to be used for the chart
  • .enter(): creates a new selection for each item in the data
  • .append("g"): appends a <g> (group) element to the selection
  • .attr("transform", function(d, i) { ... }): sets the transform attribute of the <g> element to position it properly
  • .append("rect"): appends a <rect> element to the selection
  • .attr("fill", "red"): sets the fill of the <rect> element to red
  • .append("text"): appends a <text> element to the selection
  • .text(function(d) { ... }): sets the text of the <text> element to the name of the node
  • .attr("x", 25): sets the x-position of the <text> element to 25
  • .attr("y", 25): sets the y-position of the <text> element to 25
  • generateChart(data): calls the generateChart() function with the data as an argument

Helpful links

Edit this code on GitHub