Overview of Nodes

11 min read

The built-in nodes in G6 include circle, rect, ellipse, diamond, triangle, star, image, modelRect, and donut(supported after v4.2.5).
img img

In this document, we will briefly introduce the built-in nodes in G6, the common property, and the way to configure the node type. To know more about each type of built-in nodes in G6, please refer to the corresponding documents in this directory.

Types of Default Nodes

The table below shows the built-in nodes and their special properties:

NameDescriptionDefault
circleCircle node:
- size is a number representing the diameter
- The circle is centered at the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- More properties are described in circle
img
rectRect node:
- size is an array, e.g. [100, 50]
- The rect in centered at the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- More properties are described in rect
img
ellipseEllipse node:
- size is an array, representing the lengths of major diameter and minor diameter
- The ellipse is centered at the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- More properties are described in ellipse
img
diamondDiamond node:
- size is an array, representing the width and height of the diamond
- The diamond is centered on the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- More properties are described in diamond
img
triangleTriangle node:
- size is an array, representing the length of the base and the height of the triangle
- The triangle is centered on the node position
- color takes effect on the stroke
- he label lays on the bottom of the node by default
- More properties are described in triangle
img
starStar node:
- size is a number, representing the size of the star
- The star is centered on the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- More properties are described in star
img
imageImage node:
- size is an array, representing the width and the height of the image
- The image is centered on the node position
- img The url of the image. It can be assigned in style as well
- color does not take effect
- The label lays on the bottom of the node by default
- More properties are described in image
img
modelRectCard node:
- size is an array, representing the width and the height of the card
- The modelRect is centered on the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- If description exists, it will lay below the label
- More properties are described in modelRect
img
 img
donutCircle node:
- size is a number representing the diameter
- The circle is centered at the node position
- color takes effect on the stroke
- The label is placed on the center of the circle by default
- Valid property donutAttrs should be assigned
- More properties are in Donut
img

Common Property

NameRequiredTypeRemark
idtrueStringThe ID of the node, MUST be a unique string
xfalseNumberx coordinate
yfalseNumbery coordinate
typefalseStringThe shape type of the node. It can be the type of built-in Node, or the custom Node. 'circle' by default
sizefalseNumber / ArrayThe size of the node
anchorPointsfalseArrayThe interactions of the node and related edges. It can be null. [0, 0] represents the anchor on the left top; [1, 1]represents the anchor ont he right bottom
stylefalseObjectThe node style
labelfalseStringThe label text of the node
labelCfgfalseObjectThe configurations of the label

style

style is an object to configure the filling color, stroke color, shadow, and so on. Here is the commonly used properties in style:

NameRequiredTypeRemark
fillfalseStringThe filling color
strokefalseStringThe stroke color
lineWidthfalseNumberThe line width of the stroke
lineDashfalseNumber[]The lineDash of the stroke
shadowColorfalseStringThe shadow color
shadowBlurfalseNumberThe blur of the shadow
shadowOffsetXfalseNumberThe x offset of the shadow
shadowOffsetYfalseNumberThe y offset of the shadow
opacityfalseNumberThe alpha or transparency of the node
fillOpacityfalseNumberThe filling alpha or transparency of the node
cursorfalseStringThe type of the mouse when hovering the node. The options are the same as cursor in CSS

Configure style globally when instantiating the Graph:

const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultNode: {
    // ... Other properties for nodes
    style: {
      fill: '#steelblue',
      stroke: '#eaff8f',
      lineWidth: 5,
      // ... Other style properties
    },
  },
});

label and labelCfg

label is a string which indicates the content of the label.
labelCfg is an object to configure the label. The commonly used configurations of labelCfg:

NameRequiredTypeRemark
positionfalseStringThe relative positions to the node. Options:  'center', 'top', 'left', 'right', 'bottom'. 'center' by default
offsetfalseNumberThe offset value of the label. When the position is 'bottom', the value is the top offset of the node; When the position is 'left', the value is the right offset of the node; it is similar with other position.
stylefalseObjectThe style property of the label

The commonly used configurations for the style in the above table are:

NameRequiredTypeRemark
fillfalseStringThe color of the label
strokefalseStringThe stroke color of the label
lineWidthfalseNumberThe line width of the label
opacityfalseNumberThe opacity of the label
fontFamilyfalseStringThe font family
fontSizefalseNumberThe font size of the label
... The label styles of node and edge are the same, summarized in Text Shape API

