Archived

Documentation for Layer Cake v10, before Svelte 5 runes. Current docs at layercake.graphics

Menu

Stacked area

Stacked area chart using Layer Cake's stack function. Because this will create a nested data structure, we use LayerCake's flatten function to pass to the flatData prop. See the server-side rendered example for the basic D3 function usage.

<script>
  import { LayerCake, Svg, flatten, stack } from 'layercake';

  import { scaleOrdinal } from 'd3-scale';
  import { format } from 'd3-format';
  import { timeParse, timeFormat } from 'd3-time-format';

  import AxisX from './_components/AxisX.svelte';
  import AxisY from './_components/AxisY.svelte';
  import AreaStacked from './_components/AreaStacked.svelte';

  // The CSV rows are parsed, and their numbers typed, by @rollup/plugin-dsv. See vite.config.js
  import data from './_data/fruit.csv';

  const xKey = 'month';
  // Each stacked point is a [start, end] pair, so the y accessor is those two indexes
  const yKey = [0, 1];
  const cKey = 'key';

  const seriesNames = Object.keys(data[0]).filter(d => d !== xKey);
  const seriesColors = ['#ff00cc', '#ff7ac7', '#ffb3c0', '#ffe4b8'];

  // Turn the date strings into Date objects, on copies so the imported rows stay as they are
  const parseDate = timeParse('%Y-%m-%d');
  const rows = data.map(d => ({ ...d, [xKey]: parseDate(d[xKey]) }));

  const formatLabelX = timeFormat('%b. %-d');
  const formatLabelY = format('~s');

  const stackedData = stack(rows, seriesNames);
</script>

<div class="chart-container">
  <LayerCake
    padding={{ bottom: 20, left: 17 }}
    x={d => d.data[xKey]}
    y={yKey}
    c={cKey}
    cScale={scaleOrdinal()}
    cDomain={seriesNames}
    cRange={seriesColors}
    flatData={flatten(stackedData)}
    data={stackedData}
  >
    <Svg>
      <AxisX format={formatLabelX} tickMarks />
      <AxisY format={formatLabelY} />
      <AreaStacked />
    </Svg>
  </LayerCake>
</div>

<style>
  /* Give the wrapper a width and height. LayerCake fills it. */
  .chart-container {
    width: 100%;
    height: 250px;
  }
</style>