Polyline
Reading needs 3 minutes
Before reading this section, please first read the API - Edge Configuration chapter.
createGraph({data: {nodes: [{id: 'node1',style: { x: 150, y: 150 },},{id: 'node2',style: {x: 400,y: 150,labelText: 'Drag Me!',labelPadding: [1, 5],labelBackground: true,labelBackgroundRadius: 10,labelBackgroundFill: '#99add1',},},],edges: [{id: 'edge1',source: 'node1',target: 'node2',text: 'polyline',},],},node: {style: {fill: '#f8f8f8',stroke: '#8b9baf',lineWidth: 1,},},edge: {type: 'polyline',style: {stroke: '#7e3feb',lineWidth: 2,labelText: (d) => d.text,labelBackground: true,labelBackgroundFill: '#f9f0ff',labelBackgroundOpacity: 1,labelBackgroundLineWidth: 2,labelBackgroundStroke: '#7e3feb',labelPadding: [1, 10],labelBackgroundRadius: 4,router: { type: 'orth' },},},behaviors: ['drag-canvas', 'drag-element'],plugins: [{ type: 'grid-line', size: 30 }],},{ width: 600, height: 300 },(gui, graph) => {gui.add({ type: 'polyline' }, 'type').disable();let index = 3;const options = {radius: 0,router: {type: 'orth',},random: () => {const x = Math.floor(Math.random() * 600);const y = Math.floor(Math.random() * 300);graph.addNodeData([{id: `node-${index}`,style: {size: 5,fill: '#7e3feb',x,y,},},]);index++;graph.updateEdgeData((prev) => {const targetEdgeData = prev.find((edge) => edge.id === 'edge1');const controlPoints = [...(targetEdgeData.style.controlPoints || [])];controlPoints.push([x, y]);return [{ ...targetEdgeData, style: { ...targetEdgeData.style, controlPoints } }];});graph.render();},};const optionFolder = gui.addFolder('polyline.style');optionFolder.add(options, 'radius', 0, 100, 1);optionFolder.add(options, 'router');optionFolder.add(options, 'random').name('Add random node as control points');optionFolder.onChange(({ property, value }) => {if (property === 'random') return;graph.updateEdgeData([{ id: 'edge1', style: { [property]: value } }]);graph.render();});},);
设置 edge.type
为 polyline
以使用折线。
If the element has its specific properties, we will list them below. For all generic style attributes, seeBaseEdge
[number, number] | [number, number, number] | Float32Array__[]
Control point array
number Default:
0
The radius of the rounded corner
false | OrthRouter | ShortestPathRouter Default:
false
Whether to enable routing, it is enabled by default and controlPoints will be automatically included
Property | Description | Type | Default |
---|---|---|---|
type | Orthogonal router, adds extra control points to the path to ensure each segment of the edge remains horizontal or vertical | 'orth' | - |
padding | Minimum distance between node connection points and corners | Padding | 0 |
Property | Description | Type | Default |
---|---|---|---|
type | Shortest path router, an intelligent version of the orthogonal router ('orth' ). This router consists of horizontal or vertical orthogonal line segments. It uses the A* algorithm to calculate the shortest path and supports automatically avoiding other nodes (obstacles) on the path | 'shortest-path' | - |
offset | Minimum distance between node anchor points and corners | Padding | 0 |
gridSize | Size of the grid cells | number | 0 |
maxAllowedDirectionChange | Maximum allowed rotation angle (in radians) | number | 0 |
startDirections | Possible starting directions from the node | Direction[] | 0 |
endDirections | Possible ending directions to the node | Direction[] | 0 |
directionMap | Specifies the directions that can be moved | { [key in Direction]: { stepX: number; stepY: number } } | 0 |
penalties | Represents additional costs for certain paths during path search. Keys are radian values, values are costs | { [key: string]: number } | 0 |
distFunc | Function that specifies how to calculate the distance between two points | (p1: Point, p2: Point) => number | 0 |
maximumLoops | Maximum number of iterations | number | 0 |
enableObstacleAvoidance | Whether to enable obstacle avoidance | boolean | false |
type Direction = 'left' | 'right' | 'top' | 'bottom';
type Point = [number, number] | [number, number, number] | Float32Array;
type Padding = number | [number, number] | [number, number, number, number];