The following code shows how to configure label and labelCfg globally when instantiating a Graph:

const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultNode: {
    // ... Other properties for nodes
    label: 'node-label',
    labelCfg: {
      position: 'bottom',
      offset: 10,
      style: {
        fill: '#666',
      },
    },
  },
});

Configure Nodes

There are three methods to configure nodes: Configure nodes globally when instantiating a Graph; Configure nodes in their data; Configure nodes by graph.node(nodeFn). Their priorities are:

graph.node(nodeFn) > Configure in data > Configure globally

That means, if there are same configurations in different ways, the way with higher priority will take effect.

⚠️ Attention: Expect for id, and label which should be assigned to every single node data, the other configurations in The Common Property and in each node type (refer to doc of each node type) support to be assigned by the three ways.

Configure Globally When Instantiating Graph

Assign defaultNode to configure all the nodes globally:

const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultNode: {
    type: 'circle',
    // Other properties for all the nodes
  },
});

Configure in Data

To configure different nodes with different properties, you can write the properties into their data individually:

const data = {
  nodes: [
    {
      id: 'node0',
      size: 100,
      type: 'rect',
      // ...    // Other properties for this node
      style: {
        // ...  // Style properties for this node. Different styles for different types of nodes can be refered to the subdocuments
      },
    },
    {
      id: 'node1',
      size: [50, 100],
      type: 'ellipse',
      // ...    // Other properties for this node
      style: {
        // ...  // Style properties for this node. Different styles for different types of nodes can be refered to the subdocuments
      },
    },
    // ... // Other nodes
  ],
  edges: [
    // ... // edges
  ],
};

Configure with graph.node(nodeFn)

By this way, we can configure different nodes with different properties.


⚠️Attention:
  • graph.node(nodeFn) must be called before calling render(). It does not take effect otherwise;
  • It has the highest priority that will override the same properties configured by other ways;
  • Each node will be updated when adding or updating items. It will cost a lot when the amount of the data is large.
// const data = ...
// const graph = ...
graph.node((node) => {
  return {
    id: node.id,
    type: 'rect',
    style: {
      fill: 'blue',
    },
  };
});

graph.data(data);
graph.render();

Example

const data = {
  nodes: [
    {
      x: 100,
      y: 100,
      type: 'circle',
      label: 'circle',
    },
    {
      x: 200,
      y: 100,
      type: 'rect',
      label: 'rect',
    },
    {
      id: 'node-ellipse',
      x: 330,
      y: 100,
      type: 'ellipse',
      label: 'ellipse',
    },
    {
      id: 'node-diamond',
      x: 460,
      y: 100,
      type: 'diamond',
      label: 'diamond',
    },
    {
      id: 'node-triangle',
      x: 560,
      y: 100,
      //size: 80,
      type: 'triangle',
      label: 'triangle',
    },
    {
      id: 'node-star',
      x: 660,
      y: 100,
      //size: [60, 30],
      type: 'star',
      label: 'star',
    },
    {
      x: 760,
      y: 100,
      size: 50,
      type: 'image',
      img: 'https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB.svg',
      label: 'image',
    },
    {
      id: 'node-modelRect',
      x: 900,
      y: 100,
      type: 'modelRect',
      label: 'modelRect',
    },
  ],
};

const graph = new G6.Graph({
  container: 'mountNode',
  width: 1500,
  height: 300,
});
graph.data(data);
graph.render();

The result:
img

  • The label of the triangle and image node are layed on the bottom, and the others are layed on the center by default.

Adjust the Properties

By writing the properties into the data, we adjust the label position, color, and styles of the node with 'node-ellipse' as its id. Replace the following code to the code about 'node-ellipse''s data to obtain the result.

{
  id: 'node-ellipse',
  x: 330,
  y: 100,
  type: 'ellipse',
  size: [60, 30],
  label: 'ellipse',
  labelCfg: {
    position: 'bottom',
    offset: 5
  },
  style: {
    fill: '#fa8c16',
    stroke: '#000',
    lineWidth: 2
  }
}
img

Then, we add some description for the node with 'node-modelRect' as its id:

{
  id: 'node-modelRect',
  x: 900,
  y: 100,
  description: '描述文本xxxxxxxxxxx',
  type: 'modelRect',
  label: 'modelRect'
}
img
  • State —— Change the styles during the interaction process.