logo

G2

  • Chart Gallery
  • Docs
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Using in Frontend Frameworks
    • Experimental Spec API
  • Chart API
  • Core Concepts
    • Chart
      • Components of G2 Charts
      • How to Use Charts
    • Mark
      • Overview
      • area
      • box
      • boxplot
      • cell
      • chord
      • density
      • gauge
      • heatmap
      • image
      • interval
      • line
      • lineX
      • lineY
      • link
      • liquid
      • sunburst
      • point
      • polygon
      • range
      • rangeX
      • rangeY
      • rect
      • shape
      • text
      • vector
      • connector
      • wordCloud
    • View
    • Data
      • Overview
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • Encode
    • Scale
      • Overview
      • band
      • linear
      • log
      • ordinal
      • point
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • Transform
      • Overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectY
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • Overview
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • cartesian3D
      • helix
    • Style
    • Animate
      • Overview
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • State
    • Interaction
      • Overview
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
      • fisheye
    • Composition
      • Overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • Overview
      • academy
      • classic
      • classicDark
    • Events
    • Color Mapping
  • Chart Component
    • Title
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Data Label
  • Extra Topics
    • Graph
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • Geo
      • geoPath
      • geoView
    • 3D
      • Draw 3D Chart
      • point3D
      • line3D
      • interval3D
      • surface3D
    • Plugin
      • renderer
      • rough
      • lottie
      • a11y
    • Package on demand
    • Set pattern
    • Server-Side Rendering (SSR)
    • Spec Function Expression Support (Available in 5.3.0)
  • Whats New
    • New Version Features
    • Migration from v4 to v5
  • Frequently Asked Questions (FAQ)

density

Previous
chord
Next
gauge

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

density is a chart type used to visualize the probability density distribution of continuous variables. Through kernel density estimation (Kernel Density Estimation, KDE), it transforms discrete data points into smooth continuous probability density curves, intuitively reflecting data concentration trends, distribution patterns, and outliers.

The core goal of density is to extract overall distribution patterns from scatter or point cloud data and map density information to a continuous area chart or gradient effect. This chart type is commonly used to analyze aggregation areas of large amounts of data points, data concentration hotspots, or probability density of data distribution, such as representing population density in geographic visualization, or showing regional trading frequency in analysis.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'density', // Set chart type as density plot
data: {
type: 'fetch', // Specify data type as network fetch
value: 'https://assets.antv.antgroup.com/g2/species.json', // Set data URL address
transform: [
{
type: 'kde', // Use Kernel Density Estimation (KDE) for data transformation
field: 'y', // Specify KDE calculation field as 'y'
groupBy: ['x', 'species'], // Group data by 'x' and 'species' fields
},
],
},
encode: {
x: 'x', // Map 'x' field to x-axis
y: 'y', // Map 'y' field to y-axis
color: 'species', // Map 'species' field to color
size: 'size', // Map 'size' field to graphic size
series: 'species', // Map 'species' field to series
},
tooltip: false, // Disable chart tooltip functionality
});
chart.render();

For more examples, please check the Chart Examples - Violin Plot page.

Configuration Options

PropertyDescriptionTypeDefault ValueRequired
coordinateConfigure density chart coordinate system, supports properties: cartesian | polarcoordinate'cartesian'
encodeConfigure density mark visual channels, including x, y, color, size, etc., used to specify relationships between visual element properties and dataencode-✓
scaleConfigure density mark graphic scaling, including x, y, series, size, etc.scale-
styleConfigure density graphic stylestyle-

coordinate

coordinate is the core system in data visualization that defines how data maps to graphic space. It determines how data transforms from "numerical domain" to "visual position". Coordinate systems reshape the visual expression of density distribution through different spatial transformation rules.

Coordinate SystemTypeChart
Cartesian'cartesian'Density plot, etc.
Polar'polar'Polar violin plot, etc.

