The official version is coming soon. If these features are what you have been waiting for, you can now try the G6 5.0 Beta version! If you encounter any upgrade issues, please leave us a message on GitHub.
To support these new capabilities, there are significant Breaking Changes in G6 5.0 compared to v4, which may require some effort to upgrade. We hope that the benefits brought by the new capabilities outweigh the upgrade costs.
In v5, we standardize the graphics of all nodes/edges/combos. Each type of element has several standardized graphic names. This standard should also be followed for custom elements. If there are additional graphics, they should be placed in otherShapes.
- node: keyShape (main graphic), labelShape (text graphic), haloShape (background halo that appears in certain states), labelBackgroundShape (text background graphic), iconShape (icon graphic in the center of the node), badgeShapes (badge graphics around the node), anchorShapes (circle graphics representing anchor points):
Sometimes, we need to return different style configurations based on different data. In such cases, function configuration is more flexible compared to the field+formatter method:
```typescript
const graph = new Graph({
node: (model) => {
const { id, data } = model;
const { size, showLabel } = data;
// Note that the returned data structure is the complete node data type
return {
id,
data: {
...data, // Make sure to return the original data in the original data, otherwise these data will be lost
keyShape: {
r: Math.max(size[0], size[1]) / 2,
// ...
},
labelShape: showLabel
? {
// Decide whether to display text based on a business field
In v4, to set animations for nodes, you had to use custom nodes and configure them with the animation API of the graphics. It was also difficult to control when the animation starts and ends. In v5, animations can be defined using JSON spec. You can specify the animates field in the node / edge / combo field of the graph configuration as mentioned above:
```typescript
const graph = new Graph({
node: {
animates: {
buildIn: [...],
buildOut: [...],
update: [...],
show: [...],
hide: [...],
}
}
});
```
Or in the functional mapping way of `node` / `edge` / `combo`:
```typescript
const graph = new Graph({
node: model => {
const { id, data } = model
return {
id,
data: {
...data,
// ... other style configurations
animates: {
buildIn: [...],
buildOut: [...],
update: [...],
show: [...],
hide: [...],
}
}
}
}
});
```
We have standardized five animation scenarios that occur at different times for each graphic: buildIn, buildOut, update (data/state update), show (appearance, compared to hide), hide. For each scenario, animations can be specified for different graphics, different fields, and the animation configuration and execution order can be specified. For example, the following expresses the animation of various graphics during different types of updates:
```typescript
update: [
{
// The entire node (shapeId: 'group') animates when x and y change
fields: ['x', 'y'],
shapeId: 'group',
duration: 500,
},
{
// The opacity of haloShape animates with a duration of 500 when selected and active states change
fields: ['opacity'],
shapeId: 'haloShape',
states: ['selected', 'active'],
duration: 500,
},
// When fill and r of keyShape change at the same time, they are animated in the order specified by order, which can achieve a sequential animation effect
Information layering can reduce visual distractions for complex graphs and display detailed information after zooming in. You can specify the lodLevels field in the `node` / `edge` / `combo` fields of the graph configuration introduced above, as shown in the code snippet below. The `levels` define the zoom levels at which information layering responds, and the `animateCfg` configuration specifies the animation method for the graph changes caused by information layering. Then, you need to configure the `lod` field in different graphic style configurations to specify at which level the graphic should be displayed in `levels`.
lod: isImportant ? -1 : 2, // You can determine in which level to display the label based on the business attribute. For example, if it is an important node, display the text in all levels; otherwise, display it after zooming in to a certain extent
// palette: { a: '#f00', b: '#0f0', c: '#00f' }, // You can specify colors for special field values
// getStyleSets: (palette) => {
// // More flexible configuration, return different styles for different states
// const styleSetsMap = {};
// Object.keys(palette).forEach((dataType) => {
// const color = palette[dataType];
// styleSetsMap[dataType] = {
// default: {
// keyShape: { fill: color },
// labelShape: { fill: color },
// },
// state1: {
// keyShape: { fill: '#000' },
// },
// state2: {
// keyShape: { stroke: '#f00' },
// },
// state3: {
// keyShape: { fill: '#ff0' },
// },
// };
// });
// return styleSetsMap;
// },
},
edge: {
dataTypeField: 'cluster', // Specify the field for mapping edge color
// ... Other configurations
},
},
},
});
```
## 4️⃣. Data Processing
The format of business data varies and may not conform to the data format of G6. Sometimes, it may be necessary to calculate some fields for the data in advance, such as the degree of nodes. In this case, the data processing module of G6 v5 can be used. It is one of the eight extensions of G6 v5 and is executed before user data flows into the Graph. Multiple data processing modules can be configured and they will be executed linearly. The configuration method is as follows:
v5 introduces new features related to tree graphs:
- The layout is compatible with Graph. Graph can specify the root node and use the minimum spanning tree to establish a tree structure before using the tree layout algorithm.
- The interaction is compatible with Graph. Graph can also expand and collapse "subtrees", which are downstream nodes without backtracking edges.
- Supports backtrack edges and loops.
- Supports forests (multiple trees).
If you need to use TreeGraphData, you just need to provide a data type tag when configuring the Graph:
```typescript
const graph = new Graph({
// ... other configurations
data: {
type: 'treeData', // type can be 'graphData', 'treeData', 'fetch', where 'fetch' will be supported in the official version
value: data, // when type is 'treeData', value can be TreeGraphData or TreeGraphData[] to support the drawing of forests
},
});
```
The `data` field can provide data of type GraphData, so G6 will treat it as a normal graph and generate a tree graph structure when necessary (such as using tree layout or interaction). It is also possible to specify the type as 'treeData' and pass TreeGraphData type data to the value, in which case G6 will store the tree graph structure and convert it to normal graph data for storage.
G6 v5 provides the ability to render and interact with 3D large graphs. To use this feature, you need to configure `renderer: 'webgl-3d'` on the Graph and configure the corresponding 3D node type (currently only supports sphere-node) and 3D interactions:
```typescript
import { Graph, Extensions, extend } from '@antv/g6';
const ExtGraph = extend(Graph, {
nodes: {
'sphere-node': Extensions.SphereNode,
},
behaviors: {
'orbit-canvas-3d': Extensions.OrbitCanvas3D,
'zoom-canvas-3d': Extensions.ZoomCanvas3D,
},
});
const graph = new ExtGraph({
renderer: 'webgl-3d', // can be 'canvas', 'svg', 'webgl', 'webgl-3d'
G6 supports 2D and 3D rendering using WebGL, which greatly improves rendering performance. Different renderers can be switched at runtime by configuring the renderer on the Graph [Renderer DEMO](/en/examples/feature/features/#lodLevels).
In addition, the layout package of G6, @antv/layout, supports WASM computation. To use this feature, you need to import the specific layout algorithm from the @antv/layout-wasm package and register it with Graph using extend, and then you can use it. [WASM Layout DEMO](/en/examples/feature/features/#wasmLayouts).
import { ForceLayout as ForceLayoutWASM, supportsThreads, initThreads } from '@antv/layout-wasm';
const ExtGraph = extend(Graph, {
layouts: {
'force-wasm': ForceLayoutWASM,
},
});
const supported = await supportsThreads();
const threads = await initThreads(supported);
const graph = new ExtGraph({
layout: {
type: 'force-wasm',
threads,
maxIteration: 200,
},
// ... other graph configurations
});
```
## 8️⃣. Reduce Package Size
In G6 v5, only the most commonly used features are registered on the Graph by default. Other features need to be imported from @antv/g6 or other packages and registered on the Graph before they can be configured.
```typescript
import { Graph, extend, Extensions } from '@antv/g6';
// External imported features
import { ForceLayout as ForceLayoutWASM, supportsThreads, initThreads } from '@antv/layout-wasm';
// Class CustomBehaviorClass...
// Class CustomEdge...
const ExtGraph = extend(Graph, {
behaviors: {
'activate-relations': Extensions.ActivateRelations, // Built-in interaction, not registered in advance