logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Getting Started
  • Section I - Defining the Scenario
  • Section II - Using the renderer
  • Section III - Adding some interaction
  • Diving Deeper
    • Scene Graph
    • 创造一个“太阳系”
    • 使用相机
    • 使用插件
    • 实现一个简单的动画
    • GPGPU 初体验
    • 进入 3D 世界
    • 使用 Box2D 物理引擎
    • 使用 matter.js 物理引擎
    • 使用 Yoga 布局引擎
    • Takeover D3's rendering
    • Takeover Observable Plot's rendering
    • Choose a renderer
    • Rendering on demand
  • Advanced Topics
    • 性能优化
    • 自定义图形
    • 理解事件传播路径
    • Using react-g
    • Exporting the contents of the canvas
    • Using lite version

Section I - Defining the Scenario

Previous
Getting Started
Next
Section II - Using the renderer

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

In this tutorial series, we will step-by-step implement a simple visualization scene that shows nodes and edges and gives them basic interaction capabilities such as dragging and picking.

In this section, we will learn how to describe a scene using a scene graph.

Our scene is very simple, it contains two nodes implemented with Circle, an edge connecting them implemented with Line, where the text on each node is implemented with Text.

2 nodes

DEMO in CodeSandbox

Create Node

First we import the base graph Circle from @antv/g, which our node uses to implement:

import { Circle } from '@antv/g';

Then we need to define a set of properties for the graph:

const node1 = new Circle({
style: {
r: 100,
fill: '#1890FF',
stroke: '#F04864',
lineWidth: 4,
},
});

We can create a second node in the same way.

Adding text to a node

We want to display descriptive text on the node, again we bring in the base graph Text from @antv/g:

import { Text } from '@antv/g';
const text1 = new Text({
style: {
text: 'Node1',
fontFamily: 'Avenir',
fontSize: 22,
fill: '#fff',
textAlign: 'center',
textBaseline: 'middle',
},
});

The text should be a child of the node, and in the scene graph, this parent-child relationship is constructed via appendChild:

node1.appendChild(text1);

We only need to set the position of the node, and all its children (text) will follow:

node1.setPosition(200, 200);

Create edge

We can import Line from @antv/g to connect the two endpoints:

import { Line } from '@antv/g';
const edge = new Line({
style: {
x1: 200,
y1: 200,
x2: 400,
y2: 200,
stroke: '#1890FF',
lineWidth: 2,
},
});

At this point our scene is defined and in the next section we will render the scene using the renderer.