Usage guidesElementsEdges

Custom edges

Reuse built-in edge bases with data-driven styles, states, and data updates

Use an EdgeExtension to reuse an edge type for a class of relationships. The base selects the path, arrows show direction, and edge.style.label supplies text. To vary width or color by data in one graph, use style functions.

Register and activate

Choose arc as the base path, use getStyle() to set color and width by weight, and register the type with register('edge', ...).

const weightedEdge: EdgeExtension = {
  base: 'arc',
  style: { curvature: 0.2, endArrow: true, hitAreaWidth: 14 },
  getStyle(_source, _target, edge) {
    const weight = Number(edge.data?.weight ?? 1);
    return { stroke: weight > 2 ? '#cf1322' : '#5366ad', lineWidth: weight + 1 };
  },
  stateStyles: { selected: { stroke: '#873bf4', lineWidth: 4 } },
};
register('edge', 'docs-weighted-edge', weightedEdge);

Set edge.type to the registered name in new Graph(). In this example, light starts with weight 1 and heavy with weight 4.

Loading example…
Click an edge to select. Toggle heavy weight; clear selection to compare base styles. Reset restores weights
edge: { type: 'docs-weighted-edge', style: { label: (_s, _t, edge) => edge.id ?? '' } },

Overrides and updates

Click Toggle heavy weight above. updateEdgeData() changes the weight and getStyle() updates the color and width. Click an edge to apply the purple selection style, or click empty space to restore its base style.

ui.addAction('Toggle heavy weight', () => {
  const weight = graph.getEdgeData('heavy')?.data?.weight === 4 ? 1 : 4;
  graph.updateEdgeData('heavy', { weight });
});

See EdgeExtension for style precedence and extension fields.

Common questions

Can I implement arbitrary drawing or shaders?

EdgeExtension provides base, style, getStyle, and stateStyles, without arbitrary draw / shader hooks. See EdgeExtension.

Why is a label missing?

Descriptor defaults do not replace label activation. This example explicitly provides edge.style.label; data or descriptor text alone does not guarantee label traversal.

Why is an edge difficult to select?

Set hitAreaWidth and enable interaction. The example uses 14 without increasing visual width; see picking width.

See registration for caching and errors.