Tooltip
Learn about the tooltip in D3.js along with an example.
We'll cover the following
Introduction
Let’s dive into the last concept of interactivity, tooltip. Tooltip is an important concept for interactivity in D3.js. We will listen to an event, and every time that event occurs, we will update the information of the tooltip and show it on the screen.
Let’s take a look at the following example to understand the working of the tooltip.
Example
Let’s look at an example of a network and apply a tooltip to it.
Explanation
Let’s dive into the following explanation to better understand how the above code works.
- We have drawn the same network as we did in the Drawing a Network lesson.
- Line 21-28: We have made a
var tooltip
with thetext
tooltip
, and we have made it hidden by using thevisibility
attribute. - Line 54-59: We have made two event listeners, namely
mouseover
andmouseout
. As we hover over the node, we change thetext
to the node’s name and make itvisible
in the top left corner, which is generally the ideal position for the tooltip. As we leave the node, we hide thetooltip
again using themouseout
event.