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

Copy Markdown View Source

Treemaps: nested rectangles whose areas are proportional to value (spec/08 §6).

iex> alias Macaw.Draw.Layout.{Hierarchy, Treemap}
iex> {:ok, root, []} = Hierarchy.stratify(~w(r a b), [nil, "r", "r"], [])
iex> {:ok, treemap} = Treemap.new(size: {10, 4}, tile: "dice")
iex> laid = Treemap.generate(treemap, Hierarchy.count(root))
iex> Enum.map(laid.children, &{&1.x0, &1.y0, &1.x1, &1.y1})
[{0.0, 0.0, 5.0, 4.0}, {5.0, 0.0, 10.0, 4.0}]

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

OptionTypeDefault
size{w, h}{1, 1}
tileone of tiles/0, an atom or its spelling:squarify
paddingnon-negative number0
padding_top, padding_right, padding_bottom, padding_leftnon-negative number or nilnil
padding_innernon-negative number or nilnil (0)
roundbooleanfalse

generate/2 gives the root {0, 0, w, h}; every node with children has its rectangle shrunk by the four paddings (a side they invert collapsing to its midpoint, as d3's positionNode does) and its children tiled into it, then laid out in turn. A child's weight is its value when that is a number above 0, else 0; a child of weight 0 gets a zero-area rectangle where it would sit. With no weight or no area, every child sits at the inner rectangle's top-left corner. Each tile is inset by padding_inner / 2; a rectangle that inverts collapses to its midpoint. This is visualize's Visualize.Layout.Treemap, its tiling read by spelling.

Summary

Types

t()

A tiling.

Functions

The columns of a treemap (§3.5): :nodes with "x0", "y0", "x1", "y1"; a treemap has no :links output, and asking for one is a fault.

Every node of root given its rectangle, x0, y0, x1, y1 (§6.2).

new/1 with every option at its default.

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

The tilings: [:squarify, :binary, :slice, :dice, :slice_dice].

Types

t()

@type t() :: %Macaw.Draw.Layout.Treemap{
  padding: number(),
  padding_bottom: number() | nil,
  padding_inner: number() | nil,
  padding_left: number() | nil,
  padding_right: number() | nil,
  padding_top: number() | nil,
  round: boolean(),
  size: {number(), number()},
  tile: tile()
}

tile()

@type tile() :: :squarify | :binary | :slice | :dice | :slice_dice

A tiling.

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 treemap (§3.5): :nodes with "x0", "y0", "x1", "y1"; a treemap has no :links output, and asking for one is a fault.

generate(treemap, root)

Every node of root given its rectangle, x0, y0, x1, y1 (§6.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 (§6.1): {:ok, treemap} or {:error, faults}.

tiles()

@spec tiles() :: [tile()]

The tilings: [:squarify, :binary, :slice, :dice, :slice_dice].