Loading...
Element State refers to the visual representation of elements (nodes, edges, combos) in a graph under different interaction scenarios. For example, when a user clicks on a node, the node might enter a "selected" state and change color; when the mouse hovers over an edge, the edge might enter a "highlight" state and become bold.
Simply put, states allow elements to dynamically change their appearance based on user operations or business logic.
G6 provides some commonly used built-in states that you can use directly:
State Name | Description | Typical Use Cases |
---|---|---|
selected | Selected state | When user clicks to select elements |
active | Active state | Currently interacting element |
highlight | Highlight state | Elements that need emphasis |
inactive | Inactive state | Dimmed display of unfocused elements |
disable | Disabled state | Non-interactive elements |
💡 Tip: These built-in states are not mandatory. You can completely define your own state names according to business requirements.
Configure corresponding styles for different states when creating a graph instance:
const graph = new Graph({// Node state style configurationnode: {// Default style (style when no state is applied)style: {fill: '#C6E5FF',stroke: '#5B8FF9',lineWidth: 1,},// Styles for various statesstate: {selected: {fill: '#95D6FB',stroke: '#1890FF',lineWidth: 2,shadowColor: '#1890FF',shadowBlur: 10,},highlight: {stroke: '#FF6A00',lineWidth: 2,},disable: {fill: '#ECECEC',stroke: '#BFBFBF',opacity: 0.5,},},},// Edge state style configurationedge: {style: {stroke: '#E2E2E2',lineWidth: 1,},state: {selected: {stroke: '#1890FF',lineWidth: 2,},highlight: {stroke: '#FF6A00',lineWidth: 3,},},},// Combo state style configurationcombo: {style: {fill: '#F0F0F0',stroke: '#D9D9D9',},state: {selected: {stroke: '#1890FF',lineWidth: 2,},},},});
You can create any custom states that meet your business requirements:
const graph = new Graph({node: {style: {fill: '#C6E5FF',stroke: '#5B8FF9',},state: {// Custom state: errorerror: {fill: '#FFEBE6',stroke: '#FF4D4F',lineWidth: 2,lineDash: [4, 4], // Dashed border},// Custom state: successsuccess: {fill: '#F6FFED',stroke: '#52C41A',lineWidth: 2,},// Custom state: warningwarning: {fill: '#FFFBE6',stroke: '#FAAD14',lineWidth: 2,// Add iconicon: {show: true,text: '⚠️',fontSize: 16,},},},},});
Set initial states for elements in data:
const data = {nodes: [{id: 'node1',states: ['selected'], // Initially in selected state},{id: 'node2',states: ['disabled'], // Initially in disabled state},{id: 'node3',states: ['highlight', 'active'], // Initially has multiple states},],edges: [{source: 'node1',target: 'node2',states: ['highlight'], // Initial state of the edge},],};graph.setData(data);
Dynamically change element states through API:
// Set a single state for a single elementgraph.setElementState('node1', 'selected');// Set multiple states for a single elementgraph.setElementState('node2', ['highlight', 'active']);// Batch set states for multiple elementsgraph.setElementState({node1: ['selected'],node2: ['highlight'],edge1: ['active'],});
When an element has multiple states, styles are stacked in order:
// Assume a node has both selected and highlight statesgraph.setElementState('node1', ['selected', 'highlight']);// Final style = default style + selected state style + highlight state style// If there are style conflicts, later state styles will override earlier ones
Restore elements to default state (no states):
// Clear all states of a single elementgraph.setElementState('node1', []);// Batch clear states of multiple elementsgraph.setElementState({node1: [],node2: [],edge1: [],});
If an element has multiple states, you can clear only some of them:
// Assume node1 currently has ['selected', 'highlight', 'active'] three states// Now only want to keep 'selected' state, clear other statesgraph.setElementState('node1', ['selected']);// Or get current states, then filter out unwanted statesconst currentStates = graph.getElementState('node1');const newStates = currentStates.filter((state) => state !== 'highlight');graph.setElementState('node1', newStates);
// Clear 'highlight' state from all nodesconst allNodes = graph.getNodeData();const stateUpdates = {};allNodes.forEach((node) => {const currentStates = graph.getElementState(node.id);const newStates = currentStates.filter((state) => state !== 'highlight');stateUpdates[node.id] = newStates;});graph.setElementState(stateUpdates);
// Get all states of a specified elementconst states = graph.getElementState('node1');console.log(states); // For example: ['selected', 'highlight']// If element has no states, returns empty arrayconsole.log(states); // []
// Get all node data in 'selected' stateconst selectedNodes = graph.getElementDataByState('node', 'selected');// Get all edge data in 'highlight' stateconst highlightEdges = graph.getElementDataByState('edge', 'highlight');
// Check if element is in specific stateconst states = graph.getElementState('node1');const isSelected = states.includes('selected');const isHighlight = states.includes('highlight');console.log('Is node selected:', isSelected);console.log('Is node highlighted:', isHighlight);