Loading...
DragElementForce is a built-in behavior in G6 for implementing node dragging under d3-force
and d3-force-3d
layouts. During dragging, the layout is recalculated in real-time, allowing the graph layout to dynamically adjust to accommodate the new position of the nodes.
Add this behavior in the graph configuration:
1. Quick Configuration (Static)
Declare directly using a string form. This method is simple but only supports default configuration and cannot be dynamically modified after configuration:
const graph = new Graph({// Other configurations...behaviors: ['drag-element-force'],});
2. Object Configuration (Recommended)
Configure using an object form, supporting custom parameters, and can dynamically update the configuration at runtime:
const graph = new Graph({// Other configurations...behaviors: [{type: 'drag-element-force',key: 'drag-element-force-1',fixed: true, // Fix node position after dragging},],});
Option | Description | Type | Default | Required |
---|---|---|---|---|
type | Behavior type name, set type: 'drag-element-force' to enable this behavior | string | drag-element-force | ✓ |
key | Unique identifier for the behavior, used for subsequent operations | string | - | |
fixed | Whether to keep the node position fixed after dragging ends, boolean values represent: - true: After dragging ends, the node's position will remain fixed and not be affected by the layout algorithm - false: After dragging ends, the node's position will continue to be affected by the layout algorithm | boolean | false | |
enable | Whether to enable the drag function, by default nodes and combos can be dragged | boolean | ((event: IElementDragEvent) => boolean) | ['node', 'combo'].includes(event.targetType) | |
state | Identifier for the selected state of nodes, when multi-selection is enabled, it will find the selected nodes based on this state | string | selected | |
hideEdge | Controls the display state of edges during dragging, optional values are: - none : Do not hide any edges - out : Hide edges with the current node as the source node - in : Hide edges with the current node as the target node - both : Hide all edges related to the current node - all : Hide all edges in the graph ⚠️ Note: When shadow (ghost node) is enabled, the hideEdge configuration will not take effect. | |||
none | all | in | out | both | none | |||
cursor | Customize the mouse style during dragging, example | { default?: Cursor; grab: Cursor; grabbing: Cursor } | - |
cursor
is used to customize the mouse pointer style during dragging:
default
: Pointer style in default stategrab
: Pointer style when hovering over a draggable elementgrabbing
: Pointer style when draggingOptional values are: auto
| default
| none
| context-menu
| help
| pointer
| progress
| wait
| cell
| crosshair
| text
| vertical-text
| alias
| copy
| move
| no-drop
| not-allowed
| grab
| grabbing
| all-scroll
| col-resize
| row-resize
| n-resize
| e-resize
| s-resize
| w-resize
| ne-resize
| nw-resize
| se-resize
| sw-resize
| ew-resize
| ns-resize
| nesw-resize
| nwse-resize
| zoom-in
| zoom-out
Example configuration:
cursor: {default: 'default', // Use normal pointer by defaultgrab: 'grab', // Show grab pointer when draggablegrabbing: 'grabbing' // Show grabbing pointer when dragging}
DragElementForce
is specifically used for d3-force
or d3-force-3d
layouts, and recalculates the layout in real-time during draggingDragElement
is a general drag interaction and does not trigger layout recalculationTextStyleProps includes the following properties:
icon{TextStyleProps} means you need to use the following property names: