The Data Wrangler
All guides

Show missing days as gaps in a D3 line chart

Keep a true zero and a missing day distinct. Add the expected dates to the input and use D3’s defined accessor to break the line.

A broken data line leaves Tuesday blank and shows Monday’s measured zero.
Editorial illustration.

On Monday, a report records zero downloads. Tuesday’s export never arrives. On Wednesday, the table contains four downloads. A line chart can make both days look like ordinary points or skip one of them so quietly that the gap goes unnoticed. Store the missing observation explicitly and make the chart break at it. The zero stays part of the series.

Start with the expected calendar

An absent row gives the drawing code nothing to reject. If Monday and Wednesday are adjacent in your array, most ordinary line generators connect them. Build the dates that the report expects, then join the observations to that calendar. Keep unknown values as null. In the example below, the index is simply the position in a four-day teaching fixture. A live chart can map timestamps to an ordinary date scale.

import { line } from 'd3-shape';

const series = [
  { day: 'Mon', downloads: 0 },
  { day: 'Tue', downloads: null },
  { day: 'Wed', downloads: 4 },
  { day: 'Thu', downloads: 7 }
];
const path = line()
  .defined(row => Number.isFinite(row.downloads))
  .x((row, index) => index * 60)
  .y(row => 80 - row.downloads * 10);

console.log(path(series));

The D3 line documentation explains how a false result closes the current segment. The predicate accepts a numeric zero and rejects null. Running the example produces M0,80ZM120,40L180,10. The path starts once for Monday’s point and again for Wednesday’s segment. With the default curve, an isolated defined point can produce a segment that is invisible as a stroke. Add a small circle for each observed point when it is important that Monday’s zero remain discoverable.

Show what a viewer should infer

Let the blank day occupy its usual horizontal space. On hover or in a companion table, say “No report received” if the source confirms that reason. If you only know the value is absent, label it “Missing observation”. A zero means the count was measured and returned none. A blank says that the data provide no count for that day. If a business owner wants a line drawn through the gap using interpolation, make it visually distinct and explain the assumption in the legend. That turns a hidden decision into a visible one.