# `Macaw.Draw.Layout.Gantt`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/layout/gantt.ex#L1)

The Gantt time grid (spec/08 §14): tasks as bars against a time axis, in rows grouped by
section, with dependency arrows and the grid behind them.

    iex> alias Macaw.Draw.Layout.Gantt
    iex> alias Macaw.Draw.Scale.Linear
    iex> scale = Linear.new() |> Linear.domain!([0, 10]) |> Linear.range!([0, 100])
    iex> {:ok, gantt} = Gantt.new(row_height: 20, row_pad: 2)
    iex> tasks = [%{id: "a", start: 0, end: 4}, %{id: "b", after: ["a"], end: {3, :second}}]
    iex> {:ok, result} = Gantt.generate(gantt, tasks, scale)
    iex> Enum.map(result.bars, &{&1.id, &1.x0, &1.x1})
    [{"a", 0.0, 40.0}, {"b", 40.0, 70.0}]

Times are mapped through `scale`, a `Macaw.Draw.Scale.Time` (or `Linear`, for a grid of
numbers) whose range is the plot's width, and the scale's ticks are the grid.

**Resolving times** (§14.1), in task order as a topological walk of `after`: a task's
start is its `start`, else the latest end of its `after` tasks, else the previous task's
end (Mermaid's rule); its end is its `end`, or its start plus a `{duration, unit}`
(seconds for `:second` to `:hour`, **calendar days in UTC** for `:day` and `:week`; a
`Date` start adds days, a number start adds the duration's seconds), or its start for a
milestone (spec/08 decision 12).

**Layout** (§14.3): sections in order of first appearance (the unnamed `nil` section
first), tasks in input order within their section, each row `row_height` tall with
`section_header` before each section's first; bars inset by `row_pad`, a milestone a
diamond at its start; one orthogonal arrow per `after` edge from the predecessor's end to
the successor's start, routed around a successor that starts too early; one grid line
per tick, one shade per `excludes` span clipped to the range, and the `now` line when
given and in range.

Options (§14.2), checked by `new/1`:

| Option | Default |
|---|---|
| `row_height` | `24` |
| `row_pad` | `4` |
| `section_header` | `0` |
| `milestone_size` | `0.6` |
| `elbow` | `8` |
| `now` | `nil`, an instant or number; never the clock |
| `excludes` | `[]`, `[{from, to}]` spans already expanded by the caller |
| `ticks` | `nil` (10), the tick count passed to the scale's ticks |

# `t`

```elixir
@type t() :: %Macaw.Draw.Layout.Gantt{
  elbow: number(),
  excludes: [{term(), term()}],
  milestone_size: number(),
  now: term(),
  row_height: number(),
  row_pad: number(),
  section_header: number(),
  ticks: number() | nil
}
```

# `columns`

```elixir
@spec columns(Macaw.Draw.Layout.Gantt.Result.t(), atom() | String.t()) ::
  %{required(String.t()) =&gt; [term()]} | Macaw.Draw.Layout.error()
```

The columns of a Gantt layout (§14.3), `"row"` the task's input row (`nil` for the grid,
shades, sections and the now line):

- `:rows`: `"row"`, `"id"`, `"section"`, `"y0"`, `"y1"`;
- `:bars`: `"row"`, `"id"`, `"x0"`, `"y0"`, `"x1"`, `"y1"`, `"progress_x1"` (`nil`
  without progress);
- `:milestones`: `"row"`, `"id"`, `"x"`, `"y"`, `"size"`, `"path"`;
- `:dependencies`: `"row"` (the successor's), `"source"`, `"target"`, `"path"`;
- `:grid`: `"row"`, `"x"`, `"tick"`, `"y0"`, `"y1"`;
- `:shades`: `"row"`, `"x0"`, `"x1"`, `"y0"`, `"y1"`;
- `:sections`: `"row"`, `"name"`, `"y0"`, `"y1"`, `"band"` (0 and 1 alternating);
- `:now`: no row or one, `"row"`, `"x"`, `"y0"`, `"y1"`.

# `generate`

```elixir
@spec generate(t(), [map()], Macaw.Draw.Scale.Time.t() | Macaw.Draw.Scale.Linear.t()) ::
  {:ok, Macaw.Draw.Layout.Gantt.Result.t()} | Macaw.Draw.Layout.error()
```

The Gantt layout of `tasks` against `scale` (§14.1, §14.3). Each task is a map with `id`
and the optional `row`, `section`, `start`, `end`, `after`, `milestone` and `progress`.

Answers `{:ok, %Gantt.Result{}}` or `{:error, faults}`: the first task with no start is
`{[:tasks, 0, :start], :required}`; an `after` naming no task
`{[:tasks, i, :after], {:unknown_task, id}}`; a cycle of `after`
`{[:tasks, i, :after], {:cycle, ids}}`; an end before its start
`{[:tasks, i, :end], :before_start}`; a task that is no map with an `id`
`{[:tasks, i], {:type, :task}}`, an id given twice `{[:tasks, i, :id], {:duplicate, id}}`,
and a field of the wrong kind a type fault at it.

# `new`

```elixir
@spec new() :: {:ok, t()}
```

`new/1` with every option at its default.

# `new`

```elixir
@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()
```

The layout of `opts` (§14.2): `{:ok, gantt}` or `{:error, faults}`. A `now` that is no
instant or number is `{[:now], {:type, :instant}}`, an `excludes` entry that is no pair of
them `{[:excludes, i], {:type, :span}}`.

# `units`

```elixir
@spec units() :: [atom()]
```

The duration units: `[:second, :minute, :hour, :day, :week]`.

---

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