Skip to content

Instantly share code, notes, and snippets.

@JsonChiu
JsonChiu / README.md
Last active August 28, 2015 07:00 — forked from mbostock/.block
Collapsible Force Layout

This example demonstrates how to create a force layout of a hierarchy whose internal nodes are collapsible. Leaf nodes are shown in orange, while internal nodes (packages) are shown in blue. Clicking on an internal node (without dragging) causes that node to expand or collapse, toggling the visibility of its descendant nodes.

@JsonChiu
JsonChiu / index.html
Last active August 28, 2015 06:49 — forked from mbostock/.block
Collapsible Force Layout
<!DOCTYPE html>
<meta charset="utf-8">
<title>Force-Directed Graph</title>
<style>
.node {
cursor: pointer;
stroke: #3182bd;
stroke-width: 1.5px;
}
@JsonChiu
JsonChiu / README.md
Last active August 28, 2015 06:48 — forked from mbostock/.block
Sticky Force Layout

This example demonstrates how to prevent D3’s force layout from moving nodes that have been repositioned by the user. When the force layout’s drag behavior dispatches a dragstart event, the fixed property of the dragged node is set to true. This prevents the force layout from subsequently changing the position of the node (due to forces). Double-click to release a node.

Internally, the force layout uses three bits to control whether a node is fixed. The first bit can be set externally, as in this example. The second and third bits are set on mouseover and mousedown, respectively, so that nodes are fixed temporarily during dragging. Although the second and third bits are automatically cleared when dragging ends, the first bit stays true in this example, and thus nodes remain fixed after dragging.

Also note that the force layout resumes au