Usage guidesLayout

Layout overview

Choose a layout and compare how it arranges the same graph

What is a layout?

A layout determines where nodes go. A workflow can run left to right, while a force-directed layout can bring connected nodes together. Omit layout to use the nodes’ existing x and y coordinates.

Choose a layout

TypeDescriptionExample configuration
gridArrange nodes in rows and columns.{ type: 'grid', cols: 3 }
circularArrange nodes along a circle.{ type: 'circular', radius: 100 }
dagreArrange steps or dependencies in layers.{ type: 'dagre', rankdir: 'LR', ranksep: 80 }
forceBring linked nodes closer while spacing nodes apart.{ type: 'force', linkDistance: 100 }
d3-forceArrange a network with D3 force simulation.{ type: 'd3-force', linkDistance: 100 }
force-atlas2Use force simulation to reveal network clusters.{ type: 'force-atlas2', kr: 8, kg: 2 }
fruchtermanSpread nodes using attraction and repulsion.{ type: 'fruchterman', gravity: 12 }
radialOptional. Arrange nodes in rings by graph distance from a focal node.{ type: 'radial', focusNode: '0', unitRadius: 68 }
concentricArrange nodes in concentric rings by degree.{ type: 'concentric', sortBy: 'degree' }
mdsOptional. Approximate graph distances in two dimensions.{ type: 'mds', linkDistance: 80 }
indentedOptional. Arrange a tree as an indented list.{ type: 'indented', indent: 80, rowHeight: 40 }
mindmapOptional. Arrange a tree as mind map branches.{ type: 'mindmap', levelGap: 120, nodeGap: 30 }
fishboneOptional. Arrange categories and items along a fishbone spine.{ type: 'fishbone', categoryGap: 180 }
randomOptional. Scatter nodes randomly within an area.{ type: 'random', width: 800, height: 600, center: [400, 300] }

Pass these configuration objects to layout. See Common layout options for more parameters and previews.

Optional layouts

Indented, Mindmap, Fishbone, Radial, MDS, and Random require registration. With npm, import the layout from its own subpath, register it, and set layout.type.

import { register } from '@antv/g6-lite';
import radial from '@antv/g6-lite/layouts/radial';

register('layout', 'radial', radial);

const layout = { type: 'radial', focusNode: '0', unitRadius: 68 };

When using UMD from a CDN, load the same version of dist/g6-lite-layouts.min.js after the main script. The layout extensions are available on G6LiteLayouts.

<script src="https://cdn.jsdelivr.net/npm/@antv/g6-lite@0.1.0/dist/g6-lite.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@antv/g6-lite@0.1.0/dist/g6-lite-layouts.min.js"></script>
<script>
  G6Lite.register('layout', 'radial', G6LiteLayouts.radial);
</script>

Registered layouts run on the main thread.

Configuration and example

Set the algorithm with layout.type. This example uses Dagre to arrange three steps from left to right.

import { Graph } from '@antv/g6-lite';

const graph = new Graph({
  container: '#container',
  data: {
    nodes: [{ id: 'Submit' }, { id: 'Review' }, { id: 'Publish' }],
    edges: [
      { source: 'Submit', target: 'Review' },
      { source: 'Review', target: 'Publish' },
    ],
  },
  layout: { type: 'dagre', rankdir: 'LR', ranksep: 80 },
  node: { style: { label: (node) => node.id } },
});

await graph.render();

Use Layout below to compare Grid, Circular, and Dagre on the same data. See Network layouts for force-directed layouts.

Loading example…
Use Layout to compare grid, circular, and layered arrangements of the same graph.
const layouts: Record<string, LayoutConfig> = {
  grid: { type: 'grid', rows: 2, cols: 3, width: 360, height: 160 },
  circular: { type: 'circular', radius: 100 },
  dagre: { type: 'dagre', rankdir: 'LR', nodesep: 64, ranksep: 96, nodeSize: 28 },
};

See Common layout options for parameters and runtime changes, or Custom layouts to calculate your own coordinates.