Skip to content

Laying out overlapping events in a time grid

Overlapping events in a day view are split into connected groups, placed into columns greedily by start time, and sized from each group's column count. A strong answer states the rule and keeps that pass off the drag path.

On this page

What it is

A time grid draws each event as a box whose vertical position comes from time: top is the start as a fraction of the day, height is the duration as a fraction of the day, and the box is positioned absolutely inside the day column. Width and left edge have no such natural source. They come from a layout pass that decides, for each event, how many events it has to share the column with and which slot it takes. Two events overlap when each starts before the other ends; two that touch end to end, such as 10:00 to 11:00 and 11:00 to 12:00, don't.

The pass works in groups. Sort the day's events by start (longer first when starts tie), then walk them while tracking the latest end time seen so far. An event that starts before that time joins the current group; one that starts at or after it closes the group and opens a new one. Groups are independent: an event with no overlap at all stands in a group of one and takes the full width. Inside a group, place each event in the first column whose last event ended at or before this event's start, and open a new column when none fits. With the events in start order, that greedy pass is the optimal coloring of an interval graph: the number of columns it opens equals the largest number of events sharing one instant, and no layout can use fewer. The cost is a sort plus one scan over the columns per event, so for a day's worth of events we'd lay out from scratch rather than maintain the result incrementally.

Then size each box: width = 1 / columns and left = column / columns, using the column count of its own group. When a day's meetings chain together through small overlaps, they form one long group, and the equal-width rule then leaves wide gaps wherever fewer events are present. The fix is expansion: after columns are assigned, let an event widen into the columns to its right until it meets a column holding something that overlaps it, so its width becomes the number of columns it spans divided by the group's total. Our advice: treat expansion as a second pass over the finished column assignment, so the columns stay stable and the widening is purely visual.

ts
type Ev = { id: string; start: number; end: number }; // minutes since midnight
type Box = { id: string; top: number; height: number; left: number; width: number };

const overlaps = (a: Ev, b: Ev) => a.start < b.end && b.start < a.end;

// Returns one box per event, in the input order.
export function layoutDay(events: Ev[]): Box[] {
  const sorted = [...events].sort((a, b) => a.start - b.start || (b.end - b.start) - (a.end - a.start));
  const byId = new Map<string, Box>();
  let group: Ev[] = [];
  let groupEnd = -Infinity;
  for (const ev of sorted) {
    if (ev.start >= groupEnd && group.length) { for (const b of layoutGroup(group)) byId.set(b.id, b); group = []; }
    group.push(ev);
    groupEnd = Math.max(groupEnd, ev.end);
  }
  if (group.length) for (const b of layoutGroup(group)) byId.set(b.id, b);
  return events.map(ev => byId.get(ev.id)!);
}

function layoutGroup(group: Ev[]): Box[] {
  const columns: Ev[][] = [];
  const colOf = new Map<string, number>();
  for (const ev of group) {
    let col = columns.findIndex(c => c[c.length - 1].end <= ev.start);
    if (col === -1) { col = columns.length; columns.push([]); }
    columns[col].push(ev);
    colOf.set(ev.id, col);
  }
  const n = columns.length;
  return group.map(ev => {
    const col = colOf.get(ev.id)!;
    let span = 1;
    while (col + span < n && !columns[col + span].some(o => overlaps(o, ev))) span++;
    return { id: ev.id, top: ev.start, height: ev.end - ev.start, left: col / n, width: span / n };
  });
}

When to use it

Reach for this whenever a day or week grid has to keep overlapping events readable and clickable side by side. The pass is pure CPU on the client, needs no server help, and runs once per day column whenever the day's event list changes: a fetch, an edit committed by the user, or an update pushed from a collaborator.

Keep the pass off the drag path. While an event is being dragged, move its box by setting transform on the element directly rather than updating application state on each pointer move; a write-up of this design snaps the pointer position to 15-minute steps in the handler, and commits one state update plus the API call on drop. transform and opacity are the two properties the compositor can change without layout or paint, so a dragged box that changes nothing else skips the most expensive stages of the pipeline. Give the dragged element its own layer with will-change, and don't promote the other 49 boxes, since layers cost memory.

On drop, rerun the day's layout. A moved event can change the groups around it in several ways: the group it left can split into two, and a long event can merge several groups where it lands, so a per-group cache has to redo the grouping before it knows which columns to keep. Our advice is to start with the full pass, since it is a sort plus one column scan per event, and add caching if profiling shows the pass itself on the frame budget. If the drag crosses into another day column, both days rerun. Our advice for the round: say that the other events don't move while the drag is in progress, and that the layout settles in one step when the drop commits. A preview that reflows neighbors on each pointer move is possible, but it makes the grid shift under the pointer and costs a layout pass per frame.

When not to

Skip the column logic when the day has no overlaps. Each event forms a group of one and takes the full width, so the grouping pass does the right thing with no extra code, and there's no reason to reserve columns for conflicts that aren't there.

Side-by-side columns stop working when the day column is narrow or the overlap is deep: five simultaneous meetings in a week view on a phone become slivers with no readable title. One alternative is a cascade. FullCalendar v7 ships with slotEventOverlap set to true, which lets timed events overlap visually with at most half of each one covered; setting it to false switches to no visual overlap. Another is a cap: lay out the first few columns and show a count for the rest, with a popover to reach the hidden events. Both trade a complete view for legible titles, which is usually the better trade in a narrow column.

The all-day strip and the month view are a different problem. Their events are horizontal spans across days, laid out in rows, and the question becomes which row each span takes and how many rows to show before a count. The interval logic is the same shape, rotated, but the sizing rule above doesn't apply. On a phone-width screen, an agenda list sorted by start often beats any grid, since nothing has to share a width at all.

Compared

ApproachHow it sizes eventsPick it whenWhat it costs

Equal columns per group

width = 1 / columns, left = column / columns within the group

You want aligned edges and overlap is shallow

A long chain of slight overlaps narrows each event in it

Columns plus rightward expansion

An event spans the free columns to its right

Days mix long and short meetings in one group

Left edges vary from event to event; a second pass

Cascade (FullCalendar v7 default)

Timed events are allowed to overlap visually, with at most half of each covered

The day column is narrow, as in a week view

Part of each event is hidden behind a neighbor

Cap the columns, count the rest

Lay out the first few columns; a chip shows how many are hidden

Deep overlap makes columns unreadable

Hidden events need a popover or list to reach

Full width, stacked

Each event takes the whole column

No events overlap

Overlapping events cover each other, and the covered parts can't be seen or clicked

In a picture

Day eventsSort by start

Step 1 of 4

Sort the day

Take four events: A 9:00 to 11:00, B 9:00 to 10:00, C 9:00 to 9:30, D 10:00 to 11:00. Sorted by start, longest first on ties: A, B, C, D.

How to say it in an interview

The common mistake

Quiz

Two meetings run 10:00 to 11:00 and 11:00 to 12:00, with nothing else nearby. How are they laid out?
Fifty meetings each overlap the next by five minutes, and at no instant are more than two in progress. How many columns does the greedy pass open for that group?
A user is dragging one meeting down the day. What should the layout pass do while the pointer moves?

Questions that use this

Hear it in

Related

Sources

  1. UIRound: Lay out overlapping events in a calendar day view
  2. DEV Community: Google Calendar Day View (system design write-up)
  3. Wikipedia: Interval graph
  4. FullCalendar v7 docs: slotEventOverlap
  5. web.dev: Stick to compositor-only properties and manage layer count
Sign in to report a mistake