logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.47
  • Introduction
  • Data
  • Getting Started
    • Quick Start
    • Installation
    • Integration
      • react
      • vue
      • angular
    • Step-by-step guide
  • Graph
    • Extensions En
    • Graph
    • Options
    • extension
  • Element
    • Element Overview
    • Element State
    • Node
      • Node Overview
      • Build-in Node
        • Common Node Configurations
        • Diamond
        • Donut
        • Ellipse
        • Hexagon
        • Html
        • Image
        • Rect
        • Star
        • Triangle
        • Circle
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Build-in Edge
        • Common Edge Configurations
        • Cubic Bezier Curve
        • CubicHorizontal Bezier Curve
        • CubicVertical Bezier Curve
        • Line
        • Polyline
        • Quadratic Bezier Curve
      • Custom Edge
    • Combo
      • Combo Overview
      • Build-in Combo
        • Circle
        • Combo Configuration Options
        • Rect
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Build-in Layout
      • 3D Force-Directed Layout
      • AntvDagre Layout
      • Circular Layout
      • ComboCombined Layout
      • Common Layout Configuration Options
      • CompactBox
      • Concentric Layout
      • D3 Force-Directed Layout
      • Dagre Layout
      • Dendrogram Layout
      • Fishbone Layout
      • Force Force-directed Layout
      • ForceAtlas2 Force-directed Layout
      • Fruchterman Force-directed Layout
      • Grid Layout
      • Indented Tree
      • MDS High-dimensional Data Dimensionality Reduction Layout
      • Mindmap Tree
      • Radial Layout
      • Random Layout
      • Snake Layout
    • Custom Layout
  • Behavior
    • Behavior Overview
    • Build-in Behavior
      • AutoAdaptLabel
      • BrushSelect
      • ClickSelect
      • CollapseExpand
      • CreateEdge
      • DragCanvas
      • DragElement
      • DragElementForce
      • FixElementSize
      • FocusElement
      • HoverActivate
      • LassoSelect
      • OptimizeViewportTransform
      • ScrollCanvas
      • ZoomCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Build-in Plugin
      • Background
      • BubbleSets
      • Contextmenu
      • EdgeBundling
      • EdgeFilterLens
      • Fisheye
      • Fullscreen
      • GridLine
      • History
      • Hull
      • Legend
      • Minimap
      • Snapline
      • Timebar
      • Toolbar
      • Tooltip
      • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • Build-in Transform
      • MapNodeSize
      • PlaceRadialLabels
      • ProcessParallelEdges
    • Custom Transform
  • Theme
    • Theme Overview
    • Custom Theme
    • Palette
    • Custom Palette
  • Animation
    • Animation Overview
    • Custom Animation
  • Further Reading
    • Event
    • renderer
    • coordinate
    • download-image
    • Using Iconfont
    • Use 3D
    • Bundle Project
  • What's new
    • Feature
    • Upgrade To 5.0
  • FAQ
  • contribute

Palette

Previous
Custom Theme
Next
Custom Palette

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

A palette refers to a set of predefined color collections that help users more conveniently select colors. In G6, a palette is a common option that allows users to configure the colors of elements such as nodes, edges, and links through the palette.

Palettes are divided into two types: discrete palette and continuous palette.

A discrete palette is an array of colors used to map discrete values within elements to different colors, such as the type of nodes, the relationship of edges, etc. Below is a simple example of a discrete palette:

['#5B8FF9', '#61DDAA', '#F6BD16', '#F6903D', '#F08BB4'];

A continuous palette is an interpolator that takes a value between 0 and 1 and returns the corresponding color. It is used to map continuous values within elements to different colors, such as the degree of nodes, the weight of edges, etc. Below is a simple example of a continuous palette:

(value: number) => `rgb(${value * 255}, 0, 0)`;

Register Palette

You can directly use the built-in palettes, but if you want to use other palettes, you need to register them first:

import { register, ExtensionCategory } from '@antv/g6';
import { CustomPalette } from 'package-name/or/path-to-your-custom-palette';
register(ExtensionCategory.PALETTE, 'custom-palette', CustomPalette);

note

During the process of registering a palette, there is no distinction made between discrete and continuous palettes. It is necessary to ensure the consistency between the palette type and the data type when using the palette.

Built-in Palettes

