Skip to content

Instantly share code, notes, and snippets.

@ngminhtrung
Last active May 23, 2018 06:35
Show Gist options
  • Save ngminhtrung/1e25f2f7f94271723f31bb89916413da to your computer and use it in GitHub Desktop.
Save ngminhtrung/1e25f2f7f94271723f31bb89916413da to your computer and use it in GitHub Desktop.
d3-v4 selectable and removable force directed graph

A detailed description of this example can be found at emptypipes.org/2017/04/29/d3v4-selectable-zoomable-force-directed-graph/.

Summary of features provided by original block (Author Peter Kerpedjiev of EmptyPipes):

  1. Clicking on a node selects it and de-selects everything else.
  2. Shift-clicking on a node toggles its selection status and leaves all other nodes as they are.
  3. Shift-dragging toggles the selection status of all nodes within the selection area.
  4. Dragging on a selected node drags all selected nodes.
  5. Dragging an unselected node selects and drags it while de-selecting everything else.

Newly-added features:

  1. Remove selected nodes, re-render the simulation.

Notes from the original block:

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-brush and 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 in d3-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 .fx and .fy parameters. This eliminates the need to set the .fixed parameter 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:

References:

Variables for monitoring status of behavior "brush"

Variables Hold SHIFT release SHIFT
Mouse drag for selecting No mouse's activity Mouse drag for selecting No mouse's activity
brushMode true true false false
brushing true false false false

Tiếng Việt

A detailed description of this example can be found at emptypipes.org/2017/04/29/d3v4-selectable-zoomable-force-directed-graph/.

Summary of features provided by original block (Author Peter Kerpedjiev of EmptyPipes):

  1. Click vào một node để select nó, đồng thời bỏ select những node khác
  2. Giữ SHIFT, đồng thời click vào một node để select/ bỏ select node đấy, trong khi giữa nguyên hiện trạng của những node khác.
  3. Giữ SHIFT, đồng thời giữ chuột trái để quét 1 vùng trên màn hình. Những node nằm trong vùng này sẽ bị "lật" trạng thái, nghĩa là đang bình thường sẽ thành select, đang select sẽ trở về bình thường.
  4. Kéo một node bị select sẽ kéo theo các node bị select khác.
  5. Dragging an unselected node selects and drags it while de-selecting everything else.

Newly-added features:

  1. Remove selected nodes, re-render the simulation.

Notes from the original block:

So với bản "selectable zoomable force directed graph" cũ dùng D3 v3, việc nâng lên D3 v4 yêu cầu một số thay đổi nhỏ và không nhỏ sau:

  • Tính năng "brush" mới trong D3 v4 cho phép bắt phím SHIFT, ALT, và các phím "meta" để thực hiện một số nhiệm vụ mặc định thiết lập bởi D3. Để vượt qua vụ này, tác giả (Peter Kerpedjiev) đã forked d3-brush và chỉnh sửa để nó không còn bắt sự kiện ứng với phím SHIFT. Phiên bản có chỉnh sửa này (d3-brush-lite) được lưu ở đây on github. Trên Github của D3js, một issue đã được tạo đề nghị bỏ đi tính năng bắt SHIFT trong d3-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 .fx and .fy parameters. This eliminates the need to set the .fixed parameter 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:

References:

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment