Archived

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

Menu

Scatter (html)

1980
1985
1990
1995
2000
2005
2010
2015
4
5
6
7

A scatter plot drawn with HTML. The client-side version stacks canvas and SVG layers with a Voronoi hover layer on top. None of those render on the server, so this version uses the HTML scatter component with a stroke to keep the dots visible on their own.

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 so are the dots.

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

  import Scatter from './_components/Scatter.percent-range.html.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/points.csv';

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

  const r = 3;
  // In percent units, so smaller than the client-side version's 10 pixels
  const scalePadding = 2.5;
</script>

<div class="chart-container">
  <LayerCake
    ssr
    percentRange
    padding={{ top: 10, right: 5, bottom: 20, left: 25 }}
    x={xKey}
    y={yKey}
    xPadding={[scalePadding, scalePadding]}
    yPadding={[scalePadding, scalePadding]}
    {data}
  >
    <Html>
      <AxisX gridlines={false} />
      <AxisY gridlines={false} ticks={4} />
      <!-- The client-side version draws these over a canvas layer. There is no canvas on the server, so the dots get a stroke instead. -->
      <Scatter {r} fill="#fff" stroke="#00ccff" strokeWidth={1.5} />
    </Html>
  </LayerCake>
</div>

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