CreateEdge
Previous
CollapseExpand
Next
DragCanvas
Loading...
CreateEdge is a built-in behavior in G6 for interactively creating edges on the canvas. After the user triggers the behavior (click or drag), the edge will follow the mouse movement and connect to the target node to complete the creation. If canceled, it will be automatically removed.
Additionally, this behavior supports customizing the style of the edge, such as color, line style, arrow, etc., to meet different visualization needs.
The elements that can be connected by this behavior are node
and combo
.
This behavior is mainly used for:
createGraph({data: {nodes: [{ id: 'node1', combo: 'combo1', style: { x: 250, y: 150 } },{ id: 'node2', combo: 'combo1', style: { x: 350, y: 150 } },{ id: 'node3', combo: 'combo2', style: { x: 250, y: 300 } },],edges: [],combos: [{ id: 'combo1', combo: 'combo2' },{ id: 'combo2', style: {} },],},node: { style: { fill: '#873bf4' } },edge: { style: { stroke: '#8b9baf' } },behaviors: [{type: 'create-edge',key: 'create-edge',},],plugins: [{ type: 'grid-line', size: 30 }],animation: true,},{ width: 600, height: 400 },(gui, graph) => {const options = {key: 'create-edge',type: 'create-edge',animation: true,enable: true,trigger: 'drag',};const optionFolder = gui.addFolder('CollapseExpand Options');optionFolder.add(options, 'type').disable(true);optionFolder.add(options, 'animation');optionFolder.add(options, 'enable');optionFolder.add(options, 'trigger', ['drag', 'click']);optionFolder.onChange(({ property, value }) => {graph.updateBehavior({key: 'create-edge',[property]: value,});graph.render();});},);
Add this behavior in the graph configuration
// Use default configurationconst graph = new Graph({// Other configurations...behaviors: ['create-edge'], // Directly add, use default configuration});// Or use custom configurationconst graph = new Graph({// Other configurationsbehaviors: [{type: 'create-edge',trigger: 'click', // Behavior configuration, create edge by clickingstyle: {}, // Custom edge style},],});
Option | Description | Type | Default | Required |
---|---|---|---|---|
type | Behavior type name | string | create-edge | √ |
trigger | The way to trigger the creation of a new edge: click means click to trigger; drag means drag to trigger | click | drag | drag | |
enable | Whether to enable this behavior | boolean | ((event: Event) => boolean) | true | |
onCreate | Callback function for creating an edge, returns edge data | (edge: EdgeData) => EdgeData | - | |
onFinish | Callback function for successfully creating an edge | (edge: EdgeData) => void | - | |
style | Style of the newly created edge, configuration options | See below | - |
Configure the style of the newly created edge, for detailed configuration options, please refer to Element - Edge - General Edge Properties - Style
{"style": {"stroke": "red","lineWidth": 2}}
const graph = new Graph({container: 'container',width: 800,height: 600,behaviors: ['create-edge'],});
const graph = new Graph({// Other configurations,behaviors: [{type: 'create-edge',style: {stroke: 'red',lineWidth: 3,},},],});
const graph = new Graph({// Other configurationsbehaviors: [{type: 'create-edge',trigger: 'click',},],});