logo

X6

  • Tutorials
  • API
  • Examples
  • Q&A
  • Change Log
  • XFlow
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quickstart
  • Basic
    • Graph
    • Nodes
    • Edges
    • Connection Pile
    • Interaction
    • Events
    • Data Serialization
  • Intermediate
    • Connection Points
    • Tools
    • Group
    • React Nodes
    • Vue Nodes
    • Angular Nodes
    • HTML Nodes
  • Plugin
    • Graphic Transformations
    • Snapline
    • Clipboard
    • Keyboard
    • History
    • Selection Box
    • Scroller
    • Dnd
    • Mini Map
    • Stencil
    • Export
  • Upgrade to Version 2.x
  • Developer Tools

Data Serialization

Previous
Events
Next
Connection Points

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...

This section mainly introduces knowledge related to canvas data serialization. By reading, you can learn about

  • How to import data
  • How to export data

Export

We can call the graph.toJSON() method to export the nodes and edges in the graph, returning an object with the structure { cells: [] }, where the cells array stores nodes and edges in rendering order.

The structure of the exported nodes is as follows:

{
id: string,
shape: string,
position: {
x: number,
y: number
},
size: {
width: number,
height: number
},
attrs: object,
zIndex: number,
}

The structure of the edges is as follows:

{
id: string,
shape: string,
source: object,
target: object,
attrs: object,
zIndex: number,
}

Import

Support for an array of node/edge metadata graph.fromJSON(cells: (Node.Metadata | Edge.Metadata)[]).

graph.fromJSON([
{
id: 'node1',
x: 40,
y: 40,
width: 100,
height: 40,
label: 'Hello',
shape: 'rect',
},
{
id: 'node2',
x: 40,
y: 40,
width: 100,
height: 40,
label: 'Hello',
shape: 'ellipse',
},
{
id: 'edge1',
source: 'node1',
target: 'node2',
shape: 'edge',
},
])

Alternatively, provide an object containing cells, nodes, and edges, rendered in the order of [...cells, ...nodes, ...edges].

graph.fromJSON({
nodes: [],
edges: [],
})

Typically, we render the data exported by graph.toJSON() using graph.fromJSON(...).

Tip

When the data does not provide a zIndex, the rendering is done according to the order of nodes/edges in the array, meaning that nodes/edges that appear earlier have a smaller zIndex, resulting in a lower layer in the canvas.