JavaScript & React libraryFeaturesLabels

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.


Labels

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:

PropertyDescriptionDefault
showTopLabelsShows labels for the most important points (determined by pointLabelWeightBy or point degree).true
showTopLabelsLimitThe maximum number of top labels to display.30
showDynamicLabelsAutomatically shows labels for points evenly distributed across the visible area.true
showDynamicLabelsLimitThe maximum number of dynamic labels to display.30
showLabelsForAn array of specific point IDs to always show labels for.undefined
showFocusedPointLabelWhether to show label for the focused point.true
showUnselectedPointLabelsWhen true, shows labels for unselected points during filtering/selection. When false, hides unselected labels to allocate more visual space for selected point labels.true
showSelectedLabelsWhen true, shows labels for selected points when filtering/selection is active. When false, selected point labels are hidden.true
selectedPointLabelsLimitMaximum 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:

PropertyDescription
pointLabelWeightByA column containing numeric values. Points with higher values are more likely to have their labels displayed.
pointLabelWeightFnA 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:

  1. pointSizeBy (if provided)
  2. Point degree (total number of links)

Style

You can customize the appearance of point labels:

PropertyDescriptionDefault
pointLabelColorCSS color for the labels. If not set, it matches the point color.undefined
pointLabelFontSizeFont size in pixels.13
pointLabelClassNameCSS class name for further styling. Can be a string or a function (text, pointIndex, pointId) => string.undefined
hoveredPointLabelClassNameCSS 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:

PropertyArgs / typeDescription
onLabelClick(index: number, id: string, event: MouseEvent) => voidCalled when a point label is clicked.
selectPointOnLabelClickboolean | 'single'When true, selects the clicked point and its neighbors. When 'single', selects only the clicked point.
focusPointOnLabelClickbooleanWhen true, focuses the clicked point.
const config = {
  // ...
  selectPointOnLabelClick: true,
  onLabelClick: (index, id) => {
    console.log('Clicked label:', { index, id })
  },
}

Advanced configuration

PropertyDescriptionDefault
labelMarginThe margin between the label and the point in pixels.5
labelPaddingThe padding inside the label in pixels. Takes an array [left, top, right, bottom].[6.5, 4.5, 6.5, 4.5]
staticLabelWeightThe weight multiplier for static labels (e.g. showLabelsFor).0.8
dynamicLabelWeightThe 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

PropertyDescriptionDefault
clusterLabelFontSizeBase font size for cluster labels. The final size might be adjusted based on scaleClusterLabels.16
scaleClusterLabelsScales label size based on the number of points in the cluster.true
usePointColorStrategyForClusterLabelsColors the label using the point color strategy.false
clusterLabelClassNameCSS 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:

PropertyArgs / typeDescription
onClusterLabelClick(clusterIndex: number, clusterId: string, event: MouseEvent) => voidCalled when a cluster label is clicked.
selectClusterOnLabelClickbooleanWhen 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.

VariableApplies toDefault
--cosmograph-label-background-colorPoint 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-opacityPoint and hovered labels75%, 25% on light graph backgrounds
--cosmograph-label-text-colorPoint and hovered labelsLabel color, lightened when dark on dark graphs
--cosmograph-label-text-shadowPoint and hovered labelsThe cluster halo colors at 30% of their opacity and a smaller size: four 0.5px offsets plus a 2px glow
--cosmograph-label-brightnessPoint and hovered labelsText 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-radiusPoint and hovered labels0.46em
--cosmograph-label-line-heightPoint, hovered and cluster labels1.2
--cosmograph-label-font-weightAll labels400
--cosmograph-label-transition-durationAll labels0.25s
--cosmograph-label-dimmed-opacityLabels outside the current selection0.2
--cosmograph-hovered-label-max-linesHovered point label5
--cosmograph-cluster-label-background-colorCluster labelsA 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-opacityCluster labels40%, 45% on light graph backgrounds
--cosmograph-cluster-label-text-colorCluster labelsThe label color, lightened when dark on dark graphs like point label colors, times --cosmograph-cluster-label-brightness
--cosmograph-cluster-label-text-shadowCluster labelsA 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-brightnessCluster labelsThe same as --cosmograph-label-brightness, for cluster labels
--cosmograph-cluster-label-opacityCluster labels0.9
--cosmograph-cluster-label-text-strokeCluster labels0, no outline; takes a -webkit-text-stroke value such as 0.75px #0b0e14
--cosmograph-cluster-label-border-radiusCluster labels0.3em
--cosmograph-link-label-background-colorLink labelsTransparent
--cosmograph-link-label-text-colorLink labelsThe link color times --cosmograph-link-label-brightness
--cosmograph-link-label-border-radiusLink labels4px
--cosmograph-link-label-text-stroke-widthLink labels0.06em
--cosmograph-link-label-text-stroke-colorLink labelsGraph background
--cosmograph-link-label-line-heightLink labels1; the text sits half a line above the link, so a taller line moves it further away
--cosmograph-link-label-brightnessLink labelsText 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:

PropertyTypeDescription
textstringThe text content of the label.
position[number, number]The [x, y] coordinates of the label.
colorstringOptional. The text color of the label. If not set, uses pointLabelColor.
classNamestringOptional. A custom CSS class for the label. Defaults to cosmographCustomLabel.
weightnumberOptional. The priority weight of the label. Defaults to staticLabelWeight.
fontSizenumberOptional. 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.