Visualize.Layout.Pack (Visualize v0.2.25)

Copy Markdown View Source

Circle packing layout for hierarchical data.

Enclosing circles show hierarchical containment, with the area of each leaf circle proportional to its value.

The sibling packing is a port of d3-hierarchy's packSiblings (the front-chain algorithm of Wang et al.) and the enclosing circle of d3's packEnclose (Welzl's algorithm over a deterministic shuffle), so a layout is a pure function of its input.

Examples

data = %{
  name: "root",
  children: [
    %{name: "A", value: 100},
    %{name: "B", value: 200},
    %{name: "C", children: [
      %{name: "C1", value: 50},
      %{name: "C2", value: 75}
    ]}
  ]
}

root = Visualize.Layout.Hierarchy.new(data)
  |> Visualize.Layout.Hierarchy.sum(fn d -> d[:value] || 0 end)

pack = Visualize.Layout.Pack.new()
  |> Visualize.Layout.Pack.size([400, 400])
  |> Visualize.Layout.Pack.padding(3)

positioned = Visualize.Layout.Pack.generate(pack, root)

# Each node now has x, y (center) and r (radius)

Summary

Functions

Generates the pack layout

Creates a new pack layout

Sets the padding between circles, in output units.

Sets a custom radius function.

Sets the size of the pack layout

Types

t()

@type t() :: %Visualize.Layout.Pack{
  padding: number() | (Visualize.Layout.Hierarchy.t() -> number()),
  radius: (Visualize.Layout.Hierarchy.t() -> number()) | nil,
  size: {number(), number()}
}

Functions

generate(pack, root)

Generates the pack layout

new()

@spec new() :: t()

Creates a new pack layout

padding(pack, p)

@spec padding(t(), number() | (Visualize.Layout.Hierarchy.t() -> number())) :: t()

Sets the padding between circles, in output units.

Can be a number or a function that takes a node and returns padding.

radius(pack, func)

@spec radius(t(), (Visualize.Layout.Hierarchy.t() -> number())) :: t()

Sets a custom radius function.

If not set, radius is computed from the node's value.

size(pack, arg2)

@spec size(t(), [number()] | {number(), number()}) :: t()

Sets the size of the pack layout