@antv/layout
graph layout algorithm
Last updated 3 years ago by kopiluwaky .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @antv/layout 
SYNC missed versions from official npm registry.

@antv/layout

Collection of basic layout algorithms to be used with @antv/graphlib.

Online Demo

Usage

NPM

Install @antv/layout first.

# npm
$ npm install @antv/layout --save

# yarn
$ yarn add @antv/layout

Choose a layout algorithm from @antv/layout then.

import { Graph } from "@antv/graphlib";
import { CircularLayout } from "@antv/layout";

const graph = new Graph({ nodes: [], edges: [] });

const circularLayout = new CircularLayout({ radius: 10 });

(async () => {
  // 1. Return positions of nodes & edges.
  const positions = await circularLayout.execute(graph);

  // 2. To directly assign the positions to the nodes:
  await circularLayout.assign(graph);
})();

UMD

Import scripts in UMD version of @antv/graphlib and @antv/layout.

<script
  src="https://unpkg.com/@antv/graphlib"
  type="application/javascript"
></script>
<script
  src="https://unpkg.com/@antv/layout"
  type="application/javascript"
></script>

Use layouts under Layout namespace.

const { Graph } = window.GraphLib;
const { CircularLayout } = window.Layout;

Documentation

We provide the following layouts:

You can choose the following two ways to use, the difference is whether to change the node & edge data in the graph model:

  • Calling execute to return positions of nodes & edges.
  • Or calling assign to directly assign the positions to the nodes in graph model.

The first argument is the target graph whose type is Graph.

Circular

Circular layout arranges the node on a circle. By tuning the configurations, user can adjust the node ordering method, division number, radial layout, and so on. G6 implements it according to the paper: A framework and algorithms for circular drawings of graphs.

circular layout circular layout circular layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • radius number The radius of the circle. If the raidus exists, startRadius and endRadius do not take effect.
  • startRadius number The start radius of spiral layout. The default value is null.
  • endRadius number The end radius of spiral layout. The default value is null.
  • clockwise boolean Whether to layout clockwisely. The default value is true.
  • divisions number The division number of the nodes on the circle. Takes effect when endRadius - startRadius !== 0. The default value is 1.
  • ordering null | 'topology' | 'degree' The ordering method for nodes. null by default, which means the nodes are arranged in data order. 'topology' means in topology order; 'degree' means in degree order.
  • angleRatio number How many 2*PIs Between the first node and the last node. The default value is 1.

Random

random layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • width number The width of the graph.
  • height number The height of the graph.

Grid

Grid orders the nodes according to the configurations and arranged them onto grid.

grid layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • width number The width of the graph.
  • height number The height of the graph.
  • rows number The row number of the grid. If rows and cols is not provided, the algorithm will calculate it according to the space and node numbers automatically.
  • cols number The col number of the grid.
  • condense boolean Wheter to utilize the minimum space of the canvas. false means utilizing the full space, true means utilizing the minimum space.
  • preventOverlap boolean Whether to prevent node overlappings. To activate preventing node overlappings, nodeSize is required, which is used for collide detection. The size in the node data will take effect if nodeSize is not assigned. If the size in node data does not exist either, nodeSize is assigned to 30 by default.
  • nodeSize number The diameter of the node. It is used for preventing node overlappings.
  • preventOverlapPadding boolean The minimum padding between nodes to prevent node overlappings. Takes effect when preventOverlap is true.
  • sortBy string The ordering method for nodes. Smaller the index in the ordered array, more center the node will be placed. If sortBy is undefined, the algorithm order the nodes according to their degrees.

MDS

MDS (Multidimensional scaling) is used for project high dimensional data onto low dimensional space.

MDS layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • linkDistance number The edge length. The default value is 50.

Concentric

Concentric arranges the nodes on several concentric circles. Each node is sorted by degree by default. The greater the degree of a node, the closer it is to the center point.

