Add Point On Click

This sample uses C# 12 features, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.

sample image

View model

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Kernel.Events;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView;

namespace ViewModelsSamples.Events.AddPointOnClick;

public partial class ViewModel
{
    public ObservableCollection<ObservablePoint> Points { get; set; }

    public ISeries[] SeriesCollection { get; set; }

    public ViewModel()
    {
        Points = [
            new(0, 5),
            new(3, 8),
            new(7, 9)
        ];

        SeriesCollection = [
            new LineSeries<ObservablePoint>
            {
                Values = Points,
                Fill = null,
                DataPadding = new LiveChartsCore.Drawing.LvcPoint(5, 5)
            }
        ];
    }

    [RelayCommand]
    public void PointerDown(PointerCommandArgs args)
    {
        var chart = (ICartesianChartView)args.Chart;

        // scales the UI coordinates to the corresponding data in the chart.
        var scaledPoint = chart.ScalePixelsToData(args.PointerPosition);

        // finally add the new point to the data in our chart.
        Points.Add(new ObservablePoint(scaledPoint.X, scaledPoint.Y));
    }
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiSample.Events.AddPointOnClick.View"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Maui;assembly=LiveChartsCore.SkiaSharpView.Maui"
             xmlns:vms="clr-namespace:ViewModelsSamples.Events.AddPointOnClick;assembly=ViewModelsSamples"
             >
    <ContentPage.BindingContext>
        <vms:ViewModel/>
    </ContentPage.BindingContext>
    <lvc:CartesianChart
                x:Name="chart"
                Series="{Binding SeriesCollection}"
                PressedCommand="{Binding PointerDownCommand}"
                TooltipPosition="Hidden">
    </lvc:CartesianChart>
</ContentPage>

View code behind

namespace MauiSample.Events.AddPointOnClick;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View : ContentPage
{
    public View()
    {
        InitializeComponent();
    }
}