Drawing a Network
Learn how to draw a network using nodes and links.
We'll cover the following...
We'll cover the following...
In the previous lesson, we have used the forceSimulation()
API to compute the position for each node in the network. Now it’s time to draw the network using the knowledge of the forceSimulation()
API.
Network visualization
Let’s demonstrate the network using the following example where we have dummy data that contains different people who have links with each other.
Let’s write code to draw a network to show their relationship.
Explanation
Let’s dive into the explanation of the above code to better understand how it works.
- Line 6:
nodes
represents thenames
of different persons, andlinks
in line 13 represents the links between the different nodes. - Line 25: We have used
on('tick', ticked)
which is used to get the state of the layout when it has changed (the simulation has advanced by a tick) and act on it. - Lines 21-24: We have used the
forceSimulation
API to find a position for eachnode
. - Lines 32-38: We have appended the
line
for eachlink
in the data. - Lines 39-45: We have appended the
circle
for eachnode
in thedata
. - Lines 46-53: We have initialized the position of
nodes
andlinks
based on thedata
.
Note: The names of nodes are not visible in the above example. Don’t worry. We will implement this functionality in the upcoming tooltip lesson.