Usage guidesElementsNodes

Node overview

Learn about built-in and custom nodes, node data, styling, and states in G6Lite

What is a node?

A node represents an object or concept in a graph, such as a user, device, or service. Each node has a unique id. Edges refer to node IDs through source and target to represent relationships.

Node types

Built-in nodes

Set node.type to use a built-in G6Lite node. The previews show filled and outlined circles, along with square and rounded rectangle corners.

Node typeType nameUse and dimensions
CirclecircleDefault type for markers such as users or devices. Set the diameter with size.
RectanglerectUseful for service names or cards. Set the shape with width, height, and borderRadius.

See Node body styles for size and corner radius defaults.

Custom nodes

Use a NodeExtension with a circle or rect base to package a reusable node. For example, fill a service node according to its health and use drawOverlay() to add an alert marker. Call register('node', name, extension), then assign the registered name to node.type. See Custom nodes.

Data structure

List nodes in data.nodes. Each entry is a GraphNode.

FieldTypeRequiredMeaning
idstringYesUnique ID for connecting, querying, and updating the node
namestringNoName available to label callbacks
dataNodeDataNoApplication data available to style functions
x, ynumberNoPosition in graph coordinates when no layout is configured
fx, fynumberNoCoordinates fixed after layout
comboIdstringNoID of a group declared in data.combos
statesNodeState[]NoInitial states, such as ['selected']
const data = {
  nodes: [
    { id: 'api', name: 'API', x: 80, y: 100, data: { health: 'ok' } },
    { id: 'db', name: 'Database', x: 220, y: 100, data: { health: 'warning' } },
  ],
  edges: [{ source: 'api', target: 'db' }],
};

Configuration methods

Configure all nodes at creation

Pass node to new Graph(). A constant style value applies to every node, such as the diameter of 32 below.

Map styles from node data

Assign a function to a field in node.style. This example uses each node's data.health for its fill and name for its label. The #container element needs to exist and have a width and height.

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

const graph = new Graph({
  container: '#container',
  data,
  node: {
    type: 'circle',
    style: {
      size: 32,
      fill: (node) => node.data?.health === 'warning' ? '#F59E0B' : '#1783FF',
      label: (node) => node.name ?? node.id,
    },
  },
});

await graph.render();

Style fields resolve in this order: node.style, the extension's getStyle(), the extension's default styles, then the theme or built-in fallbacks. See Common node options for the full list of fields and how to configure them.

Update nodes after rendering

Use graph.updateNodeData() to change one node's application data. Use graph.updateOptions() to change the graph's node configuration. Style objects merge by field. The example below gives the database node its healthy color and changes both circles to a diameter of 40.

graph.updateNodeData('db', { health: 'ok' });
graph.updateOptions({ node: { style: { size: 40 } } });

Node states

Set initial states in the node's states array. After rendering, call graph.setItemState('db', 'selected', true) to select the node. Pass false as the last argument to clear the selection. Set node.stateStyles.selected to customize the selection stroke, fill, or halo. See Element states and node state styles.