link.id([id])
If id is specified, sets the node id accessor to the specified function and returns this force. If id is not specified, returns the current node id accessor, which defaults to the numeric node.index:
function id(d) { return d.index; }
The default id accessor allows each link’s source and target to be specified as a zero-based index into the nodes array. For example:
var nodes = [ {"id": "Alice"}, {"id": "Bob"}, {"id": "Carol"} ]; var links = [ {"source": 0, "target": 1}, // Alice → Bob {"source": 1, "target": 2} // Bob → Carol ];
Now consider a different id accessor that returns a string:
function id(d) { return d.id; }
With this accessor, you can use named sources and targets:
var nodes = [ {"id": "Alice"}, {"id": "Bob"}, {"id": "Carol"} ]; var links = [ {"source": "Alice", "target": "Bob"}, {"source": "Bob", "target": "Carol"} ];
This is particularly useful when representing graphs in JSON, as JSON does not allow references. See this example.
The id accessor is invoked for each node whenever the force is initialized, as when the nodes or links change, being passed the node and its zero-based index.
Please login to continue.