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

Copy Markdown View Source

Flow diagrams: the nodes and links of a directed weighted graph positioned so a link's width is its value (spec/08 §8), a port of d3-sankey.

iex> alias Macaw.Draw.Layout.{Graph, Sankey}
iex> {:ok, graph} = Graph.new(["a", "a"], ["b", "c"], [3, 1], nil, 0)
iex> {:ok, sankey} = Sankey.new(size: {100, 40}, node_padding: 0)
iex> laid = Sankey.compute(sankey, graph)
iex> Enum.map(laid.nodes, &{&1.id, &1.layer, &1.y1 - &1.y0})
[{"a", 0, 40.0}, {"b", 1, 30.0}, {"c", 1, 10.0}]

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

OptionTypeDefault
size{w, h}{400, 300}
node_widthnon-negative number24
node_paddingnon-negative number8
iterationsnon-negative integer6
node_alignone of alignments/0, an atom or its spelling:justify
link_sortfunction of two links answering a boolean, or nilnil

compute/2 gives every node its value (the greater of its outgoing and incoming totals), its depth and height (longest paths from a source and to a sink), its column by the alignment, and its rectangle; one scale sizes nodes and links alike, and iterations relaxation passes move nodes toward straight ribbons with collisions resolved. A cycle never settles: the walk stops after as many rounds as there are nodes, and cyclic?/1 says it did. This is visualize's Visualize.Layout.Sankey over a Graph, with Visualize.Chart.Transform.Layout sankey/3 and sankey_link/1, answering IR paths.

Summary

Types

A node alignment.

A laid-out link, its node snapshots carrying their positions.

A laid-out node: source_links and target_links are the positions of its links in links, in the settled order.

t()

Functions

The node alignments: [:left, :right, :center, :justify].

The columns of a computed layout (§8.3). :nodes: one row per node in node order, "row", "id", "index", "depth", "height", "layer", "value", "x0", "x1", "y0", "y1". :links: one row per link in input order, "row", "source", "target", "value", "width", "x0" (the source's right edge), "y0", "x1" (the target's left edge), "y1", "path".

The flow diagram of graph (§8.2): the layout with nodes in node order and links in input order. link_sort, when given, is called with two links as %{index, source, target, value}, the ids of their ends.

Whether the depth walk of a computed layout stopped on a cycle (§8.2).

The ribbon of a laid-out link (§8.3): a move to {source.x1, y0}, a cubic to {target.x0, y1} with both control points at the midpoint x, a line down by width, a cubic back, closed.

new/1 with every option at its default.

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

The nodes of a computed layout grouped by ascending layer (§8.3).

Types

alignment()

@type alignment() :: :left | :right | :center | :justify

A node alignment.

sankey_link()

@type sankey_link() :: %{
  index: non_neg_integer(),
  row: non_neg_integer(),
  source: term(),
  target: term(),
  value: number(),
  source_index: non_neg_integer(),
  target_index: non_neg_integer(),
  source_node: map(),
  target_node: map(),
  y0: number(),
  y1: number(),
  width: number()
}

A laid-out link, its node snapshots carrying their positions.

sankey_node()

@type sankey_node() :: %{
  index: non_neg_integer(),
  id: term(),
  row: non_neg_integer() | nil,
  depth: non_neg_integer(),
  height: non_neg_integer(),
  layer: non_neg_integer(),
  value: number(),
  source_links: [non_neg_integer()],
  target_links: [non_neg_integer()],
  x0: number(),
  x1: number(),
  y0: number(),
  y1: number()
}

A laid-out node: source_links and target_links are the positions of its links in links, in the settled order.

t()

@type t() :: %Macaw.Draw.Layout.Sankey{
  cyclic: boolean(),
  iterations: non_neg_integer(),
  link_sort: (map(), map() -> boolean()) | nil,
  links: [sankey_link()],
  node_align: alignment(),
  node_padding: number(),
  node_width: number(),
  nodes: [sankey_node()],
  size: {number(), number()}
}

Functions

alignments()

@spec alignments() :: [alignment()]

The node alignments: [:left, :right, :center, :justify].

columns(sankey, output)

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

The columns of a computed layout (§8.3). :nodes: one row per node in node order, "row", "id", "index", "depth", "height", "layer", "value", "x0", "x1", "y0", "y1". :links: one row per link in input order, "row", "source", "target", "value", "width", "x0" (the source's right edge), "y0", "x1" (the target's left edge), "y1", "path".

compute(sankey, graph)

@spec compute(t(), Macaw.Draw.Layout.Graph.t()) :: t() | Macaw.Draw.Layout.error()

The flow diagram of graph (§8.2): the layout with nodes in node order and links in input order. link_sort, when given, is called with two links as %{index, source, target, value}, the ids of their ends.

cyclic?(sankey)

@spec cyclic?(t()) :: boolean() | Macaw.Draw.Layout.error()

Whether the depth walk of a computed layout stopped on a cycle (§8.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 (§8.1): {:ok, sankey} or {:error, faults}.

nodes_by_layer(sankey)

@spec nodes_by_layer(t()) :: [[sankey_node()]] | Macaw.Draw.Layout.error()

The nodes of a computed layout grouped by ascending layer (§8.3).