# Baselines

> Save the planned dates of a project as baselines, show them under the task bars, compare the current schedule with the plan and exchange baselines through MS Project XML.

A baseline is a snapshot of the planned dates, taken when a plan is approved. Showing it under the live schedule makes slippage visible at a glance: a bar that has moved to the right of its baseline is late.

## Baseline data

Each task can carry a list of baselines, each with a `start` and an `end`:

```ts
const tasks = [
  {
    id: 'racking',
    name: 'Install pallet racking',
    duration: 10,
    baselines: [
      { start: '2027-01-11T08:00', end: '2027-01-22T17:00' },
      { start: '2027-01-18T08:00', end: '2027-01-29T17:00' },
    ],
  },
];
```

Index `0` is the original plan; later entries are re-baselines. Baselines are data: the engine never changes them, and they do not affect scheduling.

## Show baselines

```ts
createGantt(container, { tasks, dependencies, showBaselines: true, baselineIndex: 0 });

compareSelect.addEventListener('change', () => {
  gantt.update({ baselineIndex: Number(compareSelect.value) });
});
```

| Option | Default | Description |
| --- | --- | --- |
| `showBaselines` | `false` | Draw the selected baseline of each task as a thin bar under its task bar, or as a small diamond for milestones. |
| `baselineIndex` | `0` | Which entry of `baselines` to draw. Tasks without that entry show no baseline. |

The root element gets the class `lq-gantt--baselines` while baselines are shown, and their color comes from `--lq-gantt-baseline`. Baseline bars are decorative and hidden from assistive technology; put variance in a grid column (below) so it is available to everyone.

## Take a baseline

Copy the current scheduled dates into the baseline slot you want. Building the new task list once and loading it is fastest for large projects:

```ts
function saveBaseline(index: number) {
  const project = gantt.toJSON();
  project.tasks = project.tasks.map((task) => {
    const baselines = [...(task.baselines ?? [])];
    baselines[index] = { start: task.start!, end: task.end! };
    return { ...task, baselines };
  });
  gantt.fromJSON(project);
}

approveButton.addEventListener('click', () => saveBaseline(0));
```

`toJSON()` writes every task's scheduled `start` and `end`, so they are the dates you capture. `fromJSON` reloads the project and clears the undo history, which is usually what you want after approving a plan. To keep the history, call `updateTask(id, { baselines })` per task instead; each call is one undo step.

## Show variance in the grid

A custom column can show how far each task's finish has moved from the baseline:

```ts
const DAY = 24 * 60 * 60 * 1000;

createGantt(container, {
  tasks,
  dependencies,
  showBaselines: true,
  columns: [
    'wbs',
    'name',
    'start',
    'end',
    'duration',
    {
      id: 'finishVariance',
      header: 'Finish variance',
      width: 120,
      align: 'right',
      renderer: (task) => {
        const baseline = task.baselines?.[0];
        if (!baseline) return '';
        const days = Math.round((task.end.getTime() - new Date(baseline.end).getTime()) / DAY);
        return days === 0 ? 'On plan' : days > 0 ? `${days} days late` : `${-days} days early`;
      },
    },
  ],
});
```

The variance here is in calendar days. For working days, compare with `getSlack()` or compute working time on your server with the [headless engine](https://docs.birchui.com/gantt/headless.md).

## Baselines in MS Project XML

`toMSProjectXML()` writes every baseline as a `<Baseline>` element with its number, start, finish and duration, and `fromMSProjectXML()` reads them back into `baselines`, so a plan approved in MS Project shows up in the Gantt and the other way round. See [MS Project XML](https://docs.birchui.com/gantt/ms-project.md).

## Next steps

- [Task grid](https://docs.birchui.com/gantt/task-grid.md) for custom columns.
- [Scheduling](https://docs.birchui.com/gantt/scheduling.md) for slack and the critical path.
