A Gantt chart looks like a calendar, but the useful part is the arithmetic behind it. Once the tasks, their durations and their dependencies are written down, two passes over the list tell you the finish date and which tasks can slip without moving it.
This post shows both passes in plain JavaScript, using a made-up website launch of eight tasks. The numbers are invented for the example.
The tasks
Durations are in calendar days, counted from day 1.
| # | Task | Duration | Depends on |
|---|---|---|---|
| 1 | Write the brief | 2 | none |
| 2 | Choose the page layout | 3 | 1 |
| 3 | Write product text | 5 | 1 |
| 4 | Take product photos | 4 | 1 |
| 5 | Build the pages | 4 | 2 |
| 6 | Load text and photos | 2 | 3, 4, 5 |
| 7 | Test on phone and desktop | 2 | 6 |
| 8 | Launch | 1 | 7 |
Forward pass: earliest start and end
A task can start the day after the last task it depends on ends. The finish date is the largest end day.
const tasks = [ { id: 1, name: 'Write the brief', days: 2, deps: [] }, { id: 2, name: 'Choose the page layout', days: 3, deps: [1] }, { id: 3, name: 'Write product text', days: 5, deps: [1] }, { id: 4, name: 'Take product photos', days: 4, deps: [1] }, { id: 5, name: 'Build the pages', days: 4, deps: [2] }, { id: 6, name: 'Load text and photos', days: 2, deps: [3, 4, 5] }, { id: 7, name: 'Test on phone and desktop', days: 2, deps: [6] }, { id: 8, name: 'Launch', days: 1, deps: [7] }, ]; const byId = new Map(tasks.map((t) => [t.id, t])); for (const t of tasks) { t.start = Math.max(0, ...t.deps.map((d) => byId.get(d).end)) + 1; t.end = t.start + t.days - 1; } const finish = Math.max(...tasks.map((t) => t.end)); This loop works because the list is already in dependency order. If yours is not, sort it first.
Backward pass: latest end and slack
Walk the list in reverse. A task must end early enough for every task that depends on it to still start on time. The gap between its earliest end and its latest end is its slack.
for (const t of [...tasks].reverse()) { const next = tasks.filter((n) => n.deps.includes(t.id)); t.lateEnd = next.length ? Math.min(...next.map((n) => n.lateEnd - n.days)) : finish; t.slack = t.lateEnd - t.end; } The result
Running it on the table above gives:
1 Write the brief start 1 end 2 slack 0 <- critical 2 Choose the page layout start 3 end 5 slack 0 <- critical 3 Write product text start 3 end 7 slack 2 4 Take product photos start 3 end 6 slack 3 5 Build the pages start 6 end 9 slack 0 <- critical 6 Load text and photos start 10 end 11 slack 0 <- critical 7 Test on phone and desktop start 12 end 13 slack 0 <- critical 8 Launch start 14 end 14 slack 0 <- critical finish day 14 Two things stand out.
- The critical path is the chain with zero slack: brief, layout, build, load, test, launch. Its durations add up to 2 + 3 + 4 + 2 + 2 + 1 = 14 days, which is the finish date. Delay any of those by a day and the launch moves by a day.
- The text and the photos have slack. The text ends on day 7 but loading only starts on day 10, so it can finish as late as day 9. The photos end on day 6, which leaves 3 days. Knowing that lets you move people from tasks that can wait to tasks that cannot.
Limits of this sketch
- It counts calendar days. If you plan in working days, convert the dates first.
- It assumes finish-to-start dependencies only. Real plans sometimes need overlaps or lags.
- Durations are elapsed time, not effort. A task with 8 hours of work can still span 4 days if the person is busy elsewhere.
- One number per task hides uncertainty. Ask the person doing the work, and leave a buffer before any date you cannot move.
Drawing it
If you want the bars without writing code, I publish a free Gantt chart maker that runs in the browser with no account: you enter a start date and a duration for each task, and it exports SVG, PNG or CSV. It does not draw dependency lines, so set each start date after the task it waits for. The worked example above, with a longer explanation of the parts of a Gantt chart and common mistakes, is in the guide it links to.
- Guide: https://projectmanagement.practiva.me/guides/what-is-a-gantt-chart/
- Tool: https://projectmanagement.practiva.me/tools/gantt-chart-maker/
Disclosure: Practiva is my site, published by Vision Software in Poland.