Menu

WebGL Scatter (svg axes, quadtree hover)Edit

1980
1985
1990
1995
2000
2005
2010
2015
4
5
6
7
8
<script>
  import { LayerCake, WebGL, Html } from 'layercake';

  import ScatterWebGL from './_components/Scatter.webgl.svelte';
  import AxisX from './_components/AxisX.percent-range.html.svelte';
  import AxisY from './_components/AxisY.percent-range.html.svelte';
  import QuadTree from './_components/QuadTree.html.svelte';

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

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

  const r = 3;
  const xyPadding = 6;
  const padding = { top: 5, right: 5, bottom: 20, left: 25 };

  data.forEach(d => {
    d[yKey] = +d[yKey];
  });
</script>

<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 {
    position: relative;
    width: 100%;
    height: 400px;
  }

  .circle {
    position: absolute;
    border-radius: 50%;
    background-color: rgba(171,0, 214);
    transform: translate(-50%, -50%);
    pointer-events: none;
    width: 10px;
    height: 10px;
  }
</style>

<div class="chart-container">
  <LayerCake
    position='absolute'
    ssr
    percentRange
    {padding}
    x={xKey}
    y={yKey}
    xPadding={[xyPadding, xyPadding]}
    yPadding={[xyPadding, xyPadding]}
    {data}
  >
    <Html>
      <AxisX/>
      <AxisY tickMarks={false}/>
    </Html>
  </LayerCake>

  <LayerCake
    position='absolute'
    {padding}
    x={xKey}
    y={yKey}
    xPadding={[xyPadding, xyPadding]}
    yPadding={[xyPadding, xyPadding]}
    {data}
  >
    <WebGL>
      <ScatterWebGL
        {r}
      />
    </WebGL>

    <Html>
      <QuadTree
        let:x
        let:y
        let:visible
      >
        <div
          class="circle"
          style="top:{y}px;left:{x}px;display: { visible ? 'block' : 'none' };"
        ></div>
      </QuadTree>
    </Html>
  </LayerCake>
</div>