Minimal example of how to use class-based D3 - rather than Svelte - for DOM manipulation, making sure graph re-drawing/responsiveness only triggers from Svelte when necessary by using reactive statements and declarations.
onMount
at all to draw the graph when the graph component's wrapper DOM element is loaded
on mount of the component. Instead, we perform a check inside the reactive declaration for the wrapper DOM element. This does
essentially the same.
onMount
with just the bit of the logic that inserts the svg element into the
container DOM element, and then use a reactive declaration for the rest of the logic,
including passing in reactive props and drawing the graph.
onMount
as it is intended, namely to perform some action that depends on the component having just mounted.
However, I have found the two ways to work identically, and the former to 'look' a little bit cleaner.
Minimal example of how to use class-based D3 - rather than Svelte - for DOM manipulation.
Similar to above, but instead of using reactive statements and declarations from Svelte, we use onMount
,
together with a resize event.
clientWidth
and clientHeight
of the container div of
the graph to variables which are then passed as props to the graph, we use don't pass any width and height related props,
but instead get them from inside the graph-drawing logic with
const width = container
.getBoundingClientRect()
.width
onMount
, and because the responsive
width and height are no longer passed in as props, we need a
separate resize event listener to be able to update the graph based on the dimensions of the container, if we want it to be responsive.
afterUpdate
as well.
This might be ok if the changing variables only change very rarely, but not ok when they update continously,
as is the case for example with a scroller.
The reason for that is that the afterUpdate
logic might fire more often that intended.
getBoundingClientRect()
, when we
could instead just bind the container's dimensions to variables which are then passed as props to the graph,
making it both cleaner and eliminating the need to use a resize event.
Minimal example of how to use Svelte for svg graphs, using D3 only for helper functions (e.g. scales) and for axes. Graph is responsive based on the width of the container and uses only reactive declarations and statements.
bind:clientWidth=w
. These are also used to base other values on, such as
fill, and make them responsive.
each
statement.
Svg-based graph done entriely in Svelte (other than scales), including different transitions for elements that enter and leave the DOM.
each
block, you have to make sure that you can provide
a key with unique ids, i.e. you need to use a keyed each block.
fly
transition imported from svelte/transition
with some optional parameters.
in:fly
with options to make
each circle enter the DOM from a -300px horizontal position, and with each circle entering 200 ms after the previous.
out:fly
with options to make the circles
fly off 200px to the right before disappearing.
transition: all 1s;