Add Point On Click

sample image

View model

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

namespace ViewModelsSamples.Events.AddPointOnClick;

public partial class ViewModel : ObservableObject
{
    public ISeries[] SeriesCollection { get; set; } =
        new ISeries[]
        {
            new LineSeries<ObservablePoint>
            {
                Values = new ObservableCollection<ObservablePoint>
                {
                    new(0, 5),
                    new(3, 8),
                    new(7, 9)
                },
                Fill = null,
                DataPadding = new LiveChartsCore.Drawing.LvcPoint(5, 5)
            }
        };

    [RelayCommand]
    public void PointerDown(PointerCommandArgs args)
    {
        var chart = (ICartesianChartView<SkiaSharpDrawingContext>)args.Chart;
        var values = (ObservableCollection<ObservablePoint>)SeriesCollection[0].Values!;

        // 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.
        values.Add(new ObservablePoint(scaledPoint.X, scaledPoint.Y));
    }
}

Form code behind

// NOTE: // mark
// BECAUSE THIS VIEWMODEL IS SHARED WITH OTHER VIEWS // mark
// THE _viewModel.ChartUpdated, _viewModel.PointerDown and _viewModel.PointerUp METHODS // mark
// are repeated in WindowsForms, WindowsForms do not support Command binding, please // mark
// ignore the viewmodel RelayCommands and use the events instead. // mark

using System.Collections.ObjectModel;
using System.Windows.Forms;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.WinForms;
using ViewModelsSamples.Events.AddPointOnClick;

namespace WinFormsSample.Events.AddPointOnClick;

public partial class View : UserControl
{
    private readonly ObservableCollection<ObservablePoint> _data;

    /// <summary>
    /// Initializes a new instance of the <see cref="View"/> class.
    /// </summary>
    public View()
    {
        InitializeComponent();
        Size = new System.Drawing.Size(50, 50);

        var viewModel = new ViewModel();

        _data = (ObservableCollection<ObservablePoint>)viewModel.SeriesCollection[0].Values;

        var cartesianChart = new CartesianChart
        {
            Series = viewModel.SeriesCollection,

            // out of livecharts properties...
            Location = new System.Drawing.Point(0, 0),
            Size = new System.Drawing.Size(50, 50),
            Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom
        };

        cartesianChart.MouseDown += CartesianChart_Click;

        Controls.Add(cartesianChart);
    }

    private void CartesianChart_Click(object sender, MouseEventArgs e)
    {
        var chart = (CartesianChart)sender;

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

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

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