(() => {
const coordinateMap = [
{
coordinate: 'cartesian',
label: 'Cartesian Coordinate System',
},
{
coordinate: 'polar',
label: 'Polar Coordinate System',
},
];
const chart = new G2.Chart();
chart.options({
type: 'density',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/species.json',
transform: [
{
type: 'kde',
field: 'y',
groupBy: ['x', 'species'],
},
],
},
encode: {
x: 'x',
y: 'y',
color: 'species',
size: 'size',
series: 'species',
},
coordinate: { type: coordinateMap[0].coordinate },
tooltip: false,
});
const handleSetCoordinate = (coordinate) => {
// Set selected coordinate system
chart.coordinate({
type: coordinate,
});
chart.render(); // Re-render chart
};
// Insert Encode-Color selector
const selectorContainer = document.createElement('div');
selectorContainer.textContent = 'Select Coordinate System ';
const selector = document.createElement('select');
selector.innerHTML = coordinateMap.map(
(coordinate, index) =>
`<option value="${coordinate.coordinate}" ${
index === 0 ? 'selected' : ''
}>${coordinate.label}</option>`,
);
selector.onchange = (e) => {
handleSetCoordinate(e.target.value);
};
selectorContainer.appendChild(selector);
const node = chart.getContainer();
node.insertBefore(selectorContainer, node.childNodes[0]);
chart.render();
return node;
})();

For more coordinate configuration, please check the coordinate introduction page.

encode

Configure density mark visual channels, an important configuration that defines the mapping relationships between data fields and visual properties, determining how data transforms into visual representation.

PropertyDescriptionTypeDefault ValueRequired
xBind the x property channel of density mark, usually a time or ordered categorical field in dataencode-✓
yBind the y property channel of density mark, usually a numerical or array field in dataencode-✓
colorBind the color property channel of density mark. If a data field is mapped to the color channel, data will be grouped and split into multiple areas with different colorsencode-
sizeBind the size property channel of density mark, used to map data fields to the size property of graphic elements, enhancing data distribution comparison dimensions and information density by adjusting density curve line thickness or filled area visual proportionsencode-✓
seriesBind the series property channel of density mark, used to map data grouping fields to chart series or categories, classifying data according to a field and generating independent graphic representations for each categoryencode-✓

For more encode configuration, please check the encode introduction page.

scale

scale is used to define how data maps to visual properties (such as color, size, shape, etc.). In density usage scenarios, the common role of scale is to provide mapping rules for each visual channel (such as color, size, position, etc.), enabling data points to be accurately presented.

PropertyDescriptionTypeDefault ValueRequired
xDefine mapping rules from data fields to X-axis visual positionsscale{type: 'band'}
seriesControl mapping rules from categorical fields (series encoding) to visual properties (such as color, line type, symbols)scale{type: 'band'}
sizeMap data fields to size properties of visual elements (such as density curve width, point area, or region height)scale{type: 'identity'}

For more scale configuration, please check the scale introduction page.

style

style is used to set the appearance style of chart elements, including fill color, border style, shadow effects, etc.

PropertyDescriptionTypeDefault ValueRequired
fillFill color of the graphicstring | Function<string>-
fillOpacityFill opacity of the graphicnumber | Function<number>-
strokeStroke of the graphicstring | Function<string>-
strokeOpacityStroke opacitynumber | Function<number>-
lineWidthWidth of graphic strokenumber | Function<number>-
lineDashDashed line configuration for stroke. The first value is the length of each dash segment, the second value is the distance between segments. Setting lineDash to [0, 0] results in no stroke.[number,number] | Function<[number, number]>-
opacityOverall opacity of the graphicnumber | Function<number>-
shadowColorShadow color of the graphicstring | Function<string>-
shadowBlurGaussian blur coefficient of graphic shadownumber | Function<number>-
shadowOffsetXSet horizontal distance of shadow from graphicnumber | Function<number>-
shadowOffsetYSet vertical distance of shadow from graphicnumber | Function<number>-
cursorMouse cursor style. Same as CSS cursor style, default 'default'.string | Function<string>'default'

For more style configuration, please check the style introduction page.

Try it out: