Menu

BarEdit

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.

<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';

  // This example loads csv data as json using @rollup/plugin-dsv
  import data from './_data/groups.csv';

  data.forEach(d => {
    d.value = +d.value;
  });
</script>

<div class="chart-container">
  <LayerCake
    ssr
    percentRange
    padding={{ top: 0, right: 20, bottom: 20, left: 35 }}
    x="value"
    y="year"
    yScale={scaleBand().paddingInner(0.05).round(true)}
    yDomain={['1979', '1980', '1981', '1982', '1983']}
    xDomain={[0, null]}
    {data}
  >
    <Html>
      <AxisX gridlines baseline snapLabels />
      <AxisY gridlines={false} tickMarks />
    </Html>
    <ScaledSvg>
      <Bar />
    </ScaledSvg>
  </LayerCake>
</div>

<style>
  /*
    The wrapper div needs to have an explicit width and height in CSS.
    It can also be a flexbox child or CSS grid element.
    The point being it needs dimensions since the <LayerCake> element will
    expand to fill it.
  */
  .chart-container {
    width: 100%;
    height: 250px;
  }
</style>