liveBook · Manning

Hierarchy

As explained in D3.js in Action, Third Edition

Nested data is a hierarchical data structure in which objects or records contain other objects or records recursively.

Nested data organizes objects or records into parent/child relationships, usually by giving each object an array of child objects. Each object has one parent, making the structure suitable for hierarchical visualizations such as dendrograms. section 3.1.3

In D3, hierarchical data can come from a CSV or a hierarchical JSON file. Use d3.stratify() for CSV data and d3.hierarchy() for JSON; both produce a root structure with descendants() and leaves() methods. section 11.1.2

A practical D3 workflow is:

  • Load the hierarchical JSON object. section 11.1.2
  • Pass the object to hierarchy(data) and store the returned structure as root. section 11.1.2
  • Use root.descendants() for every node and root.leaves() for nodes without children. section 11.1.2

Language-family hierarchy

A JSON object can represent Languages as the root, Indo-European as a child, Germanic beneath it, and English as a leaf. Passing this object to d3.hierarchy() creates the nested structure used for a hierarchical visualization. section 11.1.2

{
  "name": "Languages",
  "children": [
    {
      "name": "Indo-European",
      "children": [
        {
          "name": "Germanic",
          "children": [
            {
              "name": "English"
            }
          ]
        },
        {
          "name": "Indo-Aryan",
          "children": [
            {
              "name": "Hindi"
            }
          ]
        },
        {
          "name": "Romance",
          "children": [
            {
               "name": "Spanish"
            },
            {
               "name": "French"
            }
          ]
        },
      ]
    },
    {
      "name": "Sino-Tibetan",
      "children": [
        {
          "name": "Sinitic",
          "children": [
            {
              "name": "Mandarin Chinese"
            }
          ]
        }
      ]
    }
  ]
}

Inspect the complete hierarchical JSON example and its parent/child nesting. section 11.1.2

How D3.js in Action, Third Edition covers it

How deep it goes
Explains parent/child nesting, CSV and JSON conversion, and descendant or leaf access, but stops before visualization layout.
Examples
Uses a language-family JSON hierarchy containing Languages, Indo-European, Germanic, English, and other leaves.
What you can do after
Builds a D3 hierarchy from JSON or CSV and retrieves its root, descendants, and leaves.
How it connects
Connects nested data with hierarchical data, JSON, CSV, D3 hierarchy, dendrograms, and parent/child relationships.

Also explained in

Compare how 7 books explain hierarchy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.