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)

linear

Previous
band
Next
log

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

Linear is the base class for continuous scales. Its core function is to linearly map data from the data domain to the visual range while preserving the proportional relationships between data points. Each output value y can be expressed as a linear function of the input value x: y = mx + b.

When no scale type is explicitly declared, G2 applies the linear scale by default to numeric fields (such as temperature, sales).

Usage

Linear scales are commonly used to map data to normalized coordinates. In this example, the scale for the y channel is configured to affect how graphics are positioned on the canvas.

chart
.line()
.encode('x', 'year')
.encode('y', 'sale')
.scale('y', {
type: 'linear',
range: [0.2, 0.8],
/* other configuration options */
});

It can be used not only with continuous numeric data types but also when proportional relationships need to be maintained.

chart
.interval()
.data([
{ time: '2023-01', sales: '100' },
{ time: '2023-01', sales: '300' },
])
.encode('x', 'time')
.encode('y', 'sales')
.scale('y', {
type: 'linear', // When sales values are strings, they may be incorrectly inferred as categorical data, requiring explicit setting
});

Configuration Levels

Scales can be configured at the Mark level:

({
type: 'line',
scale: {
x: { padding: 0.5 },
y: {
type: 'linear', // Specify type
domain: [10, 100], // Specify domain
range: [0, 1], // Specify range
},
},
});

They can also be configured at the View level:

({
type: 'view',
scale: {
x: { padding: 0.5 },
y: {
type: 'linear', // Specify type
domain: [10, 100], // Specify domain
range: [0, 1], // Specify range
},
},
});

Configuration Options

PropertyDescriptionTypeDefault Value
domainSet the domain range of the datanumber[]Min-max range of input data
domainMinSet the minimum value of the data domainnumberMinimum value of input data
domainMaxSet the maximum value of the data domainnumberMaximum value of input data
rangeSet the range of data mappingnumber[] | string[][0, 1]
rangeMinSet the minimum value of the data mapping rangenumber | string0
rangeMaxSet the maximum value of the data mapping rangenumber | string1
unknownReturn value for undefined, NaN, null valuesanyundefined
tickCountSet the recommended number of ticks to generate; tickCount is only a suggestionnumber5
tickMethodSet the method for generating ticks, commonly used for custom ticks(min: number, max: number, count: number) => number[]d3-ticks
roundRound output valuesbooleanfalse
clampLimit mapped values to the rangebooleanfalse
niceExtend domain range to make tick display more friendlybooleanfalse
interpolateCustom interpolation function(a: number, b: number) => (t: number) => T(a, b) => (t) => a * (1 - t) + b * t

Example

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const data = [
{ time: '2023-01', sales: 100 },
{ time: '2023-02', sales: 200 },
{ time: '2023-03', sales: 150 },
{ time: '2023-04', sales: 300 },
{ time: '2023-05', sales: 400 },
];
chart
.interval()
.data(data)
.encode('x', 'time')
.encode('y', 'sales')
.scale('y', {
type: 'linear', // Use linear scale
nice: true,
domain: [0, 300], // Custom scale range setting, needs to be explicitly set
});
chart.render();

FAQ

  • How to customize y-axis ticks?

For example, if you only need to display 0, 100, 600 on the ticks, set the y scale as follows:

chart
.line()
// ...
.scale('y', {
type: 'linear',
domain: [0, 700],
tickMethod: () => [0, 100, 600],
});