JavaScript is required

连线数据模型(JsonLine / RGLine

连线用于表示节点之间的方向关系。

与节点一样,连线也分为:

  • JsonLine:你输入的配置对象。
  • RGLine:图谱运行时对象。

1. 基础模型

最小连线数据至少包含:

  • from: string 起点节点 id
  • to: string 终点节点 id

示例:

const line = {
  id: 'line-a-b',
  from: 'a',
  to: 'b',
  text: 'A -> B'
};

2. 核心字段说明

标识与关系

  • id?: string(建议设置)
  • from: string
  • to: string
  • text?: string
  • type?: string
  • data?: Record<string, any>

形状与路径控制

  • lineShape?: RGLineShape
  • fromJunctionPoint?: RGJunctionPoint
  • toJunctionPoint?: RGJunctionPoint
  • junctionOffset?: number
  • lineRadius?: number(直角折线圆角)
  • polyLineStartDistance?: number(简易折线起始段)

样式与文字

  • color?: string
  • lineWidth?: number
  • opacity?: number
  • className?: string
  • fontColor?: string
  • fontSize?: number
  • textOffsetX?: number
  • textOffsetY?: number
  • placeText?: 'start' | 'center' | 'end'
  • textAnchor?: 'start' | 'middle' | 'end'

箭头与效果

  • startMarkerId?: string
  • endMarkerId?: string
  • dashType?: number
  • animation?: number
  • useTextOnPath?: boolean

行为控制

  • disablePointEvent?: boolean
  • selected?: boolean
  • hidden?: boolean
  • forDisplayOnly?: boolean

3. 常见 CRUD 操作

// Create
graphInstance.addLine({ from: 'a', to: 'b', text: 'A -> B' });
graphInstance.addLines([{ from: 'b', to: 'c' }]);

// Read
const line = graphInstance.getLineById('line-a-b');
const allLines = graphInstance.getLines();

// Update
graphInstance.updateLine('line-a-b', {
  color: '#60a5fa',
  lineWidth: 3,
  text: 'Updated'
});

// Delete
graphInstance.removeLineById('line-a-b');

4. 实战示例

const lines = [
  {
    id: 'l1',
    from: 'root',
    to: 'svc-a',
    text: 'request',
    lineShape: 2,
    color: '#3b82f6',
    endMarkerId: 'arrow-default',
    data: { weight: 0.8 }
  },
  {
    id: 'l2',
    from: 'root',
    to: 'svc-a',
    text: 'fallback',
    dashType: 2,
    opacity: 0.8,
    textOffsetY: -8
  }
];

graphInstance.addLines(lines);

5. 关键注意事项

  • 连线端点字段是 from/to,不是 source/target
  • 文本字段是 text,不是 label
  • hidden: true 只隐藏渲染,关系计算语义需结合运行时可见性理解。
  • 同一对节点支持多条连线,运行时会通过 RGLink 提供索引信息。

6. 下一步阅读