元素状态
上一篇
元素总览
下一篇
节点总览
Loading...
元素状态(State)是指图中元素(节点、边、组合)在不同交互场景下的视觉表现形式。比如当用户点击一个节点时,节点可能会变成"选中"状态并改变颜色;当鼠标悬停在边上时,边可能会进入"高亮"状态并加粗显示。
简单来说,状态让元素能够根据用户的操作或业务逻辑动态改变外观。
G6 提供了一些常用的内置状态,您可以直接使用:
状态名 | 说明 | 典型使用场景 |
---|---|---|
selected | 选中状态 | 用户点击选择元素时 |
active | 激活状态 | 当前正在交互的元素 |
highlight | 高亮状态 | 需要强调显示的元素 |
inactive | 非活跃状态 | 淡化显示非关注元素 |
disable | 禁用状态 | 不可交互的元素 |
💡 提示:这些内置状态并非必须使用,您完全可以根据业务需求定义自己的状态名称。
在创建图实例时,为不同状态配置相应的样式:
const graph = new Graph({// 节点的状态样式配置node: {// 默认样式(无状态时的样式)style: {fill: '#C6E5FF',stroke: '#5B8FF9',lineWidth: 1,},// 各种状态下的样式state: {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: {style: {stroke: '#E2E2E2',lineWidth: 1,},state: {selected: {stroke: '#1890FF',lineWidth: 2,},highlight: {stroke: '#FF6A00',lineWidth: 3,},},},// 组合的状态样式配置combo: {style: {fill: '#F0F0F0',stroke: '#D9D9D9',},state: {selected: {stroke: '#1890FF',lineWidth: 2,},},},});
您可以创建任何符合业务需求的自定义状态:
const graph = new Graph({node: {style: {fill: '#C6E5FF',stroke: '#5B8FF9',},state: {// 自定义状态:错误error: {fill: '#FFEBE6',stroke: '#FF4D4F',lineWidth: 2,lineDash: [4, 4], // 虚线边框},// 自定义状态:成功success: {fill: '#F6FFED',stroke: '#52C41A',lineWidth: 2,},// 自定义状态:警告warning: {fill: '#FFFBE6',stroke: '#FAAD14',lineWidth: 2,// 添加图标icon: {show: true,text: '⚠️',fontSize: 16,},},},},});
在数据中为元素设置初始状态:
const data = {nodes: [{id: 'node1',states: ['selected'], // 初始为选中状态},{id: 'node2',states: ['disabled'], // 初始为禁用状态},{id: 'node3',states: ['highlight', 'active'], // 初始有多个状态},],edges: [{source: 'node1',target: 'node2',states: ['highlight'], // 边的初始状态},],};graph.setData(data);
通过 API 动态改变元素状态:
// 设置单个元素的单个状态graph.setElementState('node1', 'selected');// 设置单个元素的多个状态graph.setElementState('node2', ['highlight', 'active']);// 批量设置多个元素的状态graph.setElementState({node1: ['selected'],node2: ['highlight'],edge1: ['active'],});
当元素有多个状态时,样式会按顺序叠加:
// 假设节点同时有 selected 和 highlight 状态graph.setElementState('node1', ['selected', 'highlight']);// 最终样式 = 默认样式 + selected状态样式 + highlight状态样式// 如果有样式冲突,后面的状态样式会覆盖前面的
将元素恢复到默认状态(无任何状态):
// 清除单个元素的所有状态graph.setElementState('node1', []);// 批量清除多个元素的状态graph.setElementState({node1: [],node2: [],edge1: [],});
如果元素有多个状态,您可以只清除其中的某些状态:
// 假设 node1 当前有 ['selected', 'highlight', 'active'] 三个状态// 现在只想保留 'selected' 状态,清除其他状态graph.setElementState('node1', ['selected']);// 或者获取当前状态,然后过滤掉不需要的状态const currentStates = graph.getElementState('node1');const newStates = currentStates.filter((state) => state !== 'highlight');graph.setElementState('node1', newStates);
// 清除所有节点的 'highlight' 状态const 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);
// 获取指定元素的所有状态const states = graph.getElementState('node1');console.log(states); // 例如:['selected', 'highlight']// 如果元素没有任何状态,返回空数组console.log(states); // []
// 获取所有处于 'selected' 状态的节点数据const selectedNodes = graph.getElementDataByState('node', 'selected');// 获取所有处于 'highlight' 状态的边数据const highlightEdges = graph.getElementDataByState('edge', 'highlight');
// 检查元素是否处于特定状态const states = graph.getElementState('node1');const isSelected = states.includes('selected');const isHighlight = states.includes('highlight');console.log('节点是否选中:', isSelected);console.log('节点是否高亮:', isHighlight);