Skip to content

Commit 315d52a

Browse files
committed
add svelte example
1 parent 2d92890 commit 315d52a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/getting-started.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,33 @@ export default {
289289
```
290290

291291
As with React, to update your plot for whatever reason, simply render a new one and replace the old one. You can find more examples on [our GitHub](https://github.com/observablehq/plot/tree/main/docs) as this documentation site is built with VitePress and uses both client- and server-side rendering for plots!
292+
293+
## Plot in Svelte
294+
295+
Here’s an example of client-side rendering in Svelte. For server-side rendering, see [#1759](https://github.com/observablehq/plot/discussions/1759).
296+
297+
:::code-group
298+
```svelte [App.svelte]
299+
<script>
300+
import * as Plot from '@observablehq/plot';
301+
import * as d3 from 'd3';
302+
303+
let div;
304+
let data = d3.ticks(-2, 2, 200).map(Math.sin);
305+
306+
function onMousemove(event) {
307+
const [x, y] = d3.pointer(event);
308+
data = data.slice(-200).concat(Math.atan2(x, y));
309+
}
310+
311+
$: {
312+
div?.firstChild?.remove(); // remove old chart, if any
313+
div?.append(Plot.lineY(data).plot({grid: true})); // add the new chart
314+
}
315+
</script>
316+
317+
<div on:mousemove={onMousemove} bind:this={div} role="img"></div>
318+
```
319+
:::
320+
321+
See our [Plot + Svelte REPL](https://svelte.dev/repl/ebf78a6a6c1145ecb84cf9345a7f82ae?version=4.2.0) for details.

0 commit comments

Comments
 (0)