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 : ObservableObject
{
    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; }
}

XAML

<UserControl x:Class="WPFSample.Events.AddPointOnClick.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
             xmlns:vms="clr-namespace:ViewModelsSamples.Events.AddPointOnClick;assembly=ViewModelsSamples">
    <UserControl.DataContext>
        <vms:ViewModel/>
    </UserControl.DataContext>
    <lvc:CartesianChart
        x:Name="chart"
        Series="{Binding SeriesCollection}"
        MouseDown="Chart_MouseDown"
        TooltipPosition="Hidden">
    </lvc:CartesianChart>
</UserControl>

View code behind

using System.Windows.Controls;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.WPF;
using ViewModelsSamples.Events.AddPointOnClick;

namespace WPFSample.Events.AddPointOnClick;

/// <summary>
/// Interaction logic for View.xaml
/// </summary>
public partial class View : UserControl
{
    public View()
    {
        InitializeComponent();
    }

    private void Chart_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        var chart = (CartesianChart)FindName("chart");
        var viewModel = (ViewModel)DataContext;

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

        // scales the UI coordinates to the corresponding data in the chart.
        var scaledPoint = chart.ScalePixelsToData(new LvcPointD(p.X, p.Y));

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

        // You can also get all the points or visual elements in a given location.
        var points = chart.GetPointsAt(new LvcPoint((float)p.X, (float)p.Y));
        var visuals = chart.GetVisualsAt(new LvcPoint((float)p.X, (float)p.Y));
    }
}