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)

ema

Previous
custom
Next
fetch

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

EMA (Exponential Moving Average) is a commonly used data smoothing algorithm that reduces data volatility by assigning higher weights to recent data points, making it clearer to observe trend changes in data.

In G2's implementation, EMA is calculated as follows:

EMA formula diagram

Where:

  • Pt: The raw data value at the current time;
  • EMAt-1: The EMA value at the previous time;
  • α: The smoothing factor, ranging between (0, 1).

⚠️ Note: G2's EMA implementation has the α weight position reversed from the traditional definition, therefore:

  • The closer α is to 1, the more pronounced the smoothing effect;
  • The closer α is to 0, the closer EMA is to the original data.

Use Cases

  • When data in time series has dramatic fluctuations and you want to highlight trends;
  • Technical analysis of financial data such as stock prices;
  • Smoothing and dynamic tracking of metrics during model training.

Configuration Properties

PropertyDescriptionTypeDefaultRequired
fieldName of the field to be smoothedstring'y'✓
alphaSmoothing factor, controls smoothing degree (larger values mean more smoothing)number0.6
asName of the new field to generate, if not specified will overwrite the original fieldstringSame as field

If you need to retain the original field, it's recommended to set the as property to output to a new field. This default value is defined internally by the component, not from the theme. ⚠️ Note: The field must be numeric, otherwise it will cause calculation errors.

Implementation Examples

Base Example: Stock Price Smoothing

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
children: [
{
type: 'line',
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv',
transform: [
{
type: 'ema',
field: 'close',
alpha: 0.8,
as: 'emaClose',
},
],
},
encode: {
x: 'date',
y: 'emaClose',
},
},
{
type: 'line',
style: {
opacity: 0.3,
},
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv',
},
encode: {
x: 'date',
y: 'close',
},
},
],
});
chart.render()

Example 1: Highlighting Trend Changes (Time Series)

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
children: [
{
type: 'line',
data: {
type: 'inline',
value: [
{ t: 0, y: 100 },
{ t: 1, y: 180 },
{ t: 2, y: 120 },
{ t: 3, y: 200 },
{ t: 4, y: 150 },
{ t: 5, y: 250 },
],
transform: [
{
type: 'ema',
field: 'y',
alpha: 0.6,
as: 'emaY',
},
],
},
encode: { x: 't', y: 'emaY' },
style: { stroke: '#f90' },
},
{
type: 'line',
data: {
type: 'inline',
value: [
{ t: 0, y: 100 },
{ t: 1, y: 180 },
{ t: 2, y: 120 },
{ t: 3, y: 200 },
{ t: 4, y: 150 },
{ t: 5, y: 250 },
],
},
encode: { x: 't', y: 'y' },
style: { stroke: '#ccc', lineDash: [4, 2] },
},
],
});
chart.render()

Example 2: Financial Market Trend Smoothing

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const data = Array.from({ length: 30 }, (_, i) => ({
date: `2024-01-${String(i + 1).padStart(2, '0')}`,
close:
100 + Math.sin(i / 3) * 20 + (i % 5 === 0 ? 20 : 0) + Math.random() * 10,
}));
chart.options({
type: 'view',
children: [
{
type: 'line',
data: {
type: 'inline',
value: data,
transform: [
{
type: 'ema',
field: 'close',
alpha: 0.7,
as: 'emaClose',
},
],
},
encode: {
x: 'date',
y: 'emaClose',
},
style: {
stroke: '#007aff',
lineWidth: 2,
},
},
{
type: 'line',
data: {
type: 'inline',
value: data,
},
encode: {
x: 'date',
y: 'close',
},
style: {
stroke: '#bbb',
lineDash: [4, 2],
},
},
],
});
chart.render()

Example 3: Training Process Metric Smoothing

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
children: [
{
type: 'line',
data: {
type: 'inline',
value: Array.from({ length: 50 }, (_, i) => ({
epoch: i,
loss: Math.sin(i / 5) * 20 + 60 + Math.random() * 5,
})),
transform: [
{
type: 'ema',
field: 'loss',
alpha: 0.4,
as: 'emaLoss',
},
],
},
encode: {
x: 'epoch',
y: 'emaLoss',
},
style: { stroke: '#52c41a' },
},
{
type: 'line',
data: {
type: 'inline',
value: Array.from({ length: 50 }, (_, i) => ({
epoch: i,
loss: Math.sin(i / 5) * 20 + 60 + Math.random() * 5,
})),
},
encode: {
x: 'epoch',
y: 'loss',
},
style: { stroke: '#ddd', lineDash: [4, 2] },
},
],
});
chart.render()

Interactive Demo