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)

scrollbarFilter

Previous
poptip
Next
sliderFilter

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

scrollbarFilter is a default interaction that is automatically enabled when a scrollbar component is configured. It allows users to filter and browse data through scrollbars, particularly suitable for visualization scenarios with large amounts of data. Scrollbar filtering can be applied to both x-axis and y-axis, helping users focus on data regions of interest.

  • Trigger: Drag scrollbar.

  • End: Release scrollbar.

  • Effect: Update chart's displayed data range.

Usage

There are two ways to configure scrollbarFilter interaction:

First, automatically enable scrollbar filtering interaction by configuring the scrollbar component:

({
scrollbar: {
x: { ratio: 0.5 }, // x-axis scrollbar, displaying 50% of data
y: { ratio: 0.3 }, // y-axis scrollbar, displaying 30% of data
},
});

Second, configure directly in interaction:

({
scrollbar: {
x: { ratio: 0.5 },
y: { ratio: 0.3 },
},
interaction: {
scrollbarFilter: true, // Enable scrollbar filtering interaction
},
});

Configuration Level

Scrollbar filtering interaction can be configured at the View level:

({
type: 'view',
scrollbar: {
x: { ratio: 0.5 },
y: { ratio: 0.3 },
},
interaction: { scrollbarFilter: true },
});

Configuration Options

Scrollbar filtering interaction supports the following configuration options:

PropertyDescriptionTypeDefaultRequired
initDomainInitialize the data domain range, used to set initial filter range{ x: [number, number], y: [number, number] }Automatically calculated based on dataNo
classNameCSS class name for scrollbar, used for style customization and DOM selectionstring'g2-scrollbar'No
prefixEvent prefix, used to define triggered event namesstring'scrollbar'No
hasStateWhether to enable state management, controls state changes during scrollbar filteringbooleantrueNo
setValueCustom function to set scrollbar value(component, values) => void(component, values) => component.setValue(values[0])No
getInitValuesCustom function to get scrollbar initial values(scrollbar) => anyInternal default implementationNo

Complex Type Description

initDomain

initDomain is an object containing x and y properties, representing the initial data domain ranges for x-axis and y-axis respectively. This configuration option allows you to precisely control the initial data range displayed by the scrollbar.

{
initDomain: {
x: [minX, maxX], // x-axis data domain range
y: [minY, maxY], // y-axis data domain range
}
}

For example, if you want the x-axis to initially display a range from the second to fifth data points:

({
interaction: {
scrollbarFilter: {
initDomain: {
x: [1, 4], // Display data points with indices 1 to 4 (second to fifth)
},
},
},
});

setValue

setValue is a function used to customize how to set the scrollbar value. The default implementation is (component, values) => component.setValue(values[0]), which sets the first value as the current value of the scrollbar.

If you need to customize the scrollbar's value setting logic, you can provide your own implementation:

({
interaction: {
scrollbarFilter: {
setValue: (component, values) => {
// Custom value setting logic
component.setValue(values[0]);
// Additional operations can be added here
},
},
},
});

getInitValues

getInitValues is a function used to get the initial values of the scrollbar. The default implementation checks if the scrollbar value is 0, and returns that value if it's not 0.

You can customize this function to control the initial position of the scrollbar:

({
interaction: {
scrollbarFilter: {
getInitValues: (scrollbar) => {
// Custom logic to get initial values
const values = scrollbar.slider.attributes.values;
// For example, always start from the middle position
return [values.length / 2];
},
},
},
});

scrollbar Component Configuration

In addition to the scrollbarFilter interaction configuration, the scrollbar component itself has some important configuration options that affect the behavior of scrollbar filtering:

PropertyDescriptionTypeDefaultRequired
ratioRatio of displayed data, value range [0, 1]number1No
styleStyle configuration for scrollbarScrollbarStyle-No
animateWhether to enable animationbooleantrueNo

For detailed documentation see Scrollbar Component

Events

Listening to Events

Scrollbar filtering interaction supports the following events:

  • scrollbarX:filter - Triggered when x-axis scrollbar filters
  • scrollbarY:filter - Triggered when y-axis scrollbar filters
chart.on('scrollbarX:filter', (event) => {
const { data, nativeEvent } = event;
if (nativeEvent) console.log('scrollbarX:filter', data.selection);
});
chart.on('scrollbarY:filter', (event) => {
const { data, nativeEvent } = event;
if (nativeEvent) console.log('scrollbarY:filter', data.selection);
});

Triggering Interaction

You can manually trigger scrollbar filtering interaction through the following methods:

  • scrollbarX:filter - Trigger x-axis scrollbar filtering
  • scrollbarY:filter - Trigger y-axis scrollbar filtering
chart.emit('scrollbarX:filter', {
data: { selection: [['2001-03'], undefined] },
});
chart.emit('scrollbarY:filter', {
data: { selection: [undefined, [50, 550]] },
});

Examples

Basic Scrollbar Filtering

The following example shows how to add basic X-axis scrollbar filtering functionality to a column chart:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
height: 300,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
},
encode: { x: 'letter', y: 'frequency', y1: 0.000001 },
scale: { y: { type: 'log' } },
scrollbar: { x: true }, // Enable X-axis scrollbar
});
chart.render();

Listening to Scrollbar Value Changes

This example shows how to listen to the scrollbar's valuechange event to get the values before and after scrollbar sliding:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
height: 300,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
},
encode: { x: 'letter', y: 'frequency', y1: 0.000001 },
scale: { y: { type: 'log' } },
scrollbar: { x: true },
});
// After rendering the chart
chart.on('afterrender', () => {
const { canvas } = chart.getContext();
const scrollbar = canvas.document.getElementsByClassName('g2-scrollbar')[0];
if (scrollbar) {
scrollbar.addEventListener('valuechange', (e) => {
console.log('Scrollbar value changed:', e.detail);
});
}
});
chart.render();

Dual-Axis Scrollbar

This example demonstrates how to use scrollbars on both X and Y axis simultaneously:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'point',
autoFit: true,
height: 400,
data: {
type: 'fetch',
value: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/scatter.json',
},
encode: {
x: 'weight',
y: 'height',
color: 'gender',
size: 4,
},
scrollbar: {
x: { ratio: 0.3 }, // Show 30% of X-axis data
y: { ratio: 0.4 }, // Show 40% of Y-axis data
},
});
chart.render();