Currently, G6 has 5 sets of commonly used discrete palettes that users can directly utilize:

  • spectral
  • tableau
  • oranges
  • greens
  • blues

Configure Palette

Currently, the configuration of palettes is mainly focused on elements, taking nodes as an example:

Discrete Palette

  1. Default Configuration: By directly setting the value of palette to the name of the palette, each node will be assigned a different color by default
{
node: {
palette: 'spectral', // spectral is the Palette Name
}
}

createGraph(
{
data: {
nodes: new Array(30).fill(0).map((_, i) => ({ id: `node-${i}` })),
},
layout: { type: 'grid', cols: 10, rows: 3 },
node: {
palette: 'spectral',
},
},
{ width: 400, height: 100 },
);

When the number of elements exceeds the number of colors in the palette, the colors in the palette will be reused in a cyclic manner.

  1. Standard Configuration: The attributes for configuring a discrete palette include: type: 'group', field, color, invert.

Among them, type: 'group' explicitly specifies that the current palette type is a discrete palette; field designates the field for grouping in the element data; color is the name of the palette; invert indicates whether to invert the palette.

Given a set of example data:

{
"nodes": [
{ "id": "node-1", "data": { "category": "A" } },
{ "id": "node-2", "data": { "category": "B" } },
{ "id": "node-3", "data": { "category": "C" } },
{ "id": "node-4", "data": { "category": "A" } },
{ "id": "node-5", "data": { "category": "B" } },
{ "id": "node-6", "data": { "category": "C" } }
]
}

In the data, node-1 and node-4 belong to category A, node-2 and node-5 belong to category B, node-3 and node-6 belong to category C.

Configure the color of the nodes in such a way that nodes of the same category have the same color:

{
node: {
palette: {
type: 'group', // Specify the palette type as a categorical palette.
field: 'category', // Specify the grouping field in the data.
color: 'tableau', // Use a Tableau-like palette.
}
}
}

createGraph(
{
data: {
nodes: new Array(6).fill(0).map((_, i) => ({ id: `node-${i}`, data: { category: ['A', 'B', 'C'][i % 3] } })),
},
layout: { type: 'grid', cols: 6 },
node: {
palette: {
type: 'group',
field: 'category',
color: 'tableau',
},
},
},
{ width: 200, height: 50 },
);

Continuous Palette

A continuous palette only supports standard configuration methods, with configuration properties including: type: 'value', field, color, invert.

Given a set of example data:

{
"nodes": [
{ "id": "node-1", "data": { "value": 0 } },
{ "id": "node-2", "data": { "value": 20 } },
{ "id": "node-3", "data": { "value": 40 } },
{ "id": "node-4", "data": { "value": 60 } },
{ "id": "node-5", "data": { "value": 80 } },
{ "id": "node-6", "data": { "value": 100 } }
]
}

Now, create an interpolator that maps the maximum value to red (rgb(255, 0, 0)) and the minimum value to black (rgb(0, 0, 0)):

(value) => `rgb(${value * 255}, 0, 0)`;

Configure the following so that the color of the nodes is mapped to different colors based on the value of the value field in the data:

{
node: {
palette: {
type: 'value', // Specify the palette type as a continuous palette
field: 'value', // Specify the numerical field in the data
color: (value) => `rgb(${value * 255}, 0, 0)`, // Use an interpolator
}
}
}

createGraph(
{
data: {
nodes: new Array(6).fill(0).map((_, i) => ({ id: `node-${i}`, data: { value: (i + 1) * 20 } })),
},
layout: { type: 'grid', cols: 6 },
node: {
palette: {
type: 'value',
field: 'value',
color: (value) => `rgb(${value * 255}, 0, 0)`,
},
},
},
{ width: 200, height: 50 },
);

note

The built-in continuous palette does not support specifying a value range. If there is a need for more complex color mapping, it can be customized within the style mapping.

Custom Palette

If the built-in palette does not meet your requirements, you can customize the palette. For details, please refer to Custom Palette.

Priority

The palette generates styles based on the type of element. For nodes and combos, the color is mapped to the fill attribute; for edges, the color is mapped to the stroke attribute.

If both a palette and a style mapping are configured, the style mapping will override the palette colors. In the following example, the color of the nodes is always red:

{
node: {
style: {
fill: 'red',
},
palette: 'spectral',
}
}