Animations

Every chart animates its changes: points move to their new positions, new points grow in, removed points shrink out and axis labels fade in and out as the range changes. Two properties control it: AnimationsSpeed, how long an animation takes, and EasingFunction, the curve it follows. The chart sets them for everything it draws, and each series and axis can override them. This article covers both properties, how to disable or delay animations, how the animated properties work and the UpdaterThrottler, which sets how often the chart updates.

Every property is listed in the API explorer: CartesianChart, EasingFunctions, Animation and Animatable.

What animates

Adding or removing items from an ObservableCollection is enough to animate the chart. A new column grows from zero, a removed one shrinks back to zero and is removed when its animation ends, and the axis moves to the new range:

real time

When zooming or panning, the labels that leave the axis range fade out and the new ones fade in:

zooming

Moving to a new place

Points that are objects, an ObservableValue or any other IChartEntity, keep their visual when Values is replaced with the same objects in another order, so each one animates from where it was to its new place. This sample adds a random amount to every pilot every 100 milliseconds and replaces Values with the same pilots sorted by their new value, so each bar slides to its new row. Each pilot is an ObservableValue with a name and a paint: the data labels show the name, and PointMeasured paints the bar with the pilot's color.

Racing bars: Rows that sort themselves as the values change.

Animations speed

AnimationsSpeed is a TimeSpan, 800 milliseconds by default. As a reference, 1 second is a slow animation and 100 milliseconds a fast one.

In XAML a TimeSpan is written as hh:mm:ss, the seconds can have decimals:

<!-- 100 milliseconds -->
<lvc:CartesianChart
    Series="{Binding Series}"
    AnimationsSpeed="00:00:00.100"><!-- mark -->
</lvc:CartesianChart>

<!-- 1 second -->
<lvc:CartesianChart
    Series="{Binding Series}"
    AnimationsSpeed="00:00:01"><!-- mark -->
</lvc:CartesianChart>

To change the speed or the easing function of every chart in the app, set them in a theme rule (HasRuleForChart); the rule applies to every chart that does not set them, see the themes article.

EasingFunction property

An easing function defines how an animation moves from start to end: at a constant pace, slowing down near the end, bouncing, and so on. EasingFunction is a Func<float, float>, EasingFunctions.ExponentialOut by default. The LiveChartsCore.EasingFunctions class contains the common curves:

  • BackIn, BounceIn, CircleIn, CubicIn, ElasticIn, ExponentialIn, PolinominalIn, QuadraticIn and SinIn, and the Out and InOut version of each one, such as BounceOut and BounceInOut.
  • Ease, EaseIn, EaseOut and EaseInOut, the cubic Bézier curves CSS uses with the same names.
  • Lineal, a constant pace.

This chart uses the BounceOut curve:

public class ViewModel
{
    public Func<float, float> Easing { get; set; } = EasingFunctions.BounceOut; // mark

    public ISeries[] Series { get; set; } = [ /* ... */ ];
}
<lvc:CartesianChart
    Series="{Binding Series}"
    EasingFunction="{Binding Easing}"><!-- mark -->
</lvc:CartesianChart>

bounce

Build your own curve

The function takes the time of the animation, from 0 (start) to 1 (end), and returns the progress, where 0 is the initial value and 1 the final value. Any function works:

chart.EasingFunction = time =>
{
    var progress = time * time; // quadratic curve

    // a constant pace would be:
    // var progress = time;

    // a sin out curve:
    // var progress = (float)Math.Sin(time * Math.PI / 2d);

    return progress;
};

EasingFunctions also has builders that return a curve with your own parameters, based on the d3-ease library:

// back curves, with the overshoot (1.70158 in BackOut)
chart.EasingFunction = EasingFunctions.BuildCustomBackOut(2f);

// elastic curves, with the amplitude and the period
chart.EasingFunction = EasingFunctions.BuildCustomElasticOut(1.5f, 0.60f);

