# Blazor Gantt Chart

URL: https://flexcoreui.com/demo/gantt  
Section: Charts  
Controls: GanttControl, GanttTask  
Keywords: Gantt chart, project timeline, task dependencies, Gantt Blazor

Plan projects in a Blazor Gantt chart: a task tree beside a day-scaled timeline, with dependencies, progress and bars users drag to change dates.

## Documentation

### Params

| Name | Type | Default | Description |
|---|---|---|---|
| Tasks | IReadOnlyList<GanttTask> | Array.Empty<GanttTask>() | The schedule. Read again only when a different list instance arrives: the control clones the tasks, so changes made to the objects in place are not seen. Checked on arrival against every rule except the dependency rule. |
| AllowEditing | bool | true | False disables Add, Edit and Delete, the dialog, double-click and the drag handles. Expand all, Collapse all and the timescale stay. |
| AllowTaskDrag | bool | true | False removes the end handles and ignores a drag on a bar; the dialog still edits. |
| EnforceDependencies | bool | true | True refuses a change after which any task starts before one of its predecessors ends. The whole schedule is checked on every change, so a list that already breaks the rule refuses every edit until the conflict is repaired. |
| View | GanttView | GanttView.Week | Timescale: Day, Week, Month or Year at 72, 32, 8 or 2 pixels per day, with an axis label every 1, 7, 30 or 90 days. The toolbar's dropdown changes it and raises ViewChanged. |
| Height | string | "450px" | Height of the task grid; the toolbar sits above it. |
| TaskTemplate | RenderFragment<GanttTask>? | null | Replaces the text in the Task column. The row still opens the editor on double-click. |

### Events

| Name | Type | Description |
|---|---|---|
| TasksChanged | IReadOnlyList<GanttTask> | After a create, update or delete has been accepted: a new array holding clones of the whole schedule. Assign it back to Tasks. |
| TaskChanging | GanttTaskChangingArgs | Before the change is stored and after the control's validation. Set Cancel to refuse; the control shows no message of its own and a refused edit keeps its dialog open. |
| ViewChanged | GanttView | After the toolbar dropdown picks a timescale. Not raised when the page sets View. |

### Methods

| Name | Type | Description |
|---|---|---|
| EditAsync(string id) | Task | Opens the edit dialog for that task. Does nothing while editing is off or another change is in flight. |
| CreateAsync() | Task | Opens the Add task dialog with a one-day draft that starts on the timeline's first day. |
| ChangeAsync(GanttTask task, GanttChangeAction action) | Task<bool> | Applies a change from code through the same path as the dialog and the drags: validation, TaskChanging, then TasksChanged. False when editing is off, the change is invalid (ErrorMessage is set) or TaskChanging canceled it. A create needs an unused id; an update or delete needs an existing one. |

### State

| Name | Type | Default | Description |
|---|---|---|---|
| ErrorMessage | string? | null | The last validation message, shown under the toolbar and inside the dialog. Cleared when a dialog opens or closes and at the start of every change. |

### Task

| Name | Type | Default | Description |
|---|---|---|---|
| Id | string | Guid.NewGuid().ToString("N") | Must be unique and not empty. Not shown in the editor. |
| Title | string | "New task" | At least one character. The row text and the bar label. |
| Start | DateTime | DateTime.Today | Bars are placed by date and time; the editor shows both. |
| End | DateTime | DateTime.Today.AddDays(1) | Not before Start. |
| Progress | int | 0 | 0 to 100; the filled part of the bar. |
| ParentId | string? | null | Id of the task this one nests under; it must exist and must not form a cycle. The editor offers the other tasks in a dropdown. |
| Predecessors | string | "" | Comma-separated ids. Each must exist, no cycles, and with EnforceDependencies each must end on or before this task starts. Drawn as an arrow from the predecessor's end to this bar's start while both rows are visible. |
| Color | string | "#2563eb" | The bar's background. Not shown in the editor. |

### Args

| Name | Type | Default | Description |
|---|---|---|---|
| Action | GanttChangeAction | required | Create, Update or Delete. |
| Previous | GanttTask? | null | A clone of the stored task for an update or delete; null for a create. |
| Task | GanttTask | required | A clone of the proposed task; for a delete, the task being removed. |
| Cancel | bool | false | Set true to refuse the change. |

### Keys

| Name | Description |
|---|---|
| Enter or Space on a bar | Opens the editor for that task (the bar is a button). |
| Left or Right arrow on an end handle | Moves that end by one day; the change goes through validation and TaskChanging like a drag. |

### Css

| Name | Description |
|---|---|
| fx-gantt | The root element; CssClass is added here. |
| fx-gantt-tree | The TreeGridControl; its rows are 34px high. |
| fx-gantt-error | The validation message under the toolbar. |
| fx-gantt-axis | The timeline header with its tick labels. |
| fx-gantt-timeline | The timeline cell; its background grid is one day wide. |
| fx-gantt-task | A bar; its left, width and background color are inline. |
| fx-gantt-move | The button that fills the bar and starts a move. |
| fx-gantt-progress | The filled part of the bar; its width is the progress percentage. |
| fx-gantt-bar-label | The title inside the bar. |
| fx-gantt-resize | An end handle, with start or end. |
| fx-gantt-links | The SVG that holds the dependency arrows of a row. |
| fx-gantt-drag-surface | A fixed full-window layer present only while a drag is in progress. |

## Examples

### Schedule

```razor
@using Fx.ControlKit.Gantt

<GanttControl Tasks="@_tasks" TasksChanged="t => _tasks = t" AllowTaskDrag="@_drag" EnforceDependencies="@_enforce" View="GanttView.Month" Height="380px" />
<p class="result">@Summary</p>
```

### Views

```razor
@using Fx.ControlKit
@using Fx.ControlKit.Gantt

@foreach (var v in Enum.GetValues<GanttView>())
{
    <ButtonControl CssClass="btn-sm" Disabled="@(_view == v)" OnClick="() => _view = v">@v</ButtonControl>
}

<GanttControl Tasks="@_plan" AllowEditing="false" View="@_view" ViewChanged="v => _view = v" Height="380px">
    <TaskTemplate Context="task">
        <span>@task.Title</span> <span class="pct">@task.Progress%</span>
    </TaskTemplate>
</GanttControl>
<p class="result">View: @_view.</p>
```

### Veto

```razor
@using Fx.ControlKit.Gantt

<GanttControl Tasks="@_guarded" TasksChanged="t => _guarded = t" TaskChanging="Guard" View="GanttView.Month" Height="380px" />
<p class="result">@_decision</p>
```

