Archived

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

Menu

Stacked bar

Stacked bar 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 { scaleBand, scaleOrdinal } from 'd3-scale';
  import { format } from 'd3-format';

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

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

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

  const seriesNames = Object.keys(data[0]).filter(d => d !== yKey);
  const seriesColors = ['#00bbff', '#8bcef6', '#c4e2ed', '#f7f6e3'];

  const formatLabelX = format('~s');

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

<div class="chart-container">
  <LayerCake
    padding={{ bottom: 20, left: 35 }}
    x={xKey}
    y={d => d.data[yKey]}
    c={cKey}
    yScale={scaleBand().paddingInner(0.05)}
    cScale={scaleOrdinal()}
    yDomainSort={true}
    cDomain={seriesNames}
    cRange={seriesColors}
    flatData={flatten(stackedData)}
    data={stackedData}
  >
    <Svg>
      <AxisX showBaseline snapLabels format={formatLabelX} />
      <AxisY gridlines={false} />
      <BarStacked />
    </Svg>
  </LayerCake>
</div>

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