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
@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
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.
Visits nodes in pre-order (parent before children).
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.
Returns list of {source, target} tuples.
Creates a hierarchy from nested data.
Options
:children- Function to get children from a node (default:& &1[:children]or& &1.children)
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.
Sorts children at each level using the comparator.
The comparator receives two nodes and returns true if the first should come before the second.
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)
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.