concentric layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • width number The width of the graph.
  • height number The height of the graph.
  • maxLevelDiff number The sum of concentric values in each level. If it is undefined, maxValue / 4 will take place, where maxValue is the max value of ordering properties. For example, if sortBy is 'degree', maxValue is the max degree value of all the nodes.
  • nodeSize number The diameter of the node. It is used for preventing node overlappings. The default value is 30.
  • minNodeSpacing number The minimum separation between adjacent nodes. The default value is 10.
  • clockwise boolean Place the nodes in clockwise or not. The default value is true.
  • startAngle number Where nodes start in radians. The default value is (3 / 2) * Math.PI.
  • equidistant boolean Whether levels have an equal radial distance between them, may cause bounding box overflow. The default value is false.

Radial

Radial layout arranges the nodes to concentrics centered at a focus node according to their shortest path length to the focus node. We implements it according to the paper: More Flexible Radial Layout.

radial layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • width number The width of the graph.
  • height number The height of the graph.
  • linkDistance number The edge length. The default value is 50.
  • focusNode string The focus node of the radial layout. The first node of the data is the default value. It can be the id of a node or the node item.
  • unitRadius number The separation between adjacent circles. If not provided, the layout will fill the canvas automatically.
  • preventOverlap boolean Whether to prevent node overlappings. To activate preventing node overlappings, nodeSize is required, which is used for collide detection. The size in the node data will take effect if nodeSize is not assigned. If the size in node data does not exist either, nodeSize is assigned to 30 by default.
  • nodeSize number The diameter of the node. It is used for preventing node overlappings.
  • preventOverlapPadding boolean The minimum padding between nodes to prevent node overlappings. Takes effect when preventOverlap is true.
  • nodeSpacing number It is the minimum distance between nodes to prevent node overlappings.
  • strictRadial boolean Whether to layout the graph as strict radial, which means the nodes will be arranged on each circle strictly. Takes effect only when preventOverlap is true.
    • true The overlapped nodes can be arranged around their circle with small offsets.
    • false The overlapped nodes are arranged along their circles strictly. But for the situation that there are too many nodes on a circle to be arranged, the overlappings might not be eliminated completely.
  • sortBy string Sort the nodes of the same level. undefined by default, which means place the nodes with connections as close as possible; 'data' means place the node according to the ordering in data, the closer the nodes in data ordering, the closer the nodes will be placed. sortBy also can be assigned to any name of property in nodes data, such as 'cluster', 'name' and so on (make sure the property exists in the data)
  • sortStrength number The strength to sort the nodes in the same circle. Larger number means place the nodes with smaller distance of sortBy more closely. Takes effect only when sortBy is not undefined.

Fruchterman

Fruchterman is a kind of force-directed layout. The implementation is according to the paper Graph Drawing by Force-directed Placement.

fruchterman layout

Online Demo

LayoutOptions:

  • dimensions number The dimensions of node's coordinates. The default value is 2.
  • center [number, number] | [number, number, number] The center of the graph. e.g. [0, 0].
  • width number The width of the graph.
  • height number The height of the graph.
  • maxIteration number
  • gravity number The gravity, which will affect the compactness of the layout. The default value is 10.
  • speed number The moving speed of each iteraction. Large value of the speed might lead to violent swing.
  • clustering boolean We can also layout according to nodeClusterBy field in data when enable clustering.
  • clusterGravity The gravity of each cluster, which will affect the compactness of each cluster. The default value is 10.

D3Force

Force is the classical force-dicrected layout algorithm, which corresponds to force-directed layout in d3.js.

d3force layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0]
  • linkDistance number The edge length. The default length is 50.
  • nodeStrength number The strength of node force. Positive value means attractive force, negative value means repulsive force.
  • edgeStrength number The strength of edge force, ranges from 0 to 1. Calculated according to the degree of nodes by default.
  • preventOverlap boolean
  • collideStrength number The strength of force for preventing node overlappings. The range is [0, 1].
  • nodeSize number The diameter of the node. It is used for preventing node overlappings. If nodeSize is not assigned, the size property in node data will take effect. If the size in node data does not exist either, nodeSize is assigned to 10 by default.
  • nodeSpacing number The minimum space between two nodes when preventOverlap is true. The default value is 0.

