Streamgraph

shifting mix

Shifting composition over time. Good for the mix, poor for reading any single value.

Streamgraph — shifting mix: rendered example
Rendered by Gnomon from the source below. No edits.

The source

71 lines of D3 JavaScript, and uses only the bundled D3 build. Copy it, or open the template inside Gnomon and render it as it is.

// Streamgraph — a stacked area with a wiggle baseline.
// Good for how a *mix* shifts over time; bad for reading any single value,
// because no series sits on a fixed axis. Use a stacked area if the absolute
// numbers matter.

const keys = ['Orders', 'Catalogue', 'Search', 'Payments', 'Identity'];
const rng = d3.randomLcg(11);
const series = d3.range(0, 36).map(month => {
  const row = { month };
  keys.forEach((k, i) => {
    // A slow sine per series plus seeded noise, so the shape has structure
    // rather than looking like static.
    row[k] = Math.max(2, 40 + 30 * Math.sin((month + i * 6) / 5) + rng() * 18);
  });
  return row;
});

const margin = { top: 20, right: 130, bottom: 34, left: 20 };
const stacked = d3.stack()
    .keys(keys)
    .offset(d3.stackOffsetWiggle)
    .order(d3.stackOrderInsideOut)(series);

const x = d3.scaleLinear()
    .domain(d3.extent(series, d => d.month))
    .range([margin.left, width - margin.right]);
const y = d3.scaleLinear()
    .domain([d3.min(stacked, s => d3.min(s, d => d[0])), d3.max(stacked, s => d3.max(s, d => d[1]))])
    .range([height - margin.bottom, margin.top]);

const color = d3.scaleOrdinal(keys, theme.palette);
const area = d3.area()
    .x(d => x(d.data.month))
    .y0(d => y(d[0]))
    .y1(d => y(d[1]))
    .curve(d3.curveBasis);

const svg = d3.select(container).append('svg')
    .attr('viewBox', [0, 0, width, height])
    .attr('width', width)
    .attr('height', height)
    .attr('font-family', 'system-ui, sans-serif')
    .attr('font-size', 11);

svg.append('g')
  .selectAll('path')
  .data(stacked)
  .join('path')
    .attr('d', area)
    .attr('fill', d => color(d.key))
    .attr('fill-opacity', 0.9);

svg.append('g')
    .attr('transform', `translate(0,${height - margin.bottom})`)
    .call(d3.axisBottom(x).ticks(6).tickFormat(d => `M${d}`))
    .call(g => g.select('.domain').remove())
    .call(g => g.selectAll('text').attr('fill', theme.muted))
    .call(g => g.selectAll('line').attr('stroke', theme.muted));

const legend = svg.append('g')
    .attr('transform', `translate(${width - margin.right + 16},${margin.top})`);
legend.selectAll('g')
  .data(keys)
  .join('g')
    .attr('transform', (_, i) => `translate(0,${i * 18})`)
    .call(g => g.append('rect')
        .attr('width', 11).attr('height', 11).attr('rx', 2)
        .attr('fill', d => color(d)))
    .call(g => g.append('text')
        .attr('x', 16).attr('y', 9)
        .attr('fill', theme.foreground).text(d => d));

Render this offline

This template ships in Gnomon and renders on your machine, with no account and no diagram uploads. The browser editor is free and needs no install.

Get GnomonOpen the browser editor

Others in D3 visualisations

Analytics cookies (optional)

May we use Google Analytics to understand visits, product links and feature use? If you accept, Google receives usage and device information, including your IP address, and sets analytics cookies. We do not send your documents, filenames or account details. Advertising features are disabled.

Analytics is off until you accept. Your choice is saved on this browser for 180 days using necessary local storage. You can change it at any time in Analytics preferences. Closing this message rejects analytics. Read our privacy policy.