Archived

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

Menu

Brush

A line chart with a brush under it. The Brush.html.svelte component reports a min and max from 0 to 1 through bindable props, and the top chart shows only that slice of the rows. Drag on the bottom chart to try it.

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

  import Line from './_components/Line.svelte';
  import Area from './_components/Area.svelte';
  import AxisX from './_components/AxisX.svelte';
  import AxisY from './_components/AxisY.svelte';
  import Brush from './_components/Brush.html.svelte';

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

  /** @type {[number|null, number|null]} */
  let brushExtents = $state([null, null]);

  const xKey = 'myX';
  const yKey = 'myY';

  // The rows inside the brush, with at least two so the line still draws when the brush is very narrow
  let brushedData = $derived.by(() => {
    const start = (brushExtents[0] ?? 0) * data.length;
    const end = (brushExtents[1] ?? 1) * data.length;
    const selection = data.slice(start, end);
    if (selection.length < 2) return data.slice(start, start + 2);
    return selection;
  });
</script>

<div class="brushed-chart-container">
  <LayerCake
    padding={{ bottom: 20, left: 25 }}
    x={xKey}
    y={yKey}
    yDomain={[0, null]}
    data={brushedData}
  >
    <Svg>
      <AxisX
        ticks={ticks => {
          const filtered = ticks.filter(t => t % 1 === 0);
          if (filtered.length > 7) {
            return filtered.filter((t, i) => i % 2 === 0);
          }
          return filtered;
        }}
      />
      <AxisY ticks={4} />
      <Line stroke="#00e047" />
      <Area fill="#00e04710" />
    </Svg>
  </LayerCake>
</div>

<div class="brush-container">
  <LayerCake padding={{ top: 5 }} x={xKey} y={yKey} yDomain={[0, null]} {data}>
    <Svg>
      <Line stroke="#00e047" />
      <Area fill="#00e04710" />
    </Svg>
    <Html>
      <Brush bind:min={brushExtents[0]} bind:max={brushExtents[1]} />
    </Html>
  </LayerCake>
</div>

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