Force

Force2 implements the force-directed layout algorithm. It comes from graphin-force, supports assign different masses and center gravities for different nodes freedomly. Comparing to graphin-force, the performance is improved greatly.

force layout

Online Demo

LayoutOptions:

  • dimensions number The dimensions of node's coordinates. The default value is 2.
  • center [number, number] | [number, number, number] The center of the graph. e.g. [0, 0].
  • linkDistance number The edge length. The default length is 200.
  • nodeStrength number The strength of node force. Positive value means repulsive force, negative value means attractive force (it is different from 'force'). The default value is 1000.
  • edgeStrength number The strength of edge force. Calculated according to the degree of nodes by default. The default value is 200.
  • preventOverlap boolean
  • nodeSize number The diameter of the node. It is used for preventing node overlappings. If nodeSize is not assigned, the size property in node data will take effect. If the size in node data does not exist either, nodeSize is assigned to 10 by default.
  • nodeSpacing number The minimum space between two nodes when preventOverlap is true. The default value is 0.
  • minMovement number When the average/minimum/maximum (according to distanceThresholdMode) movement of nodes in one iteration is smaller than minMovement, terminate the layout.
  • distanceThresholdMode 'mean' | 'max' | 'min' The condition to judge with minMovement, 'mean' means the layout stops while the nodes' average movement is smaller than minMovement, 'max' / 'min' means the layout stops while the nodes' maximum/minimum movement is smaller than minMovement. 'mean' by default
  • damping number Range [0, 1], affect the speed of decreasing node moving speed. Large the number, slower the decreasing. The default value is 0.9.
  • interval number Controls the speed of the nodes' movement in each iteration. The default value is 0.02.
  • maxSpeed number The max speed in each iteration. The default value is 1000.
  • force number Coefficient for the repulsive force. Larger the number, larger the repulsive force.
  • coulombDisScale number A parameter for repulsive force between nodes. Large the number, larger the repulsion. The default value is 0.005.
  • gravity number The gravity strength to the center for all the nodes. Larger the number, more compact the nodes. The default value is 10.

ForceAtlas2

FA2 is a kind of force directed layout, which performs better on the convergence and compactness.

forceAtlas2 layout

Online Demo

LayoutOptions:

  • center [number, number] The center of the graph. e.g. [0, 0].
  • kr number Repulsive parameter, smaller the kr, more compact the graph. The default value is 5.
  • kg number The parameter for the gravity. Larger kg, the graph will be more compact to the center. The default value is 5.
  • ks number The moving speed of the nodes during iterations. The default value is 0.1.
  • tao number The threshold of the swinging. The default value is 0.1.
  • preventOverlap boolean
  • dissuadeHubs boolean Wheather to enable hub mode. If it is true, the nodes with larger in-degree will be placed on the center in higher priority.
  • barnesHut boolean Whether to enable the barnes hut speedup, which is the quad-tree optimization. Due to the computation for quad-tree re-build in each iteration, we sugguest to enable it in large graph. It is undefined by deafult, when the number of nodes is larger than 250, it will be activated automatically. If it is set to be false, it will not be activated anyway.

Supervisor

Sometimes we need to execute CPU-intensive algorithms in WebWorker so as not to block the main thread. For this we encapsulate a class called Supervisor. It accepts a graph model and layout algorithm as parameters, completes the creation and communication of WebWorker internally, and notifies the main thread through events after the calculation is completed.

const graph = new Graph();
const layout = new CircularLayout();

const supervisor = new Supervisor(graph, layout);
const positions = await supervisor.execute();

Online Demo

  • execute Call execute on the selected layout algorithm. This is an async method.
  • stop Call stop on the selected layout algorithm.
  • kill Terminate the WebWorker.

Use WebWorker in UMD and ESM format

We use the following syntax in ESM, which is also available in native ECMAScript modules in the browser:

new Worker(new URL('./worker.js', import.meta.url));

Here are some demos with different bundlers:

