Area and Curve Generators
What you'll learn: How to create filled area charts and apply curve interpolation to make your D3.js visualizations smooth and polished.
The Core Idea
While D3's line generators (from the previous lesson) create the outline of your data, area generators fill the space between your data and a baseline. Think of a line chart showing temperature over time—an area generator would shade the entire region between that line and zero (or any baseline you choose).
Curve generators control how the connecting lines between data points are drawn. Instead of rigid straight segments, you can apply smooth interpolations like curves or natural splines, making your visualizations feel more organic and easier to read.
Area Generators
Use d3.area() to create filled regions:
const areaGenerator = d3.area()
.x(d => xScale(d.date))
.y0(height) // baseline (usually bottom)
.y1(d => yScale(d.value)); // top edge (your data)
svg.append("path")
.datum(data)
.attr("d", areaGenerator)
.attr("fill", "steelblue");
Notice y0 and y1: these define the bottom and top boundaries of your filled area. For a typical area chart, y0 is your chart's bottom.
Curve Interpolation
Both line and area generators accept .curve() to smooth the path:
areaGenerator.curve(d3.curveCatmullRom);
// or d3.curveMonotoneX, d3.curveNatural, d3.curveStep, etc.
Different curves suit different data: curveMonotoneX prevents overshoot for strictly increasing/decreasing data, while curveNatural creates flowing splines.
Combining Both
Pair area and line generators with matching curves for consistent styling—filled area below, stroke outline above.
Key Takeaway: Area generators fill regions between baselines and data, while curve interpolation smooths jagged paths into elegant, readable visualizations.