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 ContentPage
instance, but LiveCharts controls can be used inside any container.

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
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinSample.Events.AddPointOnClick.View"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Xamarin.Forms;assembly=LiveChartsCore.SkiaSharpView.XamarinForms"
xmlns:vms="clr-namespace:ViewModelsSamples.Events.AddPointOnClick;assembly=ViewModelsSamples">
<ContentPage.BindingContext>
<vms:ViewModel/>
</ContentPage.BindingContext>
<lvc:CartesianChart
x:Name="chart"
Series="{Binding SeriesCollection}"
Touched="Chart_Touched"
TooltipPosition="Hidden">
</lvc:CartesianChart>
</ContentPage>
View code behind
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Xamarin.Forms;
using ViewModelsSamples.Events.AddPointOnClick;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace XamarinSample.Events.AddPointOnClick;
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View : ContentPage
{
public View()
{
InitializeComponent();
}
private void Chart_Touched(object sender, SkiaSharp.Views.Forms.SKTouchEventArgs e)
{
var chart = (CartesianChart)FindByName("chart");
var viewModel = (ViewModel)BindingContext;
// 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.
viewModel?.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((float)e.Location.X, (float)e.Location.Y));
var visuals = chart.GetVisualsAt(new LvcPoint((float)e.Location.X, (float)e.Location.Y));
}
}