groups

groups.createBy

createBy(fn: (node: NodeData) => string, spec?: Partial<GroupSpec> | (key: string, nodeIds: string[]) => Partial<GroupSpec>): string[]

Group nodes using a custom accessor function (D3-style).

Each node is passed to fn; the returned string becomes the group key (and default name). Nodes where fn returns null/undefined are skipped.

The optional second parameter controls per-group visual properties:

  • A Partial<GroupSpec> object is applied to all groups.
  • A function (key, nodeIds) => Partial<GroupSpec> is called per group, enabling different colors, stroke widths, etc. per group.

Not undoable. Emits "group:created" for each created group.

fn(node: NodeData) => stringAccessor that maps a node to its group key.
specPartial<GroupSpec> | (key: string, nodeIds: string[]) => Partial<GroupSpec>Static overrides or per-group spec function.

Returns: string[]

Example

// Simple — same as createByAttribute("category")
api.groups.createBy(node => node.category);

// Conditional grouping with per-group styling
api.groups.createBy(
  node => (node.properties.degree as number) > 10 ? "Hub" : "Leaf",
  (key) => ({ color: key === "Hub" ? "#e74c3c" : "#95a5a6" }),
);