Add Point On Click

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

This web site wraps every sample using a UserControl instance, but LiveCharts controls can be used inside any container.

sample image

View model

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;

namespace ViewModelsSamples.Events.AddPointOnClick;

public partial class ViewModel
    public ViewModel()
        var data = new ObservableCollection<ObservablePoint>
            new(0, 5),
            new(3, 8),
            new(7, 9)

        Data = data;

        SeriesCollection = new ISeries[]
            new LineSeries<ObservablePoint>
                Values = data,
                Fill = null,
                DataPadding = new LiveChartsCore.Drawing.LvcPoint(5, 5)

    public ObservableCollection<ObservablePoint> Data { get; set; }

    public ISeries[] SeriesCollection { get; set; }


<UserControl x:Class="AvaloniaSample.Events.AddPointOnClick.View"
      Series="{Binding SeriesCollection}"

View code behind

using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Avalonia;
using ViewModelsSamples.Events.AddPointOnClick;

namespace AvaloniaSample.Events.AddPointOnClick;

public class View : UserControl
    public View()

    private void ChartPointerPressed(object? sender, Avalonia.Input.PointerPressedEventArgs e)
        var chart = this.FindControl<CartesianChart>("chart");
        var viewModel = DataContext as ViewModel;

        // gets the point in the UI coordinates.
        var p = e.GetPosition(chart);

        // scales the UI coordinates to the corresponding data in the chart.
        // ScaleUIPoint returns an array of double
        var scaledPoint = chart.ScaleUIPoint(new LvcPoint((float)p.X, (float)p.Y));

        // where the X coordinate is in the first position
        var x = scaledPoint[0];

        // and the Y coordinate in the second position
        var y = scaledPoint[1];

        // finally add the new point to the data in our chart.
        viewModel?.Data.Add(new ObservablePoint(x, y));

    private void InitializeComponent()