Code

D3.js

Area Chart

Intermediate Stacked area chart using d3.stack() to compute layer baselines and d3.area() with y0 / y1 accessors. d3-shape/area

const keys = ['Organic', 'Referral', 'Direct'];

// Stack layout — computes [y0, y1] for each layer
const stack = d3.stack().keys(keys);
const series = stack(data);

const y = d3.scaleLinear()
  .domain([0, d3.max(series,
    s => d3.max(s, d => d[1]))])
  .range([innerHeight, 0]);

// Area generator uses stacked y0/y1
const area = d3.area()
  .x(d => x(d.data.date))
  .y0(d => y(d[0]))   // bottom of layer
  .y1(d => y(d[1]))   // top of layer
  .curve(d3.curveCatmullRom);

g.selectAll('path')
  .data(series)
  .join('path')
  .attr('fill', (_, i) => colors[i])
  .attr('d', area);
Key APIs
  • d3.stack().keys(keys) — stack layout generator for multi-series data
  • d3.area().y0(accessor).y1(accessor) — area path with baseline and topline
  • Each stacked datum is [y0, y1] with .data reference to the original row
  • d3.max(series, s => d3.max(s, d => d[1])) — find the stacked maximum for the y domain
  • d3.curveCatmullRom — smooth curved interpolation between points