Macaw.Draw.Layout.Tree (macaw_draw v0.1.0)

Copy Markdown View Source

The tidy tree, for node-link diagrams (spec/08 §3.2).

iex> alias Macaw.Draw.Layout.{Hierarchy, Tree}
iex> {:ok, root, []} = Hierarchy.stratify(~w(r a b), [nil, "r", "r"], [])
iex> {:ok, tree} = Tree.new(size: {100, 50})
iex> tree |> Tree.generate(root) |> Hierarchy.descendants() |> Enum.map(&{&1.x, &1.y})
[{50.0, 0.0}, {0.0, 50.0}, {100.0, 50.0}]

generate/2 sets x and y on every node: y the depth, x by the Reingold–Tilford algorithm in the linear-time form of Buchheim, Jünger and Leipert, ported from d3-hierarchy's tree. Children of one parent are placed left to right with the gap separation gives, each parent centered over its first and last child, and no two nodes at one depth closer than separation allows.

Options (§3.1), checked by new/1:

OptionTypeDefault
size{w, h} or [w, h] of non-negative numbersnil
node_size{dx, dy} or [dx, dy] of positive numbersnil
separationfunction of two nodes answering a non-negative numbersiblings 1, otherwise 2

size and node_size are exclusive. Without either, x is in separation units with the root at 0 and y in depth levels; size normalizes both into the box, node_size multiplies them. This is visualize's Visualize.Layout.Tree, its setters replaced by new/1 (spec/08 §17).

Summary

Functions

The columns of a laid-out tree (§3.5): :nodes, one row per node in pre-order ("row", "id", "depth", "height", "value", "x", "y"), or :links, one row per parent-child pair ("row" the child's, "source", "target", "x0", "y0", "x1", "y1", "path") with options shape: and orientation: (Macaw.Draw.Layout.Links).

Every node of root positioned by the tidy tree, then scaled (§3.1, §3.2).

new/1 with every option at its default.

The layout of opts (§3.1): {:ok, tree}, or {:error, faults} with every bad option at its path, {[:node_size], {:exclusive, :size}} when both sizes are given.

Types

t()

@type t() :: %Macaw.Draw.Layout.Tree{
  node_size: {number(), number()} | nil,
  separation: (Macaw.Draw.Layout.Hierarchy.t(),
               Macaw.Draw.Layout.Hierarchy.t() ->
                 number()),
  size: {number(), number()} | nil
}

Functions

columns(root, output, opts)

@spec columns(Macaw.Draw.Layout.Hierarchy.t(), atom() | String.t(), keyword()) ::
  %{required(String.t()) => [term()]} | Macaw.Draw.Layout.error()

The columns of a laid-out tree (§3.5): :nodes, one row per node in pre-order ("row", "id", "depth", "height", "value", "x", "y"), or :links, one row per parent-child pair ("row" the child's, "source", "target", "x0", "y0", "x1", "y1", "path") with options shape: and orientation: (Macaw.Draw.Layout.Links).

generate(tree, root)

Every node of root positioned by the tidy tree, then scaled (§3.1, §3.2).

new()

@spec new() :: {:ok, t()}

new/1 with every option at its default.

new(opts)

@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()

The layout of opts (§3.1): {:ok, tree}, or {:error, faults} with every bad option at its path, {[:node_size], {:exclusive, :size}} when both sizes are given.