JavaScript & React libraryFeaturesColor

Color

Cosmograph provides powerful and flexible ways to color both points and links in your graph visualizations. You can color elements based on data attributes, topology, or even custom logic.

Coloring in Cosmograph is managed through a set of predefined strategies. Each strategy is an algorithm that dictates how data column values are translated into visual colors. You can explicitly select a strategy to match your visualization goals, or you can let Cosmograph automatically select the most suitable one based on your data’s type and structure. This provides both fine-grained control and ease of use.

Point Coloring Strategies

Point coloring is primarily dependent on the pointColorBy configuration property, which specifies the data column to use for coloring.

You can explicitly set a coloring strategy using the pointColorStrategy property. If not provided, Cosmograph will choose an optimal strategy.

StrategyDescriptionKey Properties
singleColors all points with a single, uniform color.pointDefaultColor*
categoricalAssigns discrete colors from a palette to unique data values.pointColorBy, pointColorPalette*
continuousCreates a smooth color gradient based on data values.pointColorBy, pointColorPalette*
mapMaps data values to specific colors from a provided object.pointColorBy, pointColorByMap
degreeColors points based on their number of connections (degree).Links data, pointColorPalette*
preciseDegreeSame as 'degree', but uses exact values without clamping outliers.Links data, pointColorPalette*
linkDirectionColors points based on link directionality (source, target, or both).Links data
directUses data values directly as colors, or via a custom function.pointColorBy with color strings, or pointColorByFn
undefinedAutomatically selects the most suitable strategy based on the data type and structure.

* Optional. If not provided, default predefined color/palette will be used.

ā„¹ļø

All strategies can be used as string values or as enums exported from Cosmograph (e.g. 'categorical' → CosmographPointColorStrategy.Categorical).

single

Exported enum: CosmographPointColorStrategy.Single

Single Point Coloring Strategy

This strategy colors all points uniformly with the color specified in pointDefaultColor.

categorical

Exported enum: CosmographPointColorStrategy.Categorical

Categorical Point Coloring Strategy

Assigns colors from the pointColorPalette to distinct values in the pointColorBy column.

  • For categorical data (strings, booleans): Each unique value is assigned a color from the palette. If there are more unique values than colors, the palette cycles.
  • For numeric data: The data range is divided into equal-width buckets, and each bucket is assigned a color from the palette.
  • null or undefined values will use the unknownColor.

continuous

Exported enum: CosmographPointColorStrategy.Continuous

Continuous Point Coloring Strategy

Interpolates colors from the pointColorPalette over the data in the pointColorBy column. This creates smooth color gradients.

  • For numeric data: Creates a continuous color scale.
  • For categorical data: Creates an evenly-spaced color scale by interpolating between palette colors to assign a unique color to every unique value.
  • null or undefined values will use the unknownColor.

map

Exported enum: CosmographPointColorStrategy.Map

Map Point Coloring Strategy

Uses a predefined map (pointColorByMap) to assign specific colors to values from the pointColorBy column. This is useful for exact color control.

degree and preciseDegree

Exported enum: CosmographPointColorStrategy.Degree and CosmographPointColorStrategy.PreciseDegree

Degree Point Coloring Strategy

Colors points based on their degree (number of connections). The colors are interpolated from the pointColorPalette.

  • 'degree': Clamps the degree values to the 5th and 95th percentiles of the total degree distribution before mapping to the color scale. This helps to reduce the effect of outliers with very high or low degrees.
  • 'preciseDegree': Uses the exact degree values without clamping.

This strategy works only when links are provided.

linkDirection

Exported enum: CosmographPointColorStrategy.LinkDirection

Link Direction Point Coloring Strategy

Colors points based on their role as a source, target, or both in links.

This strategy works only when links are provided.

direct

Exported enum: CosmographPointColorStrategy.Direct

Direct Point Coloring Strategy

This strategy uses values from the pointColorBy column directly as colors. The values should be valid color strings (e.g., hex, rgb, named colors) or an array of [r, g, b, a].

