Random Layout
Overview
Random Layout is a layout method based on simple rules. Its core logic is to generate random coordinates for each node within a specified layout area (defined by the layout center, width, and height). The coordinates are completely random, and there is no node collision prevention.
Use Cases
The use cases for random layout are very limited. It is only recommended for the following scenarios:
-
Initial Data Display:
During early development, when debugging data loading logic or quickly verifying data structure, random layout can be used for preliminary validation.
For final business delivery, it is recommended to use layouts that better reflect business value, such as AntVDagreLayout, ForceLayout, or custom layouts.
Basic Usage
All other configurations use defaults (the layout width and height default to the entire canvas container).
const graph = new Graph({
// other configurations
layout: {
type: 'random',
},
});
Options
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| type | Layout type | random | - | ✓ |
| center | Layout center | [number, number] | [number, number, number] | [layout width / 2, layout height / 2] |
|
| height | Layout height | number | canvas height | |
| width | Layout width | number | canvas width |
Example
import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 600,
height: 300,
autoFit: 'view',
data: {
nodes: Array.from({ length: 50 }).map((_, i) => ({
id: `${i}`,
})),
},
node: {
style: {
labelText: (d) => d.id,
},
},
behaviors: ['drag-canvas', 'zoom-canvas'],
layout: {
type: 'random',
},
});
graph.render();
import { Graph } from '@antv/g6';
import GUI from 'lil-gui';
export function addPanel(renderPanel: (gui: GUI) => void) {
const gui = new GUI({ container: document.body });
gui.title('Control');
Object.assign(gui.domElement.style, { position: 'absolute', top: '0', right: '0', zIndex: '10' });
renderPanel(gui);
addEventListener('pagehide', () => gui.destroy(), { once: true });
}
export async function createGraph(
options: ConstructorParameters<typeof Graph>[0],
size: { width?: number; height?: number } = {},
renderPanel?: (gui: GUI, graph: Graph) => void,
) {
const container = document.createElement('div');
Object.assign(container.style, {
width: '100%',
maxWidth: `${size.width || 600}px`,
height: `${size.height || 400}px`,
});
document.getElementById('container')!.append(container);
const graph = new Graph({ ...size, ...options, width: container.clientWidth, container, autoResize: true });
addEventListener('pagehide', () => graph.destroy(), { once: true });
await graph.render();
if (renderPanel) addPanel((gui) => renderPanel(gui, graph));
return container;
}