Hover Text
Things to try:
- Hover over a node - note the text describes the node
- Hover over an edge - note the text describes the edge
Below is a complete HTML example using the vis-network library that demonstrates how to display tooltips when hovering over nodes and edges. The tooltips show the text associated with each vertex (node) and edge.
Features Implemented:
- Nodes with Tooltips: Each node has a
title
property that specifies the text to display when hovered over. - Edges with Tooltips: Similarly, each edge has a
title
property for hover text. - Customized Appearance: Nodes and edges are styled with different colors, shapes, and sizes for better visualization.
Summary
- Both nodes end edges have
title
properties. - The options have an
interaction
section
1 2 3 4 5 6 |
|
Full listing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
Explanation of the Code:
-
Including the vis-network Library:
- The
vis-network
library is included via a CDN link in the<head>
section. -
Styling:
-
The
#mynetwork
div is styled to have a specific width, height, background color, and border. - The body is styled to center the content and add some padding.
-
Defining Nodes:
-
Each node in the
nodes
DataSet has several properties:id
: Unique identifier for the node.label
: Text displayed on the node.color
,shape
,size
,x
,y
: Define the appearance and position.fixed
: Fixes the node's position along the x-axis.font
: Customizes the font size.title
: The tooltip text that appears on hover.
-
Defining Edges:
-
Each edge in the
edges
DataSet connects two nodes (from
andto
) and includes atitle
for the tooltip. -
Network Options:
-
Edges:
- Arrows are enabled at the destination.
- Edges are colored black with a fixed width.
- Smooth curves are added for better aesthetics.
- Nodes:
- Font size and color are customized.
- Borders and shadows are added for better visibility.
- Physics:
- The
forceAtlas2Based
solver is used for node positioning. - Spring length is adjusted for spacing.
- Stabilization iterations ensure the network settles into a stable layout.
- The
- Interaction:
- Hover interactions are enabled to show tooltips.
- A slight delay is added before tooltips appear.
- Layout:
- Improved layout settings enhance the visual arrangement of nodes and edges.
-
Initializing the Network:
-
The
vis.Network
constructor is used to create and render the network inside the#mynetwork
div with the specifieddata
andoptions
. -
Tooltips:
-
The
title
property for both nodes and edges automatically enables tooltips on hover. - The
interaction.hover
option ensures that hovering is enabled, andtooltipDelay
controls the delay before the tooltip appears.
- The
How It Works:
-
Hovering Over Nodes: When you move your mouse over any node, a tooltip appears displaying the
title
associated with that node. For example, hovering over the "Addition" node will show "Addition is the process of finding the total or sum by combining two or more numbers." -
Hovering Over Edges: Similarly, hovering over any edge will display the
title
of that edge. For example, hovering over the edge connecting "Arithmetic" to "Addition" will show "Connects Arithmetic to Addition."
Live Demo:
You can run the above HTML code in your local environment or any online HTML editor (like CodePen or JSFiddle) to see the hover functionality in action.
Additional Customizations:
-
Styling Tooltips: If you want to further customize the appearance of the tooltips, you can add custom CSS or use HTML within the
title
property to include richer content. -
Dynamic Content: You can dynamically update the
title
properties based on user interactions or other events to provide more interactive experiences.
Feel free to modify the nodes, edges, and their associated titles to suit your specific needs!