Upgrade To 5.0
This document will guide you through the process of upgrading from G6 version 4.x to 5.x. If you are using version 3.x, please upgrade to version 4.x first.
Preparation Before Upgrade
- Please ensure that your current git branch is clean and there is no uncommitted code.
- Refer to the Installation document to install version
5.xand remove the dependencies for version4.x.
Start Upgrade
Data
The data format in the new version has changed as follows:
- All style attributes in
nodes,edges, andcombosneed to be placed withinstyle, and data attributes should be stored indata:
// 4.x
const data = {
nodes: [
{ id: 'node1', label: 'node1', size: 20 },
{ id: 'node2', label: 'node2', size: 20 },
],
edges: [{ source: 'node1', target: 'node2' }],
};
// 5.x
const data = {
nodes: [
// The label is a non-stylistic attribute, placed in the data, and can be accessed in the style mapping function
// The `size` is a stylistic attribute, placed within the `style`
{ id: 'node1', data: { label: 'node1' }, style: { size: 20 } },
{ id: 'node2', data: { label: 'node2' }, style: { size: 20 } },
],
edges: [{ source: 'node1', target: 'node2' }],
};
Since we have redesigned and implemented the elements, please refer to the corresponding documentation to modify the new element options:
- If you need to specify the element type in the data, you can use the
typeattribute:
{
nodes: [
// Specify the node type as rect
{ id: 'node1', type: 'rect' },
];
}
Options
Change fitView / fitCenter / fitViewPadding
- The
fitViewandfitCenteroptions have been merged intoautoFit. - To use
fitView, you can configure it asautoFit: 'view' - To use
fitCenter, you can configure it asautoFit: 'center' - You can also pass an object for full configuration:
autoFit: {
type: 'view',
options: {
// ...
}
}
- The
fitViewPaddinghas been changed topadding.
Removed linkCenter
In version 5.x, the edge connection mechanism will attempt to connect to nodes/Combos in the following order:
- Connect Port
- Outline
- Center
Removed groupByTypes
Removed autoPaint
Please manually call the render or draw method to perform rendering.
Changed modes
In version 5.x, interaction modes have been removed. You can switch the currently enabled behaviors by setting behaviors.
// 4.x
{
modes: {
default: ['drag-canvas', 'zoom-canvas'],
preview: ['drag-canvas'],
},
}
graph.setMode('preview');
// 5.x
{
behaviors: ['drag-canvas', 'zoom-canvas'],
}
graph.setBehaviors(['drag-canvas']);
Change defaultNode / defaultEdge / defaultCombo
The element styles have been moved to [element].style, for example, defaultNode has been changed to node.style:
// 4.x
{
defaultNode: {
size: 20,
fill: 'red',
}
}
// 5.x
{
node: {
style: {
size: 20,
fill: 'red',
}
}
}
Change nodeStateStyles / edgeStateStyles / comboStateStyle
Element state styles have been moved to [element].state, for example, nodeStateStyles has been changed to node.stateStyles:
// 4.x
{
nodeStateStyles: {
selected: {
fill: 'red',
}
}
}
// 5.x
{
node: {
state: {
selected: {
fill: 'red',
}
}
}
}
Change animate / animateCfg
- The
animateoptions has been changed toanimation animateandanimateCfghave been merged intoanimation
// 4.x
{
animate: true,
}
// 5.x
{
animation: true,
}
{
animation: {
duration: 500,
easing: 'easeLinear',
}
}
Change minZoom / maxZoom
- The
minZoomandmaxZoomoptions have been merged intozoomRange
// 4.x
{
minZoom: 0.5,
maxZoom: 2,
}
// 5.x
{
zoomRange: [0.5, 2],
}
Change renderer
G6 5.x supports multi-layer canvases and defaults to using the canvas renderer.
The renderer no longer supports the string type and has been changed to a callback function:
// 4.x
var options = {
renderer: 'svg',
};
// 5.x
import { Renderer } from '@antv/g-svg';
{
renderer: () => new Renderer(),
}
Removed enabledStack / maxStep
The built-in undo and redo functionality has been removed in version 5.x. For related capabilities, please use a plugin to implement.
API
Change data / save / read / changeData
Version 5.x offers a completely new data API. For details, see Data API.
- The
dataandchangeDatamethods from 4.x are replaced bysetDatain 5.x. - The
savemethod from 4.x is replaced bygetDatain 5.x. - The
readmethod from 4.x is replaced bysetData+renderin 5.x.
Change get / set
To access Graph options, please use getOptions or the getXxx API, such as getZoomRange, getBehaviors, etc. The set method is analogous.
Change getContainer
Direct API to obtain the container is not currently supported, but you can obtain it through graph.getCanvas().getContainer().
In most cases, you do not need to directly manipulate the container.
Removed getGroup
Change getMinZoom / getMaxZoom
Use getZoomRange to obtain the values.
Change setMinZoom / setMaxZoom
Use the setZoomRange method to set the values.
Change getWidth / getHeight
Use getSize to get the dimensions.
Change changeSize
Use setSize to set the dimensions.
Change zoom
Changed to zoomBy.
Change translate
Changed to translateBy.
Change moveTo
Changed to translateTo.
Change focusItem
Changed to focusElement.
Removed addItem / updateItem / removeItem
To add or remove elements, use the methods addData / updateData / removeData to manipulate data.
Removed refreshItem
Removed refreshPositions
Removed updateCombo
Removed updateCombos
Removed updateComboTree
Change node / edge / combo
Use the setNode / setEdge / setCombo methods as alternatives.
Change showItem / hideItem
Use the setElementVisibility method as an alternative.
Removed getNodes / getEdges / getCombos / getComboChildren / getNeighbors / find / findById / findAll / findAllByState
In version 5.x, direct retrieval of element instances is not supported.
- To obtain element data, use the methods
getData,getNodeData,getEdgeData,getComboData, which support searching by element ID. - To obtain child node data, use the
getChildrenDatamethod. - To obtain neighbor node data, use the
getNeighborNodesDatamethod. - To find element data based on state, use the
getElementDataByStatemethod.
Change collapseCombo / expandCombo
Use the collapseElement / expandElement methods as alternatives.
Removed collapseExpandCombo
Removed createCombo
Combos can now be added using the addData / addComboData methods.
Removed uncombo
Combos can now be removed using the removeData / removeComboData methods.
Change setItemState
Use the setElementState method as an alternative.
Removed clearItemStates
- To clear all states of a single element:
graph.setElementState(id, []) - To clear all states of multiple elements:
graph.setElementState({ id1: [], id2: [] })
Removed priorityState
When using setElementState, the state that appears later in the array has a higher priority.
Removed setMode
Use setBehaviors to set the current behaviors.
Removed setCurrentMode
Change layout
Does not support parameters. To configure the layout, please use setLayout.
Change updateLayout
Changed to setLayout.
Removed destroyLayout
Change addBehaviors / removeBehaviors
Replaced with setBehaviors.
Removed createHull / getHulls / removeHull / removeHulls
- For multiple
Hullinstances, you need to configure multiplehullplugins inplugins, such as:
{
plugins: ['hull', 'hull'],
};
- Operations to retrieve, update, and remove
Hullare implemented throughsetPlugins,updatePlugin.
Not yet available getNodeDegree
Not yet available getShortestPathMatrix
Not yet available getAdjMatrix
Removed pushStack / getUndoStack / getRedoStack / getStackData / clearStack
All undo and redo related APIs should be called after obtaining the corresponding plugin, for example:
// 'history' is the key configured for use with the plugin
const history = graph.getPluginInstance('history');
history.redo();
Removed positionsAnimate / stopAnimate / isAnimating
Animation-related information is now emitted through events:
- Animation start event:
beforeanimate - Animation end event:
afteranimate - To stop an animation:
graph.on('beforeanimate', (event) => {
event.animation.stop();
});
Change getPointByClient / getClientByPoint / getPointByCanvas / getCanvasByPoint / getGraphCenterPoint / getViewPortCenterPoint
G6 5.x uses a different coordinate system than 4.x. For details, see Coordinate.
Removed setTextWaterMarker / setImageWaterMarker
For watermark functionality, please refer to the Watermarkplugin.
Change toFullDataURL
Replaced with toDataURL, specify the parameter as: mode: 'overall'
graph.toDataURL({ mode: 'overall' });
Removed downloadFullImage / downloadImage
Only the capability to export as a DataURL is provided. If you need to download an image, please refer to the following example code:
async function downloadImage() {
const dataURL = await graph.toDataURL();
const [head, content] = dataURL.split(',');
const contentType = head.match(/:(.*?);/)![1];
const bstr = atob(content);
let length = bstr.length;
const u8arr = new Uint8Array(length);
while (length--) {
u8arr[length] = bstr.charCodeAt(length);
}
const blob = new Blob([u8arr], { type: contentType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'graph.png';
a.click();
}
Removed clear
Use setData + draw to clear data and the canvas.
Extension Registration
Unlike G6 4.x, G6 5.x uses a unified extension registration function (register). You can refer to the Extension Register to register G6 extensions.
The following G6 4.x registration functions have been deprecated:
- registerNode
- registerEdge
- registerCombo
- registerLayout
- registerBehavior
Events
Compared to G6 4.x, G6 5.x has the following differences in events:
- The
mouseandtouchevents have been removed and are unified under thepointerevent. - The naming convention for lifecycle events is usually in the format of:
before/after+object/property+action, for example:beforeelementcreateis triggered before an element is created. - The following events have been removed:
- afteractivaterelations
- afteradditem
- aftercreateedge
- aftergraphrefresh
- aftergraphrefreshposition
- afteritemrefresh
- aftermodechange
- afterremoveitem
- afterupdateitem
- beforeadditem
- beforecreateedge
- beforegraphrefresh
- beforegraphrefreshposition
- beforeitemrefresh
- beforemodechange
- beforeremoveitem
- beforeupdateitem
- dragnodeend
- nodeselectchange
- stackchange
- tooltipchange
- The following element change events have been removed, but you can still access them through
beforeelementupdateandafterelementupdate:- afteritemstatechange
- afteritemstatesclear
- afteritemvisibilitychange
- beforeitemstatechange
- beforeitemstatesclear
- beforeitemvisibilitychange
- The following events have been changed:
- The
graphstatechangeevent has been changed tobeforeelementstatechange/afterelementstatechange. - The
viewportchangeevent has been changed tobeforetransform/aftertransform.
- The
For a complete list of events, please refer to Event.