### Install ngraph.graph with npm Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Install the ngraph.graph library using npm for Node.js projects. ```bash npm install ngraph.graph ``` -------------------------------- ### Get a specific link object Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Retrieve a link object between two nodes using the `getLink` method, providing the IDs of the source and target nodes. ```javascript var helloWorldLink = g.getLink('hello', 'world'); // returns a link from 'hello' to 'world' console.log(helloWorldLink); ``` -------------------------------- ### Get a specific node object Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Retrieve a node object from the graph by its ID using the `getNode` method. The returned object contains the node's `id` and `data`. ```javascript var world = g.getNode('world'); // returns 'world' node console.log(world.id, world.data); ``` -------------------------------- ### Create a new graph Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Initialize an empty graph using the `createGraph` function. This graph will have no nodes or edges initially. ```javascript var createGraph = require('ngraph.graph'); var g = createGraph(); ``` -------------------------------- ### Add a link, creating nodes if they don't exist Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Use `addLink` to connect two nodes. If the nodes do not exist, they will be created automatically. ```javascript g.addLink('space', 'bar'); // now graph 'g' has two new nodes: 'space' and 'bar' ``` -------------------------------- ### Include ngraph.graph from CDN Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Include the ngraph.graph library from a CDN for browser usage. The library will be available under the global `createGraph` name. ```html ``` -------------------------------- ### Import ngraph.graph as an ES module Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Import the createGraph function from the ngraph.graph library for use in ES module environments. ```javascript import createGraph from 'ngraph.graph'; const g = createGraph(); ``` -------------------------------- ### Enumerate all links in the graph Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Traverse all links within the graph using the `forEachLink` method. The callback function is executed for each link. ```javascript g.forEachLink(function(link) { console.dir(link); }); ``` -------------------------------- ### Associate data with a node Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Attach arbitrary data to a node using the optional second argument of `addNode`. This data can be retrieved later using `getNode`. ```javascript // Node 'world' is associated with a string object 'custom data' g.addNode('world', 'custom data'); // You can associate arbitrary objects with node: g.addNode('server', { status: 'on', ip: '127.0.0.1' }); // to get data back use `data` property of node: var server = g.getNode('server'); console.log(server.data); // prints associated object ``` -------------------------------- ### Listen to Graph Changes Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Use the `on` method to subscribe to 'changed' events. This is useful for reacting to any modification made to the graph, such as adding nodes or links. The handler receives an array of change records. ```javascript g.on('changed', function(changes) { console.dir(changes); // prints array of change records }); g.addNode(42); // this will trigger 'changed' event ``` -------------------------------- ### Batch Graph Updates Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Employ `beginUpdate()` and `endUpdate()` to group multiple graph modifications into a single event. This is efficient for large-scale changes, as it prevents intermediate event triggers. ```javascript g.beginUpdate(); for(var i = 0; i < 100; ++i) { g.addLink(i, i + 1); // no events are triggered here } g.endUpdate(); // this triggers all listeners of 'changed' event ``` -------------------------------- ### Enumerate all nodes and their data Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Iterate over all nodes in the graph using `forEachNode`. The callback function receives the node object, which contains `id` and `data` properties. ```javascript g.forEachNode(function(node){ console.log(node.id, node.data); }); ``` -------------------------------- ### Associate data with a link Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Attach arbitrary data to a link using the optional third argument of `addLink`. This data is associated with the connection between two nodes. ```javascript // A link between nodes '1' and '2' is now associated with object 'x' g.addLink(1, 2, x); ``` -------------------------------- ### Add individual nodes to the graph Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Add nodes to the graph one at a time using the `addNode` method. Each call adds a distinct node. ```javascript g.addNode('hello'); g.addNode('world'); ``` -------------------------------- ### Add a link between existing nodes Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Connect two nodes that are already present in the graph using `addLink`. This operation only creates the link, not new nodes. ```javascript // Only a link between 'hello' and 'world' is created. No new nodes. g.addLink('hello', 'world'); ``` -------------------------------- ### Enumerate linked nodes for a specific node Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Iterate over nodes connected to a specific node using `forEachLinkedNode`. This includes both inbound and outbound links by default. ```javascript g.forEachLinkedNode('hello', function(linkedNode, link){ console.log("Connected node: ", linkedNode.id, linkedNode.data); console.dir(link); // link object itself }); ``` -------------------------------- ### Clear all nodes and links from the graph Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Reset the graph to an empty state by removing all nodes and links using the `clear` method. ```javascript g.clear(); ``` -------------------------------- ### ChangeRecord Structure Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md A `ChangeRecord` object provides details about a specific modification to the graph. It indicates the type of change and the affected node or link. ```plaintext ChangeRecord = { changeType: add|remove|update - describes type of this change node: - only present when this record reflects a node change, represents actual node link: - only present when this record reflects a link change, represents actual link } ``` -------------------------------- ### Enumerate only outbound links for a specific node Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Use `forEachLinkedNode` with a third boolean argument set to `true` to enumerate only the outbound links from a specified node. ```javascript g.forEachLinkedNode('hello', function(linkedNode, link) { /* ... */ }, true // enumerate only outbound links ); ``` -------------------------------- ### Stop Listening to Graph Events Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Use the `off` method to unsubscribe from 'changed' events. Provide the event name and the handler function to remove the listener. ```javascript g.off('changed', yourHandler); // no longer interested in changes from graph ``` -------------------------------- ### Remove a node from the graph Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Delete a node from the graph using the `removeNode` method, specifying the ID of the node to be removed. ```javascript g.removeNode('space'); ``` -------------------------------- ### Remove a link from the graph Source: https://github.com/anvaka/ngraph.graph/blob/main/README.md Remove a link between two nodes by first obtaining the link object (e.g., via `forEachLinkedNode`) and then passing it to `removeLink`. ```javascript g.forEachLinkedNode('hello', function(linkedNode, link){ g.removeLink(link); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.