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
AddClusteringPlugin.type (or "clustering") to the plugins array in your Surface render options:<script>
import { SurfaceComponent } from "@visuallyjs/browser-ui-svelte"
import { ClusteringPlugin } from "@visuallyjs/browser-ui"
const renderOptions = {
plugins: [
{
type: ClusteringPlugin.type,
options: {
hulls: {
padding: 16
}
}
}
]
}
</script>
<SurfaceComponent {renderOptions}/>
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.
| Algorithm | Type | Basis | What it groups |
|---|---|---|---|
| Connected components (default) | connected-components | Topological | Vertices reachable through edges, ignoring direction |
| Louvain | louvain | Topological | Communities with dense internal connections |
| Label propagation | label-propagation | Topological | Communities formed by spreading labels across edges |
| DBSCAN | dbscan | Spatial | Dense regions of nearby vertices, with optional noise clusters |
| K-means | kmeans | Spatial | Vertices near a specified number of centroids |
| Agglomerative | agglomerative | Spatial | Nearby 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: falseto 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: trueto enable Voronoi cells using automatic bounds. - Or pass a
ClusteringVoronoiOptionsobject: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—trueto draw a marker at each cluster's centroid, or a(site: ClusteringSite) => SVGElementfactory 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
| Option | Type | Default | Description |
|---|---|---|---|
hulls | boolean | { padding?: number } | true | Controls convex hull rendering around each cluster. |
voronoi | boolean | ClusteringVoronoiOptions | false | Controls Voronoi cell rendering around cluster centroids. |
palette | readonly string[] | Built-in palette | Colors cycled through when a cluster does not specify color. |
algorithm | ClusteringAlgorithmOptions 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 SurfaceComponent, declare a ref of type SurfaceComponent and assign it in the template.
You can then access the model through the ref's value, which is of type SurfaceComponent
<script setup lang="ts">
import { SurfaceComponent} from "@visuallyjs/browser-ui-svelte"
let surfaceComponent: SurfaceComponent = $state(null)
function centerContent() {
surfaceComponent.getSurface().centerContent()
}
</script>
<template>
<SurfaceComponent bind:this={surfaceComponent}/>
<button onClick={() => centerContent()}>Center Content!</button>
</template>
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 the useSurface() hook in conjunction with a SurfaceProvider to access the surface. Your app component should look something like this:
<script setup lang="ts">
</script>
<template>
<SurfaceProvider>
<SurfaceComponent/>
<MyOtherComponent/>
</SurfaceProvider>
</template>
Then the implementation of MyOtherComponent can use the useSurface hook:
<script setup lang="ts">
import { useSurface } from "@visuallyjs/browser-ui-svelte"
import { Surface } from "@visuallyjs/browser-ui"
const surface:RefObject<Surface> = useSurface() // this is reactive state
function centerContent() {
surface.current.centerContent()
}
</script>
<template>
<div><button onClick={() => centerContent()}>Center content!</button></div>
</template>
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.