Alternatively, you can provide a pointColorByFn function to transform values into colors programmatically. This provides maximum flexibility.

If a value is not parseable as color, or can’t be transformed into color by pointColorByFn, unknownColor is used.

undefined (automatic strategy resolution)

If pointColorStrategy is not set, Cosmograph resolves it in this order of priority:

PriorityConditionResulting Strategy
1pointColorByFn is provided'direct'
2pointColorByMap is provided'map'
3pointColorBy has numeric data'continuous'
4pointColorBy has string/boolean data'categorical'
5Links are provided (and no pointColorBy)'degree'
6Fallback'single'

Link coloring works similarly to point coloring and mostly depends on the linkColorBy property.

You can explicitly set a coloring strategy using the linkColorStrategy property. If not provided, Cosmograph will choose an optimal strategy.

StrategyDescriptionKey Properties
singleColors all links with a single, uniform color.linkDefaultColor*
sumAggregates numeric values with a sum and maps it to a color.linkColorBy (numeric), linkColorPalette*
averageAggregates numeric values with an average and maps it to a color.linkColorBy (numeric), linkColorPalette*
countColors links based on the number of parallel edges between points.Links data, linkColorPalette*
categoricalAssigns discrete colors from a palette to unique data values.linkColorBy, linkColorPalette*
directUses data values directly as colors, or via a custom function.linkColorBy with color strings, or linkColorByFn
undefinedAutomatically selects the most suitable strategy based on the data type and structure.

* Optional. If not provided, default predefined color/palette will be used.

ā„¹ļø

All strategies can be used as string values or as enums exported from Cosmograph (e.g. 'categorical' → CosmographLinkColorStrategy.Categorical).

single

Exported enum: CosmographLinkColorStrategy.Single

Single Link Coloring Strategy

This is the default fallback strategy. It colors all links uniformly with the color specified in linkDefaultColor.

sum

Exported enum: CosmographLinkColorStrategy.Sum

Sum Link Coloring Strategy

This strategy is useful when you have multiple links between the same two points. It uses the sum of numeric values in linkColorBy for all links between two points. The colors are interpolated from the linkColorPalette.

This has effect only if linkColorBy provided and has numeric data.

average

Exported enum: CosmographLinkColorStrategy.Average

Average Link Coloring Strategy

This strategy uses the average of numeric values in linkColorBy for all links between two points. The colors are interpolated from the linkColorPalette.

This has effect only if linkColorBy provided and has numeric data.

count

Exported enum: CosmographLinkColorStrategy.Count

Count Link Coloring Strategy

This strategy uses the total number of links between two points to determine color. The colors are interpolated from the linkColorPalette.

linkColorBy is not required for this strategy.

categorical

Exported enum: CosmographLinkColorStrategy.Categorical

Categorical Link Coloring Strategy

Assigns colors from the linkColorPalette to distinct values in the linkColorBy column.

  • For categorical data (strings, booleans): Each unique value is assigned a color from the palette, which cycles if needed.
  • For numeric data: Divides the data into equal-width buckets, each assigned a color.

direct

Exported enum: CosmographLinkColorStrategy.Direct

Direct Link Coloring Strategy

This strategy uses values from the linkColorBy column directly as colors (values should be valid color strings or [r, g, b, a] arrays), or uses a linkColorByFn for programmatic coloring.

Alternatively, you can provide a linkColorByFn function to transform values into colors programmatically. This provides maximum flexibility.

If a value is not parseable as color, or can’t be transformed into color by linkColorByFn, unknownColor is used.

undefined (automatic strategy resolution)

If linkColorStrategy is not set, Cosmograph resolves it in this order of priority:

PriorityConditionResulting Strategy
1linkColorByFn is provided'direct'
2linkColorBy has numeric data'sum'
3linkColorBy has string/boolean data'categorical'
4Links are provided (and no linkColorBy)'count'
5Fallback'single'