// polynomial curves, with the exponent
chart.EasingFunction = EasingFunctions.BuildCustomPolinominalOut(4f);

// a cubic Bézier curve, with its two control points (x1, y1, x2, y2)
chart.EasingFunction = EasingFunctions.BuildCubicBezier(0.68f, -0.55f, 0.27f, 1.55f);

BuildFunctionUsingKeyFrames joins several segments. Each KeyFrame has a Time and a Value, both from 0 to 1, and the EasingFunction used to reach it from the previous frame, Lineal by default. It needs at least two frames; when the last one is before time 1, the curve keeps its value until the end.

chart.EasingFunction = EasingFunctions.BuildFunctionUsingKeyFrames(new KeyFrame[]
{
    new KeyFrame { Time = 0, Value = 0 },
    new KeyFrame { Time = 0.5f, Value = 0.8f, EasingFunction = EasingFunctions.CubicOut },
    new KeyFrame { Time = 1, Value = 1, EasingFunction = EasingFunctions.BounceOut }
});

Series and axes

Series and axes also have AnimationsSpeed and EasingFunction. Both are null by default, which means the element uses the value of the chart. Set one of them to override only that value: the other one still comes from the chart.

Series = new ISeries[]
{
    new ColumnSeries<int>
    {
        Values = new[] { 2, 5, 4 },
        AnimationsSpeed = TimeSpan.FromMilliseconds(1500), // mark
        EasingFunction = EasingFunctions.BounceOut // mark
    },
    new LineSeries<int>
    {
        Values = new[] { 3, 1, 6 } // uses the speed and the curve of the chart
    }
};

XAxes = new Axis[]
{
    new Axis
    {
        AnimationsSpeed = TimeSpan.FromMilliseconds(300) // mark
    }
};

The default tooltip and legend have their own settings: an AnimationsSpeed of 150 milliseconds and the EaseOut curve in their Easing property.

Disable animations

Setting the EasingFunction of the chart to null disables animations: every change is drawn at its final value. An AnimationsSpeed of TimeSpan.Zero does the same.

<lvc:CartesianChart
    Series="{Binding Series}"
    EasingFunction="{x:Null}"><!-- mark -->
</lvc:CartesianChart>

On a series or an axis, null means "use the value of the chart", so to disable the animations of only one series or axis, set its AnimationsSpeed to TimeSpan.Zero:

new LineSeries<int>
{
    Values = new[] { 3, 1, 6 },
    AnimationsSpeed = TimeSpan.Zero // mark
}

Charts rendered to images, such as SKCartesianChart, have animations disabled, see charts as images.

Delay each point

The PointMeasured event (a command in the XAML samples) runs every time a point gets its position and size, and gives access to the shape drawn for it in point.Context.Visual. SetTransition replaces the animation of that shape. In this sample each column gets its own Animation: the easing function returns 0 until a delay that grows with the index of the point, then follows BuildCustomElasticOut(1.5f, 0.60f), so the columns on the right start moving later.

Delayed animation: Each bar starts a moment after the previous one.

How animated properties work

Everything the chart draws (geometries, labels and paints) derives from Animatable, and its animated properties are motion properties: setting one does not change the value at once. The property stores where it is now, the new value and the time, and every time the chart draws a frame, reading the property returns the value at that moment of the animation: the start value plus the progress returned by the easing function times the distance to the new value. When a value changes in the middle of an animation, the next animation starts from where the property is, so the shapes do not jump.

A property returns its final value at once when it has no Animation, when the easing function is null or when the duration is 0. That is how disabling animations works.

The animated properties of a geometry include X, Y, Opacity, StrokeThickness, the transforms (TranslateTransform, RotateTransform, ScaleTransform, SkewTransform and TransformOrigin) and DropShadow, plus Width and Height in shapes with a size. The Color of a SolidColorPaint is also an animated property. Numbers, points, sizes, colors, paddings, shadows and arrays of numbers move smoothly; a property that holds a paint switches to the new paint at once.

