D3v4 Selectable, Draggable, Zoomable Force Directed Graph
This interactive force-directed graph demonstrates multi-node selection and manipulation in D3 v4. Users can click to select a single node, shift-click to toggle individual selection, or shift-drag to select all nodes within a brushed region. Dragging a selected node moves the entire selection group, while dragging an unselected node selects and drags it alone. The graph supports zooming and panning, and uses a modified d3-brush-lite to avoid conflicts with shift-key behaviors in v4. Implemented with SVG rendering, the visualization leverages D3 v4's force layout with `.fx`/`.fy` for node pinning and link accessors for cleaner JSON graph data.
AI-generated descriptionA detailed description of this example can be found at <a href="http://emptypipes.org/2017/04/29/d3v4-selectable-zoomable-force-directed-graph/">emptypipes.org/2017/04/29/d3v4-selectable-zoomable-force-directed-graph/</a>.
In summary:
- Clicking on a node selects it and de-selects everything else.
- Shift-clicking on a node toggles its selection status and leaves all other nodes as they are.
- Shift-dragging toggles the selection status of all nodes within the selection area.
- Dragging on a selected node drags all selected nodes.
- Dragging an unselected node selects and drags it while de-selecting everything else.
Upgrading selectable zoomable force directed graph implementation to D3 v4 required a few minor and not-so-minor changes.
- The new brush in v4 captures the shift, alt and meta keys to perform some
actions by default. To get around this, I forked
d3-brushand modified it so that it doesn't capture the shift events. The new version (d3-brush-lite) can be found on github. There is an open github issue to disable this behavior ind3-brush. - Because the d3-drag behavior consumes all events in v4, it is no longer necessary to stop propagation.
- The brush creates its own overlay which catches all events meaning that we don't need to turn the zoom behavior off when the shift key is pressed.
- Whether a node is fixed is specified by the
.fxand.fyparameters. This eliminates the need to set the.fixedparameter on each node. - The force layout in v4 lets us specify an accessor for the nodes that a link connects. This lets us use ids for a link's endpoint and makes the graph specification JSON easier to read:
forked from <a href='http://bl.ocks.org/pkerpedjiev/'>pkerpedjiev</a>'s block: <a href='http://bl.ocks.org/pkerpedjiev/f2e6ebb2532dae603de13f0606563f5b'>D3v4 Selectable, Draggable, Zoomable Force Directed Graph</a>