Double Click for Properties
We can enhance the hover vis.js
demo to include functionality that lists all properties of a node when it's double-clicked. This addition will provide a more interactive experience, allowing users to inspect node details easily.
Here is how the node insector will look after you double-click on a node:
Features Added:
- Double-Click Event on Nodes: When a user double-clicks on a node, a detailed list of all its properties will be displayed.
- Display Area for Node Properties: A dedicated section below the network graph will show the properties of the selected node.
Updated HTML Code:
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
Explanation of the Additions:
-
HTML Structure Enhancements:
- Node Details Div: A new
<div>
with the IDnode-details
is added below the network graph. This section will display the properties of a node when it's double-clicked. - Styling for Node Details: CSS styles are added to ensure the details section is well-formatted, scrollable if content overflows, and visually distinct.
- Node Details Div: A new
-
Extended Node Definitions:
- Description Property: Each node now includes a
description
property to provide more detailed information. This is optional and can be tailored as needed.
- Description Property: Each node now includes a
-
JavaScript Enhancements:
- displayNodeProperties Function: This function retrieves the node data based on its ID and constructs an HTML table listing all its properties. It formats object properties (like
fixed
andfont
) usingJSON.stringify
for better readability. - formatProperty Helper Function: This helper ensures that object properties are displayed in a readable format by converting them to JSON strings and replacing spaces and line breaks with HTML-friendly equivalents.
- Double-Click Event Listener: An event listener is added to the network to listen for
doubleClick
events. When a node is double-clicked:- If a node is clicked, its ID is retrieved, and
displayNodeProperties
is called to show its properties. - If the empty space is double-clicked, the details section is reset to its default state.
- If a node is clicked, its ID is retrieved, and
- displayNodeProperties Function: This function retrieves the node data based on its ID and constructs an HTML table listing all its properties. It formats object properties (like
-
User Experience Enhancements:
- Responsive Details Section: The node details section updates dynamically based on user interactions, providing immediate feedback.
- Clear Instructions: The details section initially contains a prompt guiding users to double-click on a node to view its properties.
How It Works:
-
Hover Functionality:
- As in the previous version, hovering over nodes or edges displays tooltips with the associated
title
text.
- As in the previous version, hovering over nodes or edges displays tooltips with the associated
-
Double-Click to View Properties:
- User Action: The user double-clicks on a node within the network graph.
- Event Triggered: The
doubleClick
event is captured by the event listener. - Property Display:
- The node's ID is extracted from the event parameters.
- The
displayNodeProperties
function retrieves all properties of the node usingnodes.get(nodeId)
. - An HTML table is constructed listing each property and its value.
- The
node-details
div is updated with this table, allowing the user to view all properties.
-
Clearing Details:
- If the user double-clicks on an empty area of the network graph (i.e., not on any node), the
node-details
section resets to its default message.
- If the user double-clicks on an empty area of the network graph (i.e., not on any node), the
Live Demo:
You can test the updated functionality by running the above HTML code in your local environment or using online editors like CodePen or JSFiddle.
Additional Customizations:
-
Formatting Enhancements:
- You can further enhance the
formatProperty
function to handle specific data types more gracefully or to format certain properties differently.
- You can further enhance the
-
Selective Property Display:
- If you prefer to display only specific properties, modify the
displayNodeProperties
function to filter out unwanted properties.
- If you prefer to display only specific properties, modify the
-
Styling Improvements:
- Customize the CSS to better match your application's theme or to improve readability.
-
Exporting Properties:
- Add functionality to export the node properties (e.g., as a JSON file) if needed.
Feel free to adjust and expand upon this foundation to create a more robust and feature-rich network visualization tool tailored to your specific requirements!