logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.47
  • Introduction
  • Data
  • Getting Started
    • Quick Start
    • Installation
    • Integration
      • react
      • vue
      • angular
    • Step-by-step guide
  • Graph
    • Extensions En
    • Graph
    • Options
    • extension
  • Element
    • Element Overview
    • Element State
    • Node
      • Node Overview
      • Build-in Node
        • Common Node Configurations
        • Diamond
        • Donut
        • Ellipse
        • Hexagon
        • Html
        • Image
        • Rect
        • Star
        • Triangle
        • Circle
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Build-in Edge
        • Common Edge Configurations
        • Cubic Bezier Curve
        • CubicHorizontal Bezier Curve
        • CubicVertical Bezier Curve
        • Line
        • Polyline
        • Quadratic Bezier Curve
      • Custom Edge
    • Combo
      • Combo Overview
      • Build-in Combo
        • Circle
        • Combo Configuration Options
        • Rect
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Build-in Layout
      • 3D Force-Directed Layout
      • AntvDagre Layout
      • Circular Layout
      • ComboCombined Layout
      • Common Layout Configuration Options
      • CompactBox
      • Concentric Layout
      • D3 Force-Directed Layout
      • Dagre Layout
      • Dendrogram Layout
      • Fishbone Layout
      • Force Force-directed Layout
      • ForceAtlas2 Force-directed Layout
      • Fruchterman Force-directed Layout
      • Grid Layout
      • Indented Tree
      • MDS High-dimensional Data Dimensionality Reduction Layout
      • Mindmap Tree
      • Radial Layout
      • Random Layout
      • Snake Layout
    • Custom Layout
  • Behavior
    • Behavior Overview
    • Build-in Behavior
      • AutoAdaptLabel
      • BrushSelect
      • ClickSelect
      • CollapseExpand
      • CreateEdge
      • DragCanvas
      • DragElement
      • DragElementForce
      • FixElementSize
      • FocusElement
      • HoverActivate
      • LassoSelect
      • OptimizeViewportTransform
      • ScrollCanvas
      • ZoomCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Build-in Plugin
      • Background
      • BubbleSets
      • Contextmenu
      • EdgeBundling
      • EdgeFilterLens
      • Fisheye
      • Fullscreen
      • GridLine
      • History
      • Hull
      • Legend
      • Minimap
      • Snapline
      • Timebar
      • Toolbar
      • Tooltip
      • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • Build-in Transform
      • MapNodeSize
      • PlaceRadialLabels
      • ProcessParallelEdges
    • Custom Transform
  • Theme
    • Theme Overview
    • Custom Theme
    • Palette
    • Custom Palette
  • Animation
    • Animation Overview
    • Custom Animation
  • Further Reading
    • Event
    • renderer
    • coordinate
    • download-image
    • Using Iconfont
    • Use 3D
    • Bundle Project
  • What's new
    • Feature
    • Upgrade To 5.0
  • FAQ
  • contribute

CollapseExpand

Previous
ClickSelect
Next
CreateEdge

Resources

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-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

CollapseExpand is a built-in behavior in G6 used to implement the expand/collapse functionality for nodes or combos. Through double-click (default) or single-click actions, users can flexibly control the expand and collapse states of graph elements, effectively managing the visualization hierarchy of the graph structure and reducing visual complexity.

Use Cases

This behavior is mainly used for:

  • Managing large hierarchical graphs, enabling layered browsing of tree or network graphs
  • Simplifying the display of complex graphs, expanding areas of interest as needed
  • Hiding branch nodes that are temporarily not needed, focusing on important information

Online Experience

createGraph(
{
data: {
nodes: [
{ id: 'node1', combo: 'combo1', style: { x: 250, y: 150 } },
{ id: 'node2', combo: 'combo1', style: { x: 350, y: 150 } },
{ id: 'node3', combo: 'combo2', style: { x: 250, y: 300 } },
],
edges: [],
combos: [
{ id: 'combo1', combo: 'combo2' },
{ id: 'combo2', style: {} },
],
},
node: { style: { fill: '#7e3feb' } },
edge: { style: { stroke: '#8b9baf' } },
behaviors: [
{
type: 'collapse-expand',
key: 'collapse-expand',
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 400 },
(gui, graph) => {
const options = {
key: 'collapse-expand',
type: 'collapse-expand',
animation: true,
enable: true,
};
const optionFolder = gui.addFolder('CollapseExpand Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'collapse-expand',
[property]: value,
});
graph.render();
});
},
);

Basic Usage

Add this behavior in the graph configuration:

1. Quick Configuration (Static)

Declare directly using a string form. This method is simple but only supports default configurations and cannot be dynamically modified after configuration:

const graph = new Graph({
// other configurations...
behaviors: ['collapse-expand'],
});

2. Object Configuration (Recommended)

Configure using an object form, supporting custom parameters, and allowing dynamic updates to the configuration at runtime:

const graph = new Graph({
// other configurations...
behaviors: [
{
type: 'collapse-expand',
key: 'collapse-expand-1',
trigger: 'click', // Change the trigger method to single-click
animation: true, // Enable animation effects
},
],
});

Configuration Options

OptionDescriptionTypeDefault ValueRequired
typeBehavior type namecollapse-expand | stringcollapse-expand✓
animationEnable expand/collapse animation effectsbooleantrue
enableEnable expand/collapse functionalityboolean | ((event: [/en/api/event#event-object-properties]) => boolean)true
triggerTrigger method, can be single-click or double-clickclick | dblclickdblclick
onCollapseCallback function when collapse is completed(id: string) => void-
onExpandCallback function when expand is completed(id: string) => void-
alignAlign with the target element to avoid view offsetbooleantrue

Code Examples

Basic Expand/Collapse Functionality

const graph = new Graph({
container: 'container',
width: 800,
height: 600,
behaviors: ['collapse-expand'],
// other configurations...
});

Use Single-Click to Trigger Expand/Collapse

const graph = new Graph({
// other configurations...
behaviors: [
{
type: 'collapse-expand',
trigger: 'click', // Change the default double-click trigger to single-click
},
],
});

Custom Expand/Collapse Callback

const graph = new Graph({
// other configurations...
behaviors: [
{
type: 'collapse-expand',
onCollapse: (id) => {
console.log(`Node ${id} has collapsed`);
// Execute custom logic
},
onExpand: (id) => {
console.log(`Node ${id} has expanded`);
// Execute custom logic
},
},
],
});

Conditional Enablement of Expand/Collapse Functionality

const graph = new Graph({
// other configurations...
behaviors: [
{
type: 'collapse-expand',
// Enable expand/collapse functionality only when the target is a node type
enable: (event) => event.targetType === 'node',
},
],
});

Disable Animation Effects

const graph = new Graph({
// other configurations...
behaviors: [
{
type: 'collapse-expand',
animation: false, // Disable expand/collapse animation effects
},
],
});

FAQ

1. How to determine if a node is collapsed?

You can check the collapsed property in the node data:

const isCollapsed = (nodeId) => {
const nodeData = graph.getNodeData(nodeId);
return nodeData?.style?.collapsed === true;
};

2. How to programmatically expand or collapse a node?

In addition to being triggered by user interaction, you can also directly control using collapseElement or expandElement:

// Collapse node
graph.collapseElement('nodeId', { animation: true });
// Expand node
graph.expandElement('nodeId', { animation: true });

Real Cases