plots in Svelte


I have a basic implementation of using plot in svelte, but I am not understanding how to translate the examples/tutorials into this syntax as the options is a json object that seems to include for example. Would anyone be able to show how it’s done in a most simplistic way, e.g. making a scatter plot using the data variable? There’s a REPL for the svelte implementation here:

(log in with GitHub, save/fork, edit save and repost link)

Many thanks!

Let’s use a more recent version of Plot in the component:


  import * as Plot from "@observablehq/plot@0.6";
  export let options;
  function addChart(node) { node.appendChild(Plot.plot(options)); }

{#key options}
  <div use:addChart {...$$restProps} />

In the App, we’ll want to distinguish PlotRender (the Svelte component) and Plot, by importing them separately. We’ll also make the charts transparent instead of the default white background.


  import * as Plot from "@observablehq/plot@0.6";
  import PlotRender from './Plot.svelte';
  let a = 100;
  let b = 0;
  const data=[{x:0,y:1},{x:1,y:4},{x:2,y:2},{x:3,y:5},{x:4,y:6},{x:5,y:45}]

<PlotRender options={{
  x: { domain: [a, b] },
  grid: true,
    Plot.tickX(data, {x:"x"})
  style: "background: transparent;"
}} />
<label>From <input type=number bind:value={a}></label>
<label>To <input type=number bind:value={b}></label>

many thanks, I understand now how to use it! cheers