Archived

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

Menu

Bar

0
2
4
6
8
10
12
14
16
18
1979
1980
1981
1982
1983

Since we want an ordinal y-axis and Layer Cake defaults to a linear scale, pass in a custom scale to yScale with a few formatting options. Set the x-scale to always start at 0 so you don't show misleading differences between groups.

This is the server-side rendered version. ssr and percentRange on <LayerCake> put the scales in percentages, so the chart renders before the browser measures it. The axes are HTML components and the marks sit in a <ScaledSvg> that stretches to fit its box.

<script>
  import { LayerCake, ScaledSvg, Html } from 'layercake';
  import { scaleBand } from 'd3-scale';

  import Bar from './_components/Bar.svelte';
  import AxisX from './_components/AxisX.percent-range.html.svelte';
  import AxisY from './_components/AxisY.percent-range.html.svelte';

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

  const xKey = 'value';
  const yKey = 'year';
</script>

<div class="chart-container">
  <LayerCake
    ssr
    percentRange
    padding={{ bottom: 20, left: 35 }}
    x={xKey}
    y={yKey}
    yScale={scaleBand().paddingInner(0.05)}
    xDomain={[0, null]}
    {data}
  >
    <Html>
      <AxisX tickMarks showBaseline snapLabels />
      <AxisY tickMarks gridlines={false} />
    </Html>
    <ScaledSvg>
      <Bar />
    </ScaledSvg>
  </LayerCake>
</div>

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