节点数据模型(JsonNode / RGNode)
节点是 relation-graph 中最基础的数据单元。
实际开发中会接触两种节点对象:
JsonNode:你输入给图谱的配置数据。RGNode:图谱运行时生成的节点对象。
1. JsonNode 与 RGNode 的关系
- 配置阶段使用
JsonNode。 - 运行时读取通常得到
RGNode。 RGNode会包含额外的运行时信息(可见性、布局计算、实际尺寸等)。
典型流程:
const nodeA = { id: 'a', text: 'Node A' };
graphInstance.addNodes([nodeA]);
const rgNodeA = graphInstance.getNodeById('a'); // RGNode
2. 必须掌握的核心字段
标识与文本
id: string必填且唯一。text?: string节点文本。
relation-graph 使用
text,不是label。
交互与状态
expanded?: booleanselected?: booleandisablePointEvent?: booleandisableDrag?: booleanhidden?: boolean
视觉样式
className?: stringnodeShape?: RGNodeShapecolor?: stringborderColor?: stringborderWidth?: numberborderRadius?: numberfontColor?: stringfontSize?: numberopacity?: number
几何与位置
width?: number、height?: numberx?: number、y?: numberzIndex?: number- 运行时测量值:
el_W、el_H
业务与树结构
data?: Record<string, any>业务扩展数据。children?: JsonNode[]树结构输入快捷写法。type?: string可用于按类型加样式(rg-node-type-{type})。
3. 常见 CRUD 操作
// Create
graphInstance.addNode({ id: 'n1', text: 'N1' });
graphInstance.addNodes([{ id: 'n2', text: 'N2' }]);
// Read
const node = graphInstance.getNodeById('n1');
const allNodes = graphInstance.getNodes();
// Update
graphInstance.updateNode('n1', {
text: 'N1 Updated',
color: '#4ade80'
});
// Delete
graphInstance.removeNodeById('n2');
4. 实战示例
const nodes = [
{
id: 'root',
text: 'Root',
type: 'service',
className: 'node-root',
color: '#dbeafe',
width: 140,
height: 48,
data: { owner: 'team-a' }
},
{
id: 'worker-1',
text: 'Worker 1',
type: 'worker',
x: 420,
y: 180,
fixed: true
}
];
graphInstance.addNodes(nodes);
5. 关键注意事项
id必须稳定且唯一。- 如果手动设置
x/y,不要和自动布局策略冲突。 children适合导入阶段;运行时是扁平化节点/连线结构。- 未设置宽高时,布局常需要等节点尺寸测量完成后再执行。