Skip to main content

Clustering

The ClusteringPlugin visualizes clusters of nodes and groups as passive SVG underlays behind your canvas. By default, it derives clusters automatically from connected components in the underlying model (via model.getClusters()) and renders a convex hull around each cluster—ideal for highlighting service boundaries or bounded contexts.

Instantiation​

Add ClusteringPlugin.type (or "clustering") to the plugins array in your Surface render options:
import { Component, ViewChild } from '@angular/core';
import { SurfaceComponent } from '@visuallyjs/browser-ui-angular';
import { ClusteringPlugin } from "@visuallyjs/browser-ui"


@Component({
selector: 'app-root',
template: ` <vjs-surface [renderOptions]="renderOptions"></vjs-surface> `
})
export class AppComponent {
renderOptions = {
plugins: [
{
type: ClusteringPlugin.type,
options: {
hulls: {
padding: 16
}
}
}
]
};
}

Choosing an Algorithm​

Set options.algorithm in either installation example above.

Spatial clustering groups vertices by their positions on the canvas. Topological clustering groups vertices by how their edges connect. Two vertices can be close on the canvas and belong to different connected components, or be far apart and belong to the same component.

AlgorithmTypeBasisWhat it groups
Connected components (default)connected-componentsTopologicalVertices reachable through edges, ignoring direction
LouvainlouvainTopologicalCommunities with dense internal connections
Label propagationlabel-propagationTopologicalCommunities formed by spreading labels across edges
DBSCANdbscanSpatialDense regions of nearby vertices, with optional noise clusters
K-meanskmeansSpatialVertices near a specified number of centroids
AgglomerativeagglomerativeSpatialNearby vertices merged according to a linkage rule
{
type: ClusteringPlugin.type,
options: {
algorithm: { type: "dbscan", epsilon: 60, minPoints: 3 },
hulls: { padding: 12 }
}
}

Spatial algorithms use the centers of visible rendered vertices in canvas coordinates by default. Supply getPosition(vertex) inside the algorithm object to use your own coordinates. A custom options.getClusters(ui) callback takes precedence over the selected algorithm.

See Clustering Algorithms for the full options and direct model examples.

Compare algorithms​

Switch from connected components to Louvain on the barbell graph to see how a single bridge affects the results. Try DBSCAN on concentric rings, then toggle Include noise on the noisy blobs dataset.

Convex Hulls​

Convex hulls wrap the bounding boxes of all vertices in each cluster. By default, hulls is true.

  • Set hulls: false to hide convex hulls.
  • Pass { padding: number } to expand each hull outward from its member vertices.
{
type: ClusteringPlugin.type,
options: {
hulls: { padding: 20 }
}
}

Voronoi Regions​

Voronoi regions partition the entire canvas into territories around each cluster's centroid—useful for visualizing coverage zones or regional hubs. By default, voronoi is false.

  • Set voronoi: true to enable Voronoi cells using automatic bounds.
  • Or pass a ClusteringVoronoiOptions object:
    • bounds — optional { x, y, w, h } (RectangleXY) clipping rectangle; computed automatically from the canvas contents when omitted.
    • padding — extra padding around the automatic bounds.
    • markers — true to draw a marker at each cluster's centroid, or a (site: ClusteringSite) => SVGElement factory function for custom markers.
{
type: ClusteringPlugin.type,
options: {
hulls: false,
voronoi: { markers: true, padding: 28 }
}
}

Custom Clusters​

By default, ClusteringPlugin groups vertices by graph connectivity. When edges cross domain boundaries—like an API Gateway connected to multiple teams—automatic connected-component detection would merge everything into a single cluster.

To group vertices using your own metadata (such as a team property on each node), supply a getClusters callback returning an array of ClusteringCluster objects ({ id, vertexIds, color? }):

{
type: ClusteringPlugin.type,
options: {
palette: ["#2563eb", "#059669", "#d97706", "#7c3aed"],
getClusters: (ui) => {
const byCluster = new Map()
ui.model.getNodes().forEach((n) => {
const key = n.data.cluster || "other"
if (!byCluster.has(key)) {
byCluster.set(key, { id: key, vertexIds: [], color: n.data.accent })
}
byCluster.get(key).vertexIds.push(n.id)
})
return Array.from(byCluster.values())
}
}
}

Options & Callbacks​

OptionTypeDefaultDescription
hullsboolean | { padding?: number }trueControls convex hull rendering around each cluster.
voronoiboolean | ClusteringVoronoiOptionsfalseControls Voronoi cell rendering around cluster centroids.
palettereadonly string[]Built-in paletteColors cycled through when a cluster does not specify color.
algorithmClusteringAlgorithmOptions or an algorithm name"connected-components"Selects the built-in clustering method.
getClusters(ui: BrowserUI) => readonly ClusteringCluster[]—Optional callback that overrides algorithm.
onClustersChanged(clusters: readonly ClusteringCluster[]) => void—Invoked whenever the resolved set of clusters changes.

Programmatic Control​

You can retrieve the plugin instance from the Surface to toggle layers or inspect clusters at runtime:

How to access the surface

From your app component​

To access the surface from inside a component that uses a vjs-surface, declare a ViewChild of type SurfaceComponent:

You can then use the surface member of your surface component to access the surface, which is of type Surface

import { ViewChild } from "@angular/core"
import { SurfaceComponent, SurfaceComponentRef } from "@visuallyjs/browser-ui-react"

@Component({
selector: 'app-my-app',
template: `<div>
<button (click)="centerContent">Center Content!</button>
<vjs-surface [renderOptions]="renderOptions">
</vjs-surface>
</div>
`,
standalone: true,
imports: [VisuallyJsModule]
})
export class MyComponent implements OnInit {

@ViewChild(SurfaceComponent) surfaceComponent!: SurfaceComponent;

centerContent() {
this.surfaceComponent.surface.centerContent()
}

}

From a different component​

If you have a component somewhere in your tree from which you wish to interact with the surface, you can use VisuallyJsService, which exposes a surface() signal:

import { ViewChild, inject } from "@angular/core"
import { VisuallyJsService } from "@visuallyjs/browser-ui-react"
import { BrowserUIModel } from "@visuallyjs/browser-ui"

@Component({
selector: 'app-my-app',
template: `<div>
<button (click)="centerContent">Center Content!</button>
</div>
`,
standalone: true,
imports: [VisuallyJsModule]
})
export class MyComponent implements OnInit {

$vjs = inject(VisuallyJsService)

centerContent() {
this.$vjs.surface().addNode({id:"1", left:50, top:50})
}

}
const clustering = surface.getPlugin("clustering")

// Switch algorithms and recompute membership
clustering.setAlgorithm({ type: "louvain", resolution: 1 })

// Toggle individual underlay layers
clustering.setLayerEnabled("voronoi", true)
clustering.setLayerEnabled("hulls", false)

// Inspect current clusters or force recomputation
const clusters = clustering.getClusters()
clustering.recompute()

refresh() also recomputes cluster membership and redraws the underlays. onClustersChanged receives the updated plugin clusters when membership changes.