Layout

5 min read

In the process of graph analysis, we need different layouts for different analysis scenes.

01. Definition of layout

  • Layout, as the name suggests, is how to place nodes. It is determined by x and y of nodes.
  • The layout algorithm is a function that adds x and y to a node.

The layout is specified by Graphin's props.layout:

<Graphin data={data} layout={{ name: 'force', options: {} }} />

02. Built-in layout

01. circle

  • name: circle
  • character: arranged in a circle order.
  • scene: When we find the key node in a group of nodes, it has the most related nodes. so with the circle layout, we can easily find this key node in the center.
  PropertyTypeDefaultDescription
xnumberCanvasWidth / 2x of the center node
ynumberCanvasHeight / 2y of the center node
rnumberNumber of nodes * 10Radius of the circle
scalenumber0.8Scaling ratio

02. concentric

  • name: concentric
  • character: The nodes are sorted by degree, and a group of nodes with a large degree are arranged at the center, and nodes with a small degree are distributed at the outermost layer.
  • scene: When we find the key node in a group of nodes, it has the most related nodes. so with the circle layout, we can easily find this key node in the center.
  PropertyTypeDefaultDescription
boundingBoxobject{ x:0, y:0, w:CanvasWidth, h:CanvasHeight }Range of the layout
minNodeSpacingnumber60Distance between nodes
levelWidthfunction(nodes: Data['nodes'], maxDegree: number) => numberrange of degree in each layer

example of levelWidth:

const LevelWidthFunction = (nodes: Data['nodes'], maxDegree: number) => {
    /** Number of layers */
    const levelNum = 8;
    return maxDegree / levelNum;
};

03. grid

  • name: grid
  • character: Arrange the nodes in order, presenting a grid
  • scene: The position of the node is expanded according to the user-defined sorting. It is generally used for pre-analysis of other layouts.
  PropertyTypeDefaultDescription
widthnumberCanvasWidthWidth of the layout
heightnumberCanvasHeightHeight of the layout
nodeSepnumber100Distance between nodes
nodeSizenumber50Size of the node

04. radial

  • name: radial
  • character: Nodes spread out like radar, which is the main layout algorithm for solving crossover problems in static layouts.
  • scene:
  PropertyTypeDefaultDescription
center[number,number][CanvasWidth/2,CanvasHeight/2]coordinate of the center node
preventOverlapboleanCanvasHeightprevent coverage
nodeSizenumber100size of a node
unitRadiusnumber150radius of each layer

05. dagre

  • name: dagre
  • character: Tree arrangement according to the direction of the edge and the level of the node
  • scene: When we need to know the hierarchy, the upstream and downstream relationship of the data, dagre is a good way.
  PropertyTypeDefaultDescription
center[number,number][width / 2, height / 2]coordinate of the center node
nodeSizenumberCanvasHeightsize of a node
nodesepnumber12horizontal distance between nodes
ranksepnumber50vertical distance between nodes
alignstringULposition

06. force

  • name: force
  • character: The nodes are distributed according to the natural force. The simulated charge repulsion between the nodes remains disjoint, and the spring tension is not dissociated. Finally, a force balance is achieved in multiple dynamic iterations.
  • scene: When you want to solve the problem of intersecting nodes, it is very suitable to use the force layout. Generally used as a pre-layout for other layouts
  PropertyTypeDefaultDescription
presetobject{ name: 'random',options:{}}Front layout that is used for switching layout mainly. tweak layout algorithm will be used when the current layout is force
stiffnessnumber200Spring stiffness factor
defSpringLennumber200Default spring length
repulsionnumber200.0 * 5Repulsive force, here refers to the Coulomb constant Ke
dampingnumber0.9Damping coefficient
minEnergyThresholdnumber0.1Minimum energy threshold
maxSpeednumber1000Maximum speed, range interval [0,1000]
MaxIterationsnumber10000001000000 times/(1000/60) = 60000 s = 1 min
animationboleantrueWhether to turn on animation
// details of the force layout:

- edge.data.spring can be used to set the spring length of edge
- node.layout.force.mass can be used to set the quality of the node