TreeGraph Layout

8 min read

Introduction

Graph layouts are the algorithms arranging the node positions to obtain a understandable visualizaiton. According to the differences of data strucutre, the layouts can be categorized into: general graph layout and tree graph layout. There are several layout algorithms for them respectively. By utilizing the built-in layouts, Translating the layouts and their configurations, translating the data can be achieved. Besides, G6 provides the Web-Worker for general graph layout in case layout calculation takes too long to block page interaction.

Besides, G6 supports Custom Layout mechanism for users to design their own layout algorithm.

In fact, 'layout' is a free mechanism in G6. The built-in layouts only calculate and manipulate the x and y in node data. In other word, users can assign x and y to nodes by any other ways including the algorithms from the third-party libraries. Once G6 find the x and y information on data, it will render the graph according to it.

In order to handle the tree data structure, G6 extends Graph to TreeGraph. Refer to: TreeGraph API. TreeGraph is appropriate for visualizing hierarchy data. In this ducoment, we will introduce the TreeGraph layout algorithms in detail.

TreeGraph Layouts Overview

Configure the TreeGraph

Similar to Graph, assign layout to Graph instance to set the layout for a TreeGraph. The Expand/Collapse behavior can be assigned to the TreeGraph by modes.

const graph = new G6.TreeGraph({
  container: 'mountNode',
  modes: {
    default: [
      {
        // Assign the collapse/expand behavior
        type: 'collapse-expand',
      },
      'drag-canvas',
    ],
  },
  // Assign the layout
  layout: {
    type: 'dendrogram', // Layout type
    direction: 'LR', // Layout direction is from the left to the right. Options: 'H' / 'V' / 'LR' / 'RL' / 'TB' / 'BT'
    nodeSep: 50, // The distance between nodes
    rankSep: 100, // The distance between adjacent levels
  },
});

Layouts for TreeGraph

compactBox

Description: CompactBox is the default layout for TreeGraph. It will consider the bounding box of each node when layout.
img
API: CompactBox API
Configuration:

NameTypeExample/OptionsDefaultDescription
directionString'TB' / 'BT' / 'LR' / 'RL' / 'H' / 'V''LR'The direction of layout.
- TB —— Root is on the top, layout from the top to the bottom
- BT —— Root is on the bottom, layout from the bottom to the top
img     img
(Left)TB. (Right)BT.
- LR —— Root is on the left, layout from the left to the right
- RL —— Root is on the right, layout from the right to the left
img             img
(Left)LR. (Right)RL.
- H —— Root is on the middle, layout in horizontal symmetry.
- V —— Root is on the middle, layout in vertical symmetry.
img          img
> (Left)H. (Right)V.
getIdFunction(d) => {
  // d is a node
  return d.id + 'node';
}
undefinedSets the id for each node
getHeightFunction(d) => {
  // d is a node
  return 10;
}
undefinedThe height of each node
getWidthFunction(d) => {
  // d is a node
  return 20;
}
undefinedhe width of each node
getVGapFunction(d) => {
  // d is a node
  return 100;
}
undefinedThe vertical separation of nodes
getHGapFunction(d) => {
// d is a node
  return 50;
}
undefinedThe horizontal separation of nodes
radialBooleantruefalseIf layout the graph in radial style. If radial is true, we recommend to set direction to 'LR' or 'RL': img

dendrogram

Description: Arranges all the leaves on the same level. It is appropriate for hierarchical clustering. It does not consider the node size, which will be regarded as 1 px.
img
API: Dendrogram API
Configuration:

NameTypeExample/OptionsDefaultDescription
directionString'TB' / 'BT' / 'LR' / 'RL' / 'H' / 'V''LR'The direction of layout.
- TB —— Root is on the top, layout from the top to the bottom
- BT —— Root is on the bottom, layout from the bottom to the top
imgimg
> (Left)TB. (Right)BT.
- LR —— Root is on the left, layout from the left to the right
- RL —— Root is on the right, layout from the right to the left
imgimg
> (Left)LR. (Right)RL.
- H —— Root is on the middle, layout in horizontal symmetry.
- V —— Root is on the middle, layout in vertical symmetry.
imgimg
> (Left)H. (Right)V.
nodeSepNumber500Node separation
rankSepNumber1000Level separation
radialBooleantruefalseWheter layout the graph in radial style. If radial is true, we recommend to set direction to 'LR' or 'RL':
img

indented

Description: Indented layout represents the hierarchy by indent between them. Each node will take a row/column. It is appropriate for file directory.
img

API: Indented API
Configuration:

NameTypeExample/OptionsDefaultDescription
directionString'LR' / 'RL' / 'H''LR'layout direction
'LR' —— Root is on the left, layout from the left to the right(left image below)
'RL' —— Root is on the right, layout from the right to the left(center image below)
'H' —— Root is on the middle, layout in horizontal symmetry(right image below)
indented1indented2indented3
indentNumber8020Colunm separation
getHeightFunction(d) => {
  // d is a node
  return 10;
}
undefinedThe height of each node
getWidthFunction(d) => {
  // d is a node
  return 20;
}
undefinedThe width of each node
getSideFunction(d) => {
  // d is a node
  return 'left';
}
undefinedThe callback function of node position(left or right of root node). Only affects the nodes which are connected to the root node directly. And the descendant nodes will be placed according to it

mindmap

Description: Mindmap arranged the nodes with same depth on the same level. Different from compactBox, it does not consider the size of nodes while doing layout.
img
API: Mindmap API
Configuration:

NameTypeExample/OptionsDefaultDescription
directionString'H' / 'V''H'layout direction
- H: Root is on the middle, layout in horizontal symmetry.
img
- V: Root is on the middle, layout in vertical symmetry.
img
getHeightFunction(d) => {
  // d is a node
  return 10;
}
undefinedThe height of each node
getWidthFunction(d) => {
  // d is a node
  return 20;
}
undefinedThe width of each node
getVGapFunction(d) => {
  // d is a node
  return 100;
}
18The vertical separation of nodes
getHGapFunction(d) => {
  // d is a node
  return 50;
}
18The horizontal separation of nodes
getSideStringFunction(d) => {
  // d is a node
  return 'left';
} / 'right'
The callback function of node position(left or right of root node). Only affects the nodes which are connected to the root node directly. And the descendant nodes will be placed according to it