This chapter explains how you can gain extra control over how HTML and SVG elements behave when they are created, updated or removed. It’s particularly relevant when you’re using transitions and want particular effects (such as elements fading in and out).
If you’re new to D3 you can safely skip this chapter!
The examples in the data joins chapter update the HTML/SVG elements in the same manner, regardless of whether they’ve just been created, are already on the page or are about to be removed.
(HTML/SVG elements that have just been created are known as entering elements and ones that are about to be removed are known as exiting elements.)
In some instances it’s useful to treat entering and exiting elements differently. This is especially the case when dealing with transitions. For example, if you’d like new elements to fade in you need to set their initial opacity to zero. Likewise if you’d like them to fade out before being removed, you need to set up a transition on each exiting element so that their opacity gradually reduces to zero.
Version 4 and below of D3 required you use a selection’s
.exitmethods. However from version 5 the
.joinmethod hides the intricacies of enter and exit from you. If you’re still working with version 4 you can still view the original chapter on enter and exit.
You can treat entering and exiting elements differently by passing functions into the
The first, second and third functions are named the enter, update and exit functions, respectively.
Each function has a single parameter::
- the enter function’s parameter
enteris the enter selection which represents the elements that need to be created
- the update function’s parameter
updateis a selection containing the elements that are already in existence (and aren’t exiting)
- the exit function’s parameter
exitis the exit selection and contains elements that need to to be removed
.join method returns a selection containing the entering and existing elements (it doesn’t contain exiting elements). Typically most of your style and attribute updates will follow the
Note that the enter, update and exit functions must return the selection.
In general the enter function must append an element to each element of the enter selection. (The enter selection consists of ‘placeholder’ elements that represent the elements that need to be added.)
This is equivalent to
You can also call the usual selection methods such as
.attr on the enter selection. This allows you to modify style and attributes of the entering elements.
The update function is optional and lets you update elements that are already in existence. For example:
This will set the opacity of entering circles to zero and the opacity of existing circles to 1.
The exit function is optional and handles HTML/SVG elements that need to be removed. In general you need to remove the elements in the exit selection:
If you’re not using transitions you rarely need to the above techniques. However if you are using transitions and you want to control how elements enter and exit the page, you will need the above techniques. See the transitions chapter for examples using transitions.
For now, to help your understanding, here’s an example that sets the opacity of entering nodes to 0.25:
The enter function appends a
circle to each element in the enter selection and sets their opacity to 0.25. The update function sets the opacity of existing circles to 1. The
.style methods after the
.join method apply to entering and existing elements.
Now circles that have just entered the page are feint, while those that were already in existence are solid.
The above example is not very representative of how you’d use enter, exit and update functions in practice. See the transitions for more representative examples.