Each animated property has a static PropertyDefinition named after it, such as DrawnGeometry.XProperty and BoundedDrawnGeometry.HeightProperty. Use them to change the animation of only some properties:

var animation = new Animation(EasingFunctions.BounceOut, TimeSpan.FromMilliseconds(500));

// only X and Y use this animation
visual.SetTransition(animation, DrawnGeometry.XProperty, DrawnGeometry.YProperty);

// the width and the height change at once
visual.RemoveTransition(BoundedDrawnGeometry.WidthProperty, BoundedDrawnGeometry.HeightProperty);

// with no properties, the call applies to all of them
visual.SetTransition(animation);

// jumps to the end of the running animations
visual.CompleteTransition();

The Animate(animation) extension method calls SetTransition and then CompleteTransition: any running animation jumps to its end, and the next changes use the new animation.

The library declares these properties with the [MotionProperty] attribute on a partial property, and a source generator writes the code that animates them. Your own geometries can do the same: reference the LiveChartsGenerators NuGet package, derive from a geometry class and mark the class and the property as partial.

public partial class MotionGeometry : BoundedDrawnGeometry, IDrawnElement<SkiaSharpDrawingContext>
{
    [MotionProperty] // mark
    public partial float Diameter { get; set; } // mark

    public void Draw(SkiaSharpDrawingContext context) =>
        context.Canvas.DrawCircle(X, Y, Diameter, context.ActiveSkiaPaint);
}

The custom drawing article shows how to add a geometry like this one to a chart.

Performance

While an animation runs, the chart draws a new frame on every tick of its render loop. When every property has reached its final value, it stops drawing until the next change. Disabling animations removes those intermediate frames, but the chart still measures the series and the axes on every update, with or without animations. When the data changes very often, reducing how many updates the chart runs with the UpdaterThrottler cuts that work.

UpdaterThrottler property

UpdaterThrottler is a TimeSpan, 50 milliseconds by default. When a change arrives, the chart waits that time before it updates, and the changes that arrive while it waits do not start new updates: the chart draws all of them in one update, with the data as it is when the wait ends.

For example, this view model removes the first point and adds a new one:

public class ViewModel
{
    private int index = 0;
    private Random random = new Random();
    private ObservableCollection<ObservablePoint> observableValues;

    public ViewModel()
    {
        observableValues = new ObservableCollection<ObservablePoint>();

        Series = new ObservableCollection<ISeries>
        {
            new ColumnSeries<ObservablePoint> { Values = observableValues }
        };
    }

    public ObservableCollection<ISeries> Series { get; set; }

    public void AddRandomItem()
    {
        var randomValue = random.Next(1, 10);
        observableValues.Add(new ObservablePoint(index++, randomValue));
    }

    public void RemoveFirstItem()
    {
        observableValues.RemoveAt(0);
    }
}

and the app calls it every 50 milliseconds:

// where vm is an instance of the previous ViewModel class.

while (true)
{
    vm.RemoveFirstItem();
    vm.AddRandomItem();
    await Task.Delay(50);
}

With the default throttler and animations of 300 milliseconds, the chart updates about every 50 milliseconds and the columns never stop moving, which makes the chart hard to read:

fast

With an UpdaterThrottler of 500 milliseconds, the data still changes every 50 milliseconds, but the chart updates about twice per second:

<lvc:CartesianChart
    Series="{Binding Series}"
    AnimationsSpeed="00:00:00.300"
    UpdaterThrottler="00:00:00.500"><!-- mark -->
</lvc:CartesianChart>

throttled

Try different values of both properties to find what reads best for your data. To change the default for every chart, call LiveCharts.Configure(config => config.WithUpdateThrottlingTimeout(TimeSpan.FromMilliseconds(500))) when the app starts, before any chart is created.

~/overview/1.9.animations.md