Labels
Cosmograph allows you to display labels for points and clusters to provide context and identification for your data elements. You can control which labels are shown, their appearance, and their priority.
Point labels
To display labels for points, you need to specify which data column contains the label text using the pointLabelBy property.
You must also enable labels globally by setting showLabels to true.
const config = {
// ...
points: [
{ id: '1', name: 'Alpha', importance: 10 },
{ id: '2', name: 'Beta', importance: 5 },
// ...
],
pointLabelBy: 'name',
showLabels: true
}Visibility control
Cosmograph provides several ways to control which labels are displayed to avoid clutter:
| Property | Description | Default |
|---|---|---|
showTopLabels | Shows labels for the most important points (determined by pointLabelWeightBy or point degree). | true |
showTopLabelsLimit | The maximum number of top labels to display. | 30 |
showDynamicLabels | Automatically shows labels for points evenly distributed across the visible area. | true |
showDynamicLabelsLimit | The maximum number of dynamic labels to display. | 30 |
showLabelsFor | An array of specific point IDs to always show labels for. | undefined |
showFocusedPointLabel | Whether to show label for the focused point. | true |
showUnselectedPointLabels | When true, shows labels for unselected points during filtering/selection. When false, hides unselected labels to allocate more visual space for selected point labels. | true |
showSelectedLabels | When true, shows labels for selected points when filtering/selection is active. When false, selected point labels are hidden. | true |
selectedPointLabelsLimit | Maximum number of labels to show for selected points when selection is active. | 100 |
Weight
You can influence which labels are prioritized using the following properties:
| Property | Description |
|---|---|
pointLabelWeightBy | A column containing numeric values. Points with higher values are more likely to have their labels displayed. |
pointLabelWeightFn | A function to transform weight values programmatically. It receives the value from pointLabelWeightBy and the point index, and should return a number between 0 and 1. |
const config = {
// ...
pointLabelWeightBy: 'importance',
// normalize / clamp any data into 0..1
pointLabelWeightFn: (value) => Math.max(0, Math.min(1, Number(value) / 100)),
}If not provided, labels are prioritized based on:
pointSizeBy(if provided)- Point degree (total number of links)
Style
You can customize the appearance of point labels:
| Property | Description | Default |
|---|---|---|
pointLabelColor | CSS color for the labels. If not set, it matches the point color. | undefined |
pointLabelFontSize | Font size in pixels. | 13 |
pointLabelClassName | CSS class name for further styling. Can be a string or a function (text, pointIndex, pointId) => string. | undefined |
hoveredPointLabelClassName | CSS class name for the hovered point label. Can be a string or a function. | undefined |
pointLabelClassName / clusterLabelClassName can also be an inline CSS string (e.g. 'background: #333; color: white;'). If the string contains :, Cosmograph treats it as inline styles.
A class name, or an inline style that includes color, takes over the label color: Cosmograph then skips pointLabelColor, linkLabelColor and the computed point, link or cluster color. An inline style without color keeps the computed color.
Hovering
You can display a label when hovering over a point using showHoveredPointLabel.
const config = {
// ...
pointLabelBy: 'id',
showHoveredPointLabel: true,
hoveredPointLabelClassName: 'my-custom-tooltip' // optional
}This label uses the same text source as defined in pointLabelBy or pointLabelFn.
Hovered labels still require pointLabelBy, but they do not require showLabels. Hovered labels are the exception: they depend on showHoveredPointLabel and do not require showLabels.
Interactivity
You can control how users interact with point labels through these props:
| Property | Args / type | Description |
|---|---|---|
onLabelClick | (index: number, id: string, event: MouseEvent) => void | Called when a point label is clicked. |
selectPointOnLabelClick | boolean | 'single' | When true, selects the clicked point and its neighbors. When 'single', selects only the clicked point. |
focusPointOnLabelClick | boolean | When true, focuses the clicked point. |
const config = {
// ...
selectPointOnLabelClick: true,
onLabelClick: (index, id) => {
console.log('Clicked label:', { index, id })
},
}Advanced configuration
| Property | Description | Default |
|---|---|---|
labelMargin | The margin between the label and the point in pixels. | 5 |
labelPadding | The padding inside the label in pixels. Takes an array [left, top, right, bottom]. | [6.5, 4.5, 6.5, 4.5] |
staticLabelWeight | The weight multiplier for static labels (e.g. showLabelsFor). | 0.8 |
dynamicLabelWeight | The weight multiplier for dynamic labels. | 0.7 |
Cluster labels
Cluster labels provide a high-level overview of the graph structure. To use them, you must have clustering enabled via pointClusterBy.
Enable cluster labels by setting showClusterLabels to true.
const config = {
// ...
pointClusterBy: 'group',
showClusterLabels: true,
}Cluster labels take priority over point labels to avoid clutter. When points are selected or filtered, cluster labels are hidden only if point labels are configured to be shown (for example via pointLabelBy with showTopLabels, showFocusedPointLabel, or showDynamicLabels). When cluster labels are visible, showLabelsFor labels can still appear alongside them. If no point labels are configured, cluster labels remain visible.
Style
| Property | Description | Default |
|---|---|---|
clusterLabelFontSize | Base font size for cluster labels. The final size might be adjusted based on scaleClusterLabels. | 16 |
scaleClusterLabels | Scales label size based on the number of points in the cluster. | true |
usePointColorStrategyForClusterLabels | Colors the label using the point color strategy. | false |
clusterLabelClassName | CSS class for custom styling. Can be a string or a function (text, clusterIndex) => string. | Default style |
Interactivity
You can control how users interact with cluster labels through these props:
| Property | Args / type | Description |
|---|---|---|
onClusterLabelClick | (clusterIndex: number, clusterId: string, event: MouseEvent) => void | Called when a cluster label is clicked. |
selectClusterOnLabelClick | boolean | When true, selects all points in the clicked cluster. |
const config = {
// ...
selectClusterOnLabelClick: true,
onClusterLabelClick: (clusterIndex, clusterId) => {
console.log('Clicked cluster label:', { clusterIndex, clusterId })
},
}CSS variables
Label styles read CSS custom properties, so you can tune them without a class. Set them on the Cosmograph container or any ancestor.
.my-graph {
--cosmograph-label-background-color: #24272fcc;
--cosmograph-cluster-label-brightness: 1.2;
}Point and hovered labels keep their text color and get a faint halo in the cluster halo colors; on light graphs their box turns almost transparent. Link labels outline their text in the graph background color, which separates it from the link underneath. Cluster labels use a larger halo. On dark graphs every label lightens dark colors and multiplies its text color by a brightness of 1.5; on light graphs label colors render as they are. This applies whether the color comes from the data or pointLabelColor; a color set through a --cosmograph-*-text-color variable renders exactly. A label with a custom class or inline style gets no automatic halo and no brightening, so its class controls its look and its colors render exactly as set.
| Variable | Applies to | Default |
|---|---|---|
--cosmograph-label-background-color | Point and hovered labels | #353a47 at --cosmograph-label-background-opacity on dark graph backgrounds, oklch(0.67 0.02 269.19) on light ones |
--cosmograph-label-background-opacity | Point and hovered labels | 75%, 25% on light graph backgrounds |
--cosmograph-label-text-color | Point and hovered labels | Label color, lightened when dark on dark graphs |
--cosmograph-label-text-shadow | Point and hovered labels | The cluster halo colors at 30% of their opacity and a smaller size: four 0.5px offsets plus a 2px glow |
--cosmograph-label-brightness | Point and hovered labels | Text color multiplier, the same as --cosmograph-link-label-brightness: 1.5 on dark graph backgrounds, 1 on light ones. Affects the text only, not the box or halo |
--cosmograph-label-border-radius | Point and hovered labels | 0.46em |
--cosmograph-label-line-height | Point, hovered and cluster labels | 1.2 |
--cosmograph-label-font-weight | All labels | 400 |
--cosmograph-label-transition-duration | All labels | 0.25s |
--cosmograph-label-dimmed-opacity | Labels outside the current selection | 0.2 |
--cosmograph-hovered-label-max-lines | Hovered point label | 5 |
--cosmograph-cluster-label-background-color | Cluster labels | A blue-gray 0.12 OKLCH lightness away from the graph background (rgb(68 81 104) on the default dark graph, a near-neutral light gray on white), at --cosmograph-cluster-label-background-opacity |
--cosmograph-cluster-label-background-opacity | Cluster labels | 40%, 45% on light graph backgrounds |
--cosmograph-cluster-label-text-color | Cluster labels | The label color, lightened when dark on dark graphs like point label colors, times --cosmograph-cluster-label-brightness |
--cosmograph-cluster-label-text-shadow | Cluster labels | A 1px halo plus a soft 5px glow, both derived from the graph background: #5e6792 at 63% and black at 15% on the default dark graph, #acaeb8 at 30% and a gray #b7b7b7 glow at 45% on light graphs |
--cosmograph-cluster-label-brightness | Cluster labels | The same as --cosmograph-label-brightness, for cluster labels |
--cosmograph-cluster-label-opacity | Cluster labels | 0.9 |
--cosmograph-cluster-label-text-stroke | Cluster labels | 0, no outline; takes a -webkit-text-stroke value such as 0.75px #0b0e14 |
--cosmograph-cluster-label-border-radius | Cluster labels | 0.3em |
--cosmograph-link-label-background-color | Link labels | Transparent |
--cosmograph-link-label-text-color | Link labels | The link color times --cosmograph-link-label-brightness |
--cosmograph-link-label-border-radius | Link labels | 4px |
--cosmograph-link-label-text-stroke-width | Link labels | 0.06em |
--cosmograph-link-label-text-stroke-color | Link labels | Graph background |
--cosmograph-link-label-line-height | Link labels | 1; the text sits half a line above the link, so a taller line moves it further away |
--cosmograph-link-label-brightness | Link labels | Text color multiplier: 1.5 on dark backgrounds, 1 on light ones; affects the text color only, not the background |
Custom labels
You can manually add labels to the graph using the customLabels property. This is useful for annotations or highlighting specific areas unrelated to data points.
const config = {
// ...
customLabels: [
{ text: 'My Label', position: [100, 200] },
{ text: 'Important Area', position: [500, 300], className: 'myCustomLabel' }
]
}The customLabels property expects an array of objects with the following properties:
| Property | Type | Description |
|---|---|---|
text | string | The text content of the label. |
position | [number, number] | The [x, y] coordinates of the label. |
color | string | Optional. The text color of the label. If not set, uses pointLabelColor. |
className | string | Optional. A custom CSS class for the label. Defaults to cosmographCustomLabel. |
weight | number | Optional. The priority weight of the label. Defaults to staticLabelWeight. |
fontSize | number | Optional. The font size of the label. |
padding | { left: number; top: number; right: number; bottom: number } | Optional. Padding around the label. |
Custom labels use the same CSS class as point labels by default. You can override this using the className property.