# `Visualize.Components.Tree`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/components/tree.ex#L2)

Phoenix LiveView function components for hierarchies (spec/10 §3): a node-link tree,
a treemap and a sunburst.

Each is a preset design of `Visualize.Chart.Presets` plus an assign mapping (spec/14
§15.4, D-64, D-70): the nested `data` — a map whose children are under `:children` or
`"children"` — is flattened to one row per node, the design's transforms (`:tree`,
`:treemap` or `:partition`, then the `:filter` steps that keep what spec/10 §3 draws)
lay the rows out, the marks draw them through `Visualize.Chart.Frame.generate/2`, and
the component prints the elements in the markup spec/10 §3 declares, reading the rows
beside them for what follows the node — the tree's circle-and-text group. The
treemap's and the sunburst's labels are their marks', fitted and inked by the layer.
Node labels default to the `:name` or `"name"` of a node's data; leaf values to its
`:value` or `"value"`.

## Usage

    use Phoenix.Component
    import Visualize.Components.Tree

    <.tree_diagram data={@hierarchy} width={800} height={600} />

## Conventions

- A root-only hierarchy is the empty case: the tree draws its one node, the treemap
  one cell, the sunburst nothing but the `<svg>` (the root is never an arc).
- `colors` is a scheme atom of `Visualize.Scale.Color.schemes/0` or an explicit color
  list, taken by index and cycled, as in `Visualize.Components`; an unknown scheme or an
  empty list raises `ArgumentError`.
- `animate={true}` puts `style="transition: all 0.3s ease-in-out;"` on every mark;
  `class` lands on the `<svg>`; coordinates print with at most four decimals.
- The defaults below are `Visualize.Chart.Presets.defaults/1`.

# `sunburst_chart`

Renders a sunburst: the radial partition of the `:partition` transform
(`Visualize.Layout.Partition`) with one arc per node below the root.

## Assigns

* `data` - the nested map with leaf values (required)
* `width`, `height` - pixels (default 600 by 600); the radius is half the smaller
* `value` - as `treemap_chart/1`
* `label` - as `tree_diagram/1`; drawn at the arc's centroid, cut with `…` to the arc's
  length there, and inked the theme's text or background, whichever reads on the arc
* `colors` - as `treemap_chart/1`
* `animate`, `class`, `responsive`, `theme`, `title`, `description` - per the module
  conventions

Angles are `Visualize.Layout.Partition` over `{2π, radius}`: the root spans the full
turn, each child a share of its parent's arc proportional to its summed value, and a
node of value 0 a zero-width slot that draws nothing. Ring radii are
`depth / (height + 1) · radius`. Each arc is `Visualize.Shape.Arc` — angles clockwise
from 12 o'clock — as a `<path fill={color} stroke={:background} stroke-width="1">`.

## Attributes

* `data` (`:map`) (required)
* `width` (`:integer`) - Defaults to `600`.
* `height` (`:integer`) - Defaults to `600`.
* `value` (`:any`) - Defaults to `nil`.
* `label` (`:any`) - Defaults to `nil`.
* `colors` (`:any`) - Defaults to `:category10`.
* `animate` (`:boolean`) - Defaults to `false`.
* `class` (`:string`) - Defaults to `nil`.
* `responsive` (`:boolean`) - Defaults to `false`.
* `theme` (`:any`) - Defaults to `nil`.
* `title` (`:string`) - Defaults to `nil`.
* `description` (`:string`) - Defaults to `nil`.

# `tree_diagram`

Renders a node-link tree laid out by the `:tree` transform (`Visualize.Layout.Tree`).

## Assigns

* `data` - the nested map (required)
* `width`, `height` - pixels (default 800 by 600)
* `margin` - merged over `%{top: 20, right: 120, bottom: 20, left: 120}`, which
  leaves room for the labels of the root and of the leaves
* `orientation` - `:horizontal` (root at the left, depth along x) or `:vertical`
  (root at the top) (default `:horizontal`)
* `node_radius` - (default 6)
* `node_fill` - fill of leaf circles (default `nil`: the theme's `:background`)
* `node_stroke` - circle stroke, and the fill of internal nodes (default `nil`: the
  theme's `:series_1`)
* `link_stroke` - (default `nil`: the theme's `:grid`)
* `label` - accessor of a node's data; `nil` reads `:name` or `"name"`
* `animate`, `class`, `responsive`, `theme`, `title`, `description` - per the module
  conventions

Links are cubic Béziers between parent and child with both control points at the
midpoint of the depth axis. Horizontal labels sit at `x = −10`, anchored `end`, for
internal nodes and at `x = 10`, anchored `start`, for leaves; vertical labels are
centered at `x = 0`.

## Attributes

* `data` (`:map`) (required)
* `width` (`:integer`) - Defaults to `800`.
* `height` (`:integer`) - Defaults to `600`.
* `margin` (`:map`) - Defaults to `%{left: 120, right: 120, bottom: 20, top: 20}`.
* `orientation` (`:atom`) - Defaults to `:horizontal`.
* `node_radius` (`:integer`) - Defaults to `6`.
* `node_fill` (`:string`) - Defaults to `nil`.
* `node_stroke` (`:string`) - Defaults to `nil`.
* `link_stroke` (`:string`) - Defaults to `nil`.
* `label` (`:any`) - Defaults to `nil`.
* `animate` (`:boolean`) - Defaults to `false`.
* `class` (`:string`) - Defaults to `nil`.
* `responsive` (`:boolean`) - Defaults to `false`.
* `theme` (`:any`) - Defaults to `nil`.
* `title` (`:string`) - Defaults to `nil`.
* `description` (`:string`) - Defaults to `nil`.

# `treemap_chart`

Renders a treemap of the hierarchy's leaves laid out by the `:treemap` transform
(`Visualize.Layout.Treemap`).

## Assigns

* `data` - the nested map with leaf values (required)
* `width`, `height` - pixels (default 800 by 600); the layout fills the whole `<svg>`
* `value` - leaf value accessor; `nil` reads `:value` or `"value"`, missing is 0
* `label` - as `tree_diagram/1`
* `tile` - a `Visualize.Layout.Treemap.tile/2` algorithm (default `:squarify`)
* `padding` - uniform cell padding (default 2)
* `colors` - per the module conventions; each child of the root takes the next color
  and every leaf beneath it that color (default `:category10`)
* `animate`, `class`, `responsive`, `theme`, `title`, `description` - per the module
  conventions

A `:label_size` label is drawn at the center of its cell, inked the theme's text or
background, whichever reads on the cell, and cut with `…` to the cell's width; a cell
shorter than the label's font size, a zero-area one among them, draws none.

## Attributes

* `data` (`:map`) (required)
* `width` (`:integer`) - Defaults to `800`.
* `height` (`:integer`) - Defaults to `600`.
* `value` (`:any`) - Defaults to `nil`.
* `label` (`:any`) - Defaults to `nil`.
* `tile` (`:atom`) - Defaults to `:squarify`.
* `padding` (`:integer`) - Defaults to `2`.
* `colors` (`:any`) - Defaults to `:category10`.
* `animate` (`:boolean`) - Defaults to `false`.
* `class` (`:string`) - Defaults to `nil`.
* `responsive` (`:boolean`) - Defaults to `false`.
* `theme` (`:any`) - Defaults to `nil`.
* `title` (`:string`) - Defaults to `nil`.
* `description` (`:string`) - Defaults to `nil`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
