Zooming And Panning

The [ObservableObject], [ObservableProperty] and [RelayCommand] attributes come from the CommunityToolkit.Mvvm package, you can read more about it here.

sample image

Zooming and panning is disabled by default, you can enable it by setting the ZoomMode property, this property is of type ZoomAndPanMode and the options are:

The ZoomAndPanMode type is a flag enum, so you can combine the options, for example, if you want to enable zooming on the X axis and panning on the Y axis you can set the ZoomMode property to ZoomAndPanMode.ZoomX | ZoomAndPanMode.PanY.

There is also the InvertPanningPointerTrigger flag, when this flag is present the panning will be triggered using the right click on desktop devices and the touch-and-hold gesture on touch devices, the zoom by section feature will be triggered to the left click on desktop devices and the touch-and-hold gesture on touch devices.

You can learn more about zooming an panning here.

In desktop, use the mouse wheel to zoom in/out, hold click and drag to move the view (panning).

View model

using System;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;

namespace ViewModelsSamples.Lines.Zoom;

public partial class ViewModel : ObservableObject
{
    public ViewModel()
    {
        var values = new int[100];
        var r = new Random();
        var t = 0;

        for (var i = 0; i < 100; i++)
        {
            t += r.Next(-90, 100);
            values[i] = t;
        }

        SeriesCollection = new ISeries[] { new LineSeries<int> { Values = values } };
    }

    public ISeries[] SeriesCollection { get; set; }
}

XAML

<UserControl x:Class="AvaloniaSample.Lines.Zoom.View"
             xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia"
             xmlns:vms="using:ViewModelsSamples.Lines.Zoom">
  <UserControl.DataContext>
    <vms:ViewModel/>
  </UserControl.DataContext>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="50"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
      
    <TextBlock Text="Use the mouse wheel to zoom in/out, click hold and drag to move panning"/>

    <lvc:CartesianChart
        Grid.Row="1"
        Series="{Binding SeriesCollection}"
        ZoomMode="X"> <!-- mark -->
    </lvc:CartesianChart>
  </Grid>
</UserControl>

Articles you might also find useful: