Add Point On Click
View model
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
namespace ViewModelsSamples.Events.AddPointOnClick;
[ObservableObject]
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; }
}
using System.Collections.ObjectModel;
using Eto.Forms;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Eto;
using ViewModelsSamples.Events.AddPointOnClick;
namespace EtoFormsSample.Events.AddPointOnClick;
public class View : Panel
{
private readonly ObservableCollection<ObservablePoint> _data;
/// <summary>
/// Initializes a new instance of the <see cref="View"/> class.
/// </summary>
public View()
{
var viewModel = new ViewModel();
_data = viewModel.Data;
var cartesianChart = new CartesianChart
{
Series = viewModel.SeriesCollection,
};
cartesianChart.MouseDown += CartesianChart_Click;
Content = cartesianChart;
}
private void CartesianChart_Click(object sender, MouseEventArgs e)
{
var chart = (CartesianChart)sender;
var p = new LvcPointD(e.Location.X, e.Location.Y);
// scales the UI coordinates to the corresponding data in the chart.
var dataCoordinates = chart.ScalePixelsToData(p);
// 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(p.X, p.Y));
var visuals = chart.GetVisualsAt(new LvcPoint(p.X, p.Y));
}
}