Loading...
The Element operation API in G6 allows you to control the behavior and attributes of elements such as nodes, edges, and Combos in the graph. These APIs can be used for:
Through these operations, you can achieve rich interactive effects and visual presentations.
Get the position of an element.
getElementPosition(id: ID): Point;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Example:
graph.getElementPosition('node1');
Get the rendering bounding box of the element itself and its child nodes in the world coordinate system.
getElementRenderBounds(id: ID): AABB;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Get the rendering style of an element.
getElementRenderStyle(id: ID): Record<string, any>;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Get the state of an element.
getElementState(id: ID): State[];
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Get the type of an element.
getElementType(id: ID): string;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Get the visibility of an element.
getElementVisibility(id: ID): 'visible' | 'hidden';
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Get the z-index of an element.
getElementZIndex(id: ID): number;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
Return Value:
Set the state of an element, supporting two calling methods:
// Set the state of a single elementsetElementState(id: ID, state: State | State[], animation?: boolean): Promise<void>;// Set the state of multiple elementssetElementState(state: Record<ID, State | State[]>, animation?: boolean): Promise<void>;
Parameters:
Single Element State Setting
| Parameter | Description | Type | Default | Required | | --------- | ----------------- | --------------- | ------- | -------- | --- | | id | Element ID to set | string | - | ✓ | | state | State to set | State | State[] | - | ✓ | | animation | Enable animation | boolean | - | |
Batch Element State Setting
| Parameter | Description | Type | Default | Required | | --------- | ------------------------------ | -------------------------- | -------- | -------- | --- | | state | Mapping of element ID to state | Record<ID, State | State[]> | - | ✓ | | animation | Enable animation | boolean | - | |
Return Value:
Example:
// Set the state of a single elementawait graph.setElementState('node1', 'selected');// Set the state of multiple elementsawait graph.setElementState({node1: 'selected',node2: 'hover',node3: ['selected', 'hover'],});
Set the visibility of an element, supporting two calling methods:
// Set the visibility of a single elementsetElementVisibility(id: ID, visibility: 'visible' | 'hidden', animation?: boolean): Promise<void>;// Set the visibility of multiple elementssetElementVisibility(visibility: Record<ID, 'visible' | 'hidden'>, animation?: boolean): Promise<void>;
Parameters:
Single Element Visibility Setting
| Parameter | Description | Type | Default | Required | | ---------- | ----------------- | --------- | -------- | -------- | --- | | id | Element ID to set | string | - | ✓ | | visibility | Visibility to set | 'visible' | 'hidden' | - | ✓ | | animation | Enable animation | boolean | - | |
Batch Element Visibility Setting
| Parameter | Description | Type | Default | Required | | ---------- | ----------------------------------- | -------------------- | --------- | -------- | --- | | visibility | Mapping of element ID to visibility | Record<ID, 'visible' | 'hidden'> | - | ✓ | | animation | Enable animation | boolean | - | |
Return Value:
Example:
// Set the visibility of a single elementawait graph.setElementVisibility('node1', 'hidden');// Set the visibility of multiple elementsawait graph.setElementVisibility({node1: 'hidden',node2: 'visibility',});
Set the z-index of an element, supporting two calling methods:
// Set the z-index of a single elementsetElementZIndex(id: ID, zIndex: number): Promise<void>;// Set the z-index of multiple elementssetElementZIndex(zIndex: Record<ID, number>): Promise<void>;
Parameters:
Single Element Z-Index Setting
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
zIndex | Z-Index | number | - | ✓ |
Batch Element Z-Index Setting
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
zIndex | Mapping of element ID to z-index | Record<ID, number> | - | ✓ |
Return Value:
Example:
// Set the z-index of a single elementawait graph.setElementZIndex('node1', 10);// Set the z-index of multiple elementsawait graph.setElementZIndex({node1: 10,node2: 20,node3: 30,});
Set the node style mapping, i.e., the value of options.node
.
setNode(node: NodeOptions): void;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
node | Node configuration | NodeOptions | - | ✓ |
Example:
// Set the fill color of all nodes to redgraph.setNode({style: {fill: 'red',},});
Set the edge style mapping, i.e., the value of options.edge
.
setEdge(edge: EdgeOptions): void;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
edge | Edge configuration | EdgeOptions | - | ✓ |
Set the combo style mapping, i.e., the value of options.combo
.
setCombo(combo: ComboOptions): void;
Parameters:
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
combo | Combo configuration | ComboOptions | - | ✓ |
Collapse the specified element, usually used to collapse Combos or nodes with child elements.
collapseElement(id: ID, options?: boolean | CollapseExpandNodeOptions): Promise<void>;
Parameters:
| Parameter | Description | Type | Default | Required | | --------- | --------------------------------------------------------------- | ------- | ------------------------------------------------------- | -------- | --- | | id | Element ID to collapse | string | - | ✓ | | options | Enable animation or detailed configuration for collapsing nodes | boolean | CollapseExpandNodeOptions | - | |
Return Value:
Example:
// Simple collapse with default configurationawait graph.collapseElement('combo1');// Collapse with animationgraph.collapseElement('combo1', true);// Collapse while ensuring the position of expanded/collapsed nodes remains unchangedawait graph.collapseElement('combo1', {align: true,});
Expand the specified element, usually used to expand previously collapsed Combos or nodes.
expandElement(id: ID, options?: boolean | CollapseExpandNodeOptions): Promise<void>;
Parameters:
| Parameter | Description | Type | Default | Required | | --------- | -------------------------------------------------------------- | ------- | ------------------------------------------------------- | -------- | --- | | id | Element ID to expand | string | - | ✓ | | options | Enable animation or detailed configuration for expanding nodes | boolean | CollapseExpandNodeOptions | - | |
Return Value:
Example:
// Simple expand with default configurationawait graph.expandElement('combo1');// Expand with animationawait graph.expandElement('combo1', true);// Expand while ensuring the position of expanded/collapsed nodes remains unchangedawait graph.expandElement('combo1', {align: true,});
Bring the specified element to the front, making it appear above other overlapping elements.
frontElement(id: ID | ID[]): void;
Parameters:
| Parameter | Description | Type | Default | Required | | --------- | ----------- | ------ | -------- | -------- | --- | | id | Element ID | string | string[] | - | ✓ |
Return Value:
Example:
// Bring a node to the frontgraph.frontElement('node1');// Bring multiple selected nodes to the frontgraph.frontElement(['node1', 'node2', 'node3']);
Show the specified element.
showElement(id: ID | ID[], animation?: boolean): Promise<void>;
Parameters:
| Parameter | Description | Type | Default | Required | | --------- | ---------------- | ------- | -------- | -------- | --- | | id | Element ID | string | string[] | - | ✓ | | animation | Enable animation | boolean | - | |
Return Value:
Example:
// Show a single elementawait graph.showElement('node1');// Show an element with animationawait graph.showElement('node1', true);// Show multiple elementsawait graph.showElement(['node1', 'node2', 'node3']);
Hide the specified element.
hideElement(id: ID | ID[], animation?: boolean): Promise<void>;
Parameters:
| Parameter | Description | Type | Default | Required | | --------- | ---------------- | ------- | -------- | -------- | --- | | id | Element ID | string | string[] | - | ✓ | | animation | Enable animation | boolean | - | |
Return Value:
Example:
// Hide an element without animationawait graph.hideElement('node1');// Hide an element with animationawait graph.hideElement('node1', true);// Hide multiple elementsawait graph.hideElement(['node1', 'node2', 'node3'], true);
Translate an element by a specified distance, supporting two calling methods:
// Translate an element by a specified distance (relative translation)translateElement(id: ID, offset: Point, animation?: boolean): Promise<void>;// Translate multiple elements by a specified distance (relative translation)translateElement(offsets: Record<ID, Point>, animation?: boolean): Promise<void>;
Parameters:
Single Element Translation
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
offset | Relative translation distance [dx, dy] | [number, number] | - | ✓ |
animation | Enable animation | boolean | - |
Batch Element Translation
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
offsets | Mapping of element ID to translation distance | Record<ID, [number, number]> | - | ✓ |
animation | Enable animation | boolean | - |
Return Value:
Example:
// Translate right by 100 pixels and down by 50 pixelsawait graph.translateElementBy('node1', [100, 50]);// Translate with animationawait graph.translateElementBy('node1', [100, 50], true);// Apply the same translation to multiple nodesawait graph.translateElementBy({node1: [50, 50],node2: [100, 100],node3: [150, 150],},true,);
Move an element to a specified position, supporting two calling methods:
// Move an element to a specified position (absolute position)translateElementTo(id: ID, position: Point, animation?: boolean): Promise<void>;// Move multiple elements to specified positions (absolute position)translateElementTo(positions: Record<ID, Point>, animation?: boolean): Promise<void>;
Parameters:
Single Element Movement
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
id | Element ID | string | - | ✓ |
position | Target absolute position [x, y] | [number, number] | - | ✓ |
animation | Enable animation | boolean | - |
Batch Element Movement
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
positions | Mapping of element ID to target position | Record<string, [number, number]> | - | ✓ |
animation | Enable animation | boolean | - |
Return Value:
Example:
// Move a node to position (200, 300) on the canvasawait graph.translateElementTo('node1', [200, 300]);// Move with animationawait graph.translateElementTo('node1', [200, 300], true);// Arrange a group of nodes neatlyawait graph.translateElementTo({node1: [100, 100],node2: [200, 200],node3: [300, 100],},true,);
Focus on the specified element, centering it in the viewport.
focusElement(id: ID | ID[], animation?: ViewportAnimationEffectTiming): Promise<void>;
Parameters:
| Parameter | Description | Type | Default | Required | | --------- | ----------------------------------- | --------------------------------------------------------------- | -------- | -------- | --- | | id | One or more element IDs to focus on | string | string[] | - | ✓ | | animation | Viewport animation configuration | ViewportAnimationEffectTiming | - | |
Return Value:
Example:
// Focus on a single nodeawait graph.focusElement('node1');// Use custom animation configurationawait graph.focusElement('node1', {duration: 800,easing: 'ease-in-out',});// Focus on multiple nodesawait graph.focusElement(['node1', 'node2', 'node3']);
Configuration options for collapsing or expanding elements.
interface CollapseExpandNodeOptions {/*** Enable animation*/animation?: boolean;/*** Ensure the position of expanded/collapsed nodes remains unchanged*/align?: boolean;}
Viewport animation configuration type.
type ViewportAnimationEffectTiming =| boolean // Enable animation| {easing?: string; // Easing functionduration?: number; // Animation duration (ms)};
AABB (Axis-Aligned Bounding Box) is a fundamental concept in computer graphics.
interface AABB {x: number; // x-coordinate of the top-left corner of the rectangley: number; // y-coordinate of the top-left corner of the rectanglewidth: number; // Width of the rectangleheight: number; // Height of the rectangle}
Element state type.
type State = 'selected' | 'hover' | 'active' | 'inactive' | 'disabled' | string;