logo

G2

  • Docs
  • Chart Introduction
  • API
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Use In Framework
    • Experimental Spec 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
      • 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
      • pow
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
    • Transform
      • overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectX
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • overview
      • fisheye
      • parallel
      • polar
      • radial
      • theta
      • transpose
      • 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
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • Composition
      • overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • overview
      • Academy
      • classic
      • classicDark
    • event
    • Color
  • Chart API
  • Chart Component
    • 标题(Title)
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • 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

Exponential Moving Average (EMA) is a widely-used data smoothing algorithm that reduces data volatility by assigning higher weights to recent data points, enabling clearer observation of trend evolution.

In G2's implementation, the EMA calculation follows this formula:

EMA formula visualization

Where:

  • Pt: Raw data value at current time step
  • EMAt-1: EMA value from previous time step
  • α: Smoothing factor (0 < α < 1)

⚠️ Implementation Note: G2's EMA implementation reverses the traditional α weighting convention. Therefore:

  • α closer to 1 produces stronger smoothing
  • α closer to 0 makes EMA values resemble raw data

Use Cases

  1. Volatile Time Series Trend Visualization
    Ideal for revealing underlying patterns in noisy sequential data
  2. Financial Technical Analysis
    Commonly applied to stock prices, market indices, and trading volumes
  3. Training Metric Smoothing
    Stabilizes fluctuating metrics in machine learning training processes
  4. Sensor Data Filtering
    Reduces measurement noise in IoT/industrial monitoring systems

Configuration Properties

PropertyDescriptionTypeDefaultRequired
fieldTarget numerical field for smoothingstring'y'✓
alphaSmoothing intensity controller (higher = smoother)number0.6
asOutput field name (overwrites original if unspecified)stringSame as field

Best Practices

  • Preserve original data by specifying as for output field
  • Default values are component-defined, not theme-based
  • ⚠️ Input validation: field must contain numerical values

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',
},
},
],
});
return chart.render().then((chart) => chart.getContainer());

Example 1: Time Series Trend Enhancement

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] },
},
],
});
return chart.render().then((chart) => chart.getContainer());

Example 2: Financial Data 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],
},
},
],
});
return chart.render().then((chart) => chart.getContainer());

Example 3: Training Metric Stabilization

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] },
},
],
});
return chart.render().then((chart) => chart.getContainer());

Interactive Demo