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

Copy Markdown View Source

The layered DAG (spec/08 §11): a directed graph laid out in ranks with routed edges, Sugiyama-style, the layout of the second cut's flowcharts, state, class and ER diagrams.

iex> alias Macaw.Draw.Layout.{Graph, Layered}
iex> {:ok, graph} = Graph.new(["a", "a"], ["b", "c"], nil, nil, 1)
iex> {:ok, layered} = Layered.new(routing: "polyline")
iex> {:ok, result} = Layered.generate(layered, graph, [{40, 20}, {40, 20}, {40, 20}], [])
iex> Enum.map(result.nodes, &{&1.id, &1.rank})
[{"a", 0}, {"b", 1}, {"c", 1}]

Its algorithms are Graphviz dot's and dagre's (Gansner, Koutsofios, North and Vo 1993; Brandes and Köpf 2001; Barth, Jünger and Mutzel 2002), every tie broken by input order so a drawing is a function of its input alone (spec/08 decision 9):

  • Normalizing (§11.3): self-loops set aside, parallel edges merged for ranking, cycles broken by a depth-first search in node and edge order (a reversed edge is drawn in its own direction and marked reversed), and with any edge label every minimum length doubled and rank_sep halved, the label on its edge's middle dummy.
  • Ranks (§11.4): :network_simplex (default) or :longest_path, per connected component, the components side by side in the order of their first node. Network simplex makes at most simplex_pivots pivots, past which the feasible ranking is kept and the result says bounded: true.
  • Order (§11.5): dummies on long edges, a breadth-first initial order, barycenter sweeps down and up with a transpose after each, crossings counted by an accumulator tree, the best order kept.
  • Coordinates (§11.6): Brandes–Köpf's four alignments, balanced by default; ranks rank_sep apart by their tallest node; the drawing starts at margin.
  • Routing (§11.7): :spline (default), :polyline or :orthogonal.

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

OptionTypeDefault
directionone of directions/0:tb
rank_sep, node_sep, edge_sepnon-negative numbers50, 20, 10
rankerone of rankers/0:network_simplex
crossing_passespositive integer24
alignone of aligns/0:balanced
rank_alignone of rank_aligns/0:center
routingone of routings/0:spline
marginnon-negative number0
simplex_pivotspositive integer or nilnil, read as max(1000, 10 · edges)

Summary

Functions

The Brandes–Köpf alignments: [:balanced, :ul, :ur, :dl, :dr].

The columns of a layered drawing (§11.8). :nodes: "row", "id", "x", "y" (the center), "x0", "y0", "x1", "y1" (the box), "rank", "order", in node order. :edges: "row", "source", "target", "path", "reversed", "label_x", "label_y" (nil without a label), and the arrowhead's place and bearing at the target end, "end_x", "end_y", "end_angle" (radians, 0 along +x, y down), in edge order.

The rank axes: [:tb, :bt, :lr, :rl].

The layered drawing of graph (§11.1): sizes one {width, height} per node in node order; opts the per-edge labels: (a {w, h} box or nil per edge) and min_lengths: (positive integers, 1 each by default).

new/1 with every option at its default.

The layout of opts (§11.2): {:ok, layered} or {:error, faults}.

Where a node sits across its rank's thickness: [:center, :start, :end].

The rank assignments: [:network_simplex, :longest_path].

The edge routings: [:orthogonal, :spline, :polyline].

Types

t()

@type t() :: %Macaw.Draw.Layout.Layered{
  align: :balanced | :ul | :ur | :dl | :dr,
  crossing_passes: pos_integer(),
  direction: :tb | :bt | :lr | :rl,
  edge_sep: number(),
  margin: number(),
  node_sep: number(),
  rank_align: :center | :start | :end,
  rank_sep: number(),
  ranker: :network_simplex | :longest_path,
  routing: :orthogonal | :spline | :polyline,
  simplex_pivots: pos_integer() | nil
}

Functions

aligns()

@spec aligns() :: [atom()]

The Brandes–Köpf alignments: [:balanced, :ul, :ur, :dl, :dr].

columns(result, output)

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

The columns of a layered drawing (§11.8). :nodes: "row", "id", "x", "y" (the center), "x0", "y0", "x1", "y1" (the box), "rank", "order", in node order. :edges: "row", "source", "target", "path", "reversed", "label_x", "label_y" (nil without a label), and the arrowhead's place and bearing at the target end, "end_x", "end_y", "end_angle" (radians, 0 along +x, y down), in edge order.

directions()

@spec directions() :: [atom()]

The rank axes: [:tb, :bt, :lr, :rl].

generate(layered, graph, sizes, opts)

The layered drawing of graph (§11.1): sizes one {width, height} per node in node order; opts the per-edge labels: (a {w, h} box or nil per edge) and min_lengths: (positive integers, 1 each by default).

Answers {:ok, %Layered.Result{}} or {:error, faults}: a size that is no box is {[:sizes, i], {:type, :size}}, a label that is no box {[:labels, i], {:type, :size}}, a minimum length below 1 {[:min_lengths, i], {:type, :positive_integer}}, a list of another length {[key], {:length, n}}. An empty graph is an empty result of size 2 · margin.

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 (§11.2): {:ok, layered} or {:error, faults}.

rank_aligns()

@spec rank_aligns() :: [atom()]

Where a node sits across its rank's thickness: [:center, :start, :end].

rankers()

@spec rankers() :: [atom()]

The rank assignments: [:network_simplex, :longest_path].

routings()

@spec routings() :: [atom()]

The edge routings: [:orthogonal, :spline, :polyline].