For more discussion see: https://github.com/antvis/layout/issues/121#issuecomment-1558408701.

License

The scripts and documentation in this project are released under the MIT License.

Current Tags

  • 1.2.13                                ...           alpha (a year ago)
  • 1.2.14-beta.9                                ...           beta (2 months ago)
  • 1.2.14-beta.8                                ...           latest (3 months ago)

161 Versions

  • 1.2.14-beta.9                                ...           2 months ago
  • 1.2.14-beta.8                                ...           3 months ago
  • 1.2.14-beta.7                                ...           4 months ago
  • 1.2.14-beta.6                                ...           4 months ago
  • 1.2.14-beta.5                                ...           5 months ago
  • 1.2.14-beta.4                                ...           7 months ago
  • 1.2.14-beta.3                                ...           8 months ago
  • 1.2.14-beta.2                                ...           8 months ago
  • 1.2.14-beta.1                                ...           9 months ago
  • 1.2.14-beta.0                                ...           9 months ago
  • 0.3.25                                ...           a year ago
  • 1.2.13                                ...           a year ago
  • 1.2.12                                ...           a year ago
  • 1.2.11-beta.0                                ...           a year ago
  • 0.3.24                                ...           a year ago
  • 1.2.11                                ...           a year ago
  • 1.2.10                                ...           a year ago
  • 1.2.9                                ...           a year ago
  • 1.2.8                                ...           a year ago
  • 1.2.7                                ...           a year ago
  • 1.2.5                                ...           a year ago
  • 0.3.23                                ...           2 years ago
  • 1.2.4                                ...           2 years ago
  • 1.2.3                                ...           2 years ago
  • 1.2.2                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 0.3.22                                ...           2 years ago
  • 0.3.22-beta.8                                ...           2 years ago
  • 0.3.22-beta.7                                ...           2 years ago
  • 0.3.22-beta.6                                ...           2 years ago
  • 0.3.22-beta.5                                ...           2 years ago
  • 0.3.22-beta.4                                ...           2 years ago
  • 0.3.22-beta.3                                ...           2 years ago
  • 0.3.22-beta.2                                ...           2 years ago
  • 0.3.22-beta.1                                ...           2 years ago
  • 0.3.22-beta.0                                ...           2 years ago
  • 0.3.21                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.3.20                                ...           2 years ago
  • 0.3.19                                ...           2 years ago
  • 1.0.0-alpha.19                                ...           2 years ago
  • 0.3.18                                ...           2 years ago
  • 1.0.0-alpha.18                                ...           2 years ago
  • 0.3.17                                ...           2 years ago
  • 0.3.17-beta.2                                ...           2 years ago
  • 0.3.17-beta.1                                ...           2 years ago
  • 0.3.17-beta.0                                ...           2 years ago
  • 1.0.0-alpha.17                                ...           2 years ago
  • 0.3.16                                ...           2 years ago
  • 0.3.15                                ...           2 years ago
  • 1.0.0-alpha.16                                ...           2 years ago
  • 0.3.15-beta.0                                ...           2 years ago
  • 0.3.14                                ...           2 years ago
  • 1.0.0-alpha.15                                ...           2 years ago
  • 1.0.0-alpha.14                                ...           2 years ago
  • 1.0.0-alpha.13                                ...           2 years ago
  • 1.0.0-alpha.12                                ...           2 years ago
  • 1.0.0-alpha.11                                ...           2 years ago
  • 1.0.0-alpha.10                                ...           2 years ago
  • 1.0.0-alpha.9                                ...           2 years ago
  • 1.0.0-alpha.8                                ...           2 years ago
  • 1.0.0-alpha.7                                ...           2 years ago
  • 1.0.0-alpha.6                                ...           2 years ago
  • 1.0.0-alpha.5                                ...           2 years ago
  • 1.0.0-alpha.4                                ...           2 years ago
  • 1.0.0-alpha.3                                ...           2 years ago
  • 1.0.0-alpha.2                                ...           2 years ago
  • 0.3.13                                ...           2 years ago
  • 1.0.0-alpha.1 [deprecated]           ...           2 years ago
  • 1.0.0-alpha.0 [deprecated]           ...           2 years ago
  • 0.3.12                                ...           2 years ago
  • 0.3.11                                ...           2 years ago
  • 0.3.10                                ...           2 years ago
  • 0.3.9                                ...           2 years ago
  • 0.3.8                                ...           2 years ago
  • 0.3.7                                ...           2 years ago
  • 0.3.6                                ...           2 years ago
  • 0.3.5                                ...           2 years ago
  • 0.3.4                                ...           2 years ago
  • 0.3.3                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.3.0-beta.5                                ...           2 years ago
  • 0.3.0-beta.4                                ...           2 years ago
  • 0.3.0-beta.3                                ...           2 years ago
  • 0.3.0-beta.2                                ...           2 years ago
  • 0.3.0-beta.1                                ...           2 years ago
  • 0.3.0-beta                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           3 years ago
  • 0.2.3                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.2.0-beta.5                                ...           3 years ago
  • 0.2.0-beta.4                                ...           3 years ago
  • 0.2.0-beta.3                                ...           3 years ago
  • 0.2.0-beta.2                                ...           3 years ago
  • 0.2.0-beta.1                                ...           3 years ago
  • 0.2.0-beta.0                                ...           3 years ago
  • 0.1.31                                ...           3 years ago
  • 0.1.30                                ...           3 years ago
  • 0.1.30-beta.1                                ...           3 years ago
  • 0.1.30-beta.0                                ...           3 years ago
  • 0.1.29                                ...           3 years ago
  • 0.1.28                                ...           3 years ago
  • 0.1.27                                ...           3 years ago
  • 0.1.26                                ...           3 years ago
  • 0.1.25                                ...           3 years ago
  • 0.1.24                                ...           3 years ago
  • 0.1.23 [deprecated]           ...           3 years ago
  • 0.1.22                                ...           3 years ago
  • 0.1.21                                ...           3 years ago
  • 0.1.20                                ...           3 years ago
  • 0.1.19                                ...           3 years ago
  • 0.1.19-beta.5                                ...           3 years ago
  • 0.1.19-beta.4                                ...           3 years ago
  • 0.1.19-beta.3                                ...           3 years ago
  • 0.1.19-beta.2                                ...           3 years ago
  • 0.1.19-beta.1                                ...           3 years ago
  • 0.1.19-beta.0                                ...           3 years ago
  • 0.1.19-beta                                ...           3 years ago
  • 0.1.18                                ...           3 years ago
  • 0.1.17                                ...           3 years ago
  • 0.1.16                                ...           3 years ago
  • 0.1.15                                ...           4 years ago
  • 0.1.14                                ...           4 years ago
  • 0.1.13                                ...           4 years ago
  • 0.1.12                                ...           4 years ago
  • 0.1.11                                ...           4 years ago
  • 0.1.10                                ...           4 years ago
  • 0.1.9                                ...           4 years ago
  • 0.1.8                                ...           4 years ago
  • 0.1.7-beta.3                                ...           4 years ago
  • 0.1.7-beta.2                                ...           4 years ago
  • 0.1.7-beta.1                                ...           4 years ago
  • 0.1.6                                ...           4 years ago
  • 0.1.5                                ...           4 years ago
  • 0.1.4                                ...           4 years ago
  • 0.1.3                                ...           4 years ago
  • 0.1.2                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
  • 0.0.16                                ...           4 years ago
  • 0.0.15                                ...           4 years ago
  • 0.0.14                                ...           4 years ago
  • 0.0.13                                ...           4 years ago
  • 0.0.12                                ...           4 years ago
  • 0.0.11                                ...           4 years ago
  • 0.0.10                                ...           4 years ago
  • 0.0.9                                ...           4 years ago
  • 0.0.8                                ...           4 years ago
  • 0.0.7                                ...           4 years ago
  • 0.0.6                                ...           4 years ago
  • 0.0.5                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
Downloads
Total 2
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)

© 2010 - cnpmjs.org x YWFE | Home | YWFE