Visualize.Layout.Hierarchy (Visualize v0.2.25)

Copy Markdown View Source

Hierarchical data structure for tree-based visualizations.

Provides functions to create and manipulate tree data structures that can be used with various layout algorithms (tree, treemap, pack, etc.).

Creating a Hierarchy

From nested data:

data = %{
  name: "root",
  children: [
    %{name: "child1", value: 10},
    %{name: "child2", children: [
      %{name: "grandchild", value: 5}
    ]}
  ]
}

root = Visualize.Layout.Hierarchy.new(data)

From tabular data with stratify:

data = [
  %{id: "root", parent: nil},
  %{id: "child1", parent: "root"},
  %{id: "child2", parent: "root"},
  %{id: "grandchild", parent: "child2"}
]

root = Visualize.Layout.Hierarchy.stratify(data)

Working with Nodes

# Traverse all nodes
Visualize.Layout.Hierarchy.each(root, fn node -> IO.inspect(node.data) end)

# Sum values up the tree
root = Visualize.Layout.Hierarchy.sum(root, fn d -> d[:value] || 0 end)

# Count leaves
root = Visualize.Layout.Hierarchy.count(root)

# Get all descendants
nodes = Visualize.Layout.Hierarchy.descendants(root)

# Get all leaves
leaves = Visualize.Layout.Hierarchy.leaves(root)

Summary

Functions

Returns all ancestor nodes (from parent to root).

Copies the hierarchy, optionally transforming data.

Sets each node's value to the count of its leaves.

Returns all descendant nodes as a flat list (including the node itself).

Traverses the hierarchy, calling the function on each node.

Traverses post-order (children before parent) and calls function.

Returns all leaf nodes (nodes without children).

Returns all links (parent-child pairs) in the hierarchy.

Creates a hierarchy from nested data.

Returns the shortest path from source to target: source, its ancestors up to the lowest common ancestor, then the ancestors of target down to target.

Sorts children at each level using the comparator.

Creates a hierarchy from flat/tabular data.

Computes the sum of values for each node.

Types

t()

@type t() :: %Visualize.Layout.Hierarchy{
  children: [t()] | nil,
  data: any(),
  depth: non_neg_integer(),
  height: non_neg_integer(),
  id: any(),
  parent: t() | nil,
  r: number() | nil,
  value: number() | nil,
  x: number() | nil,
  x0: number() | nil,
  x1: number() | nil,
  y: number() | nil,
  y0: number() | nil,
  y1: number() | nil
}

Functions

ancestors(hierarchy)

@spec ancestors(t()) :: [t()]

Returns all ancestor nodes (from parent to root).

copy(node, transform \\ nil)

@spec copy(t(), (any() -> any()) | nil) :: t()

Copies the hierarchy, optionally transforming data.

count(node)

@spec count(t()) :: t()

Sets each node's value to the count of its leaves.

descendants(node)

@spec descendants(t()) :: [t()]

Returns all descendant nodes as a flat list (including the node itself).

each(node, func)

@spec each(t(), (t() -> any())) :: :ok

Traverses the hierarchy, calling the function on each node.

Visits nodes in pre-order (parent before children).

each_after(node, func)

@spec each_after(t(), (t() -> any())) :: :ok

Traverses post-order (children before parent) and calls function.

leaves(node)

@spec leaves(t()) :: [t()]

Returns all leaf nodes (nodes without children).

links(node)

@spec links(t()) :: [{t(), t()}]

Returns all links (parent-child pairs) in the hierarchy.

Returns list of {source, target} tuples.

new(data, opts \\ [])

@spec new(map(), keyword()) :: t()

Creates a hierarchy from nested data.

Options

  • :children - Function to get children from a node (default: & &1[:children] or & &1.children)

path(source, target)

@spec path(t(), t()) :: [t()]

Returns the shortest path from source to target: source, its ancestors up to the lowest common ancestor, then the ancestors of target down to target.

Nodes are matched by their data lineage from the root, so a node compares equal to the snapshot of itself held in a descendant's parent field.

sort(node, comparator)

@spec sort(t(), (t(), t() -> boolean())) :: t()

Sorts children at each level using the comparator.

The comparator receives two nodes and returns true if the first should come before the second.

stratify(data, opts \\ [])

@spec stratify([map()], keyword()) :: t()

Creates a hierarchy from flat/tabular data.

Options

  • :id - Function to get node ID (default: & &1[:id] or & &1.id)
  • :parent_id - Function to get parent ID (default: & &1[:parent] or & &1.parent)

sum(node, value_fn)

@spec sum(t(), (any() -> number())) :: t()

Computes the sum of values for each node.

The value of each node becomes the sum of its own value plus all descendant values. Evaluates the value function for leaves.