9951 explained code solutions for 126 technologies


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


Creating an organization chart using d3.js is a great way to visualize hierarchical data. To do this, you will need to create a data structure that contains the parent-child relationships between the elements of the chart. Once you have the data, you can use the d3.hierarchy() function to create a tree layout from the data. The tree layout can then be used to create the chart.

For example, the following code creates a simple organization chart from a data structure containing parent-child relationships:

// Create a data structure containing parent-child relationships
var data = {
    "name": "Root",
    "children": [
        { "name": "Child 1" },
        { "name": "Child 2" },
        {
            "name": "Child 3",
            "children": [
                { "name": "Grandchild 1" },
                { "name": "Grandchild 2" }
            ]
        }
    ]
};

// Create a tree layout from the data
var tree = d3.hierarchy(data);

// Create the chart
var svg = d3.select("body").append("svg")
    .attr("width", 500)
    .attr("height", 500);

// Draw the chart
var chart = svg.append("g")
    .attr("transform", "translate(50, 50)")
    .attr("class", "chart");

// Draw the nodes
chart.selectAll("circle")
    .data(tree.descendants())
    .enter()
    .append("circle")
    .attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; })
    .attr("r", 5);

// Draw the links
chart.selectAll("line")
    .data(tree.links())
    .enter()
    .append("line")
    .attr("x1", function(d) { return d.source.x; })
    .attr("y1", function(d) { return d.source.y; })
    .attr("x2", function(d) { return d.target.x; })
    .attr("y2", function(d) { return d.target.y; });

The output of the above code is an organization chart with nodes and links connecting them, as shown below:

Organization Chart

The code consists of the following parts:

  1. Data structure - This is the data structure containing the parent-child relationships of the elements in the chart.
  2. Tree layout - This is the tree layout created from the data structure using the d3.hierarchy() function.
  3. Chart - This is the SVG element that will contain the chart.
  4. Nodes - These are the nodes in the chart, created by selecting all the descendants of the tree layout and appending circles to the chart.
  5. Links - These are the links connecting the nodes, created by selecting all the links of the tree layout and appending lines to the chart.

For more information on creating an organization chart with d3.js, check out the following resources:

Edit this code on GitHub