Crosshairs
This sample uses C# 12 features, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.
View model
using SkiaSharp;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.Kernel.Sketches;
namespace ViewModelsSamples.Axes.Crosshairs;
public class ViewModel
{
public ISeries[] Series { get; set; } = [
new LineSeries<double> { Values = [200, 558, 458, 249, 457, 339, 587] },
new LineSeries<double> { Values = [210, 400, 300, 350, 219, 323, 618] },
];
public ICartesianAxis[] XAxes { get; set; } = [
new Axis
{
CrosshairLabelsBackground = SKColors.DarkOrange.AsLvcColor(),
CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkRed),
CrosshairPaint = new SolidColorPaint(SKColors.DarkOrange, 1),
Labeler = value => value.ToString("N2")
}
];
public ICartesianAxis[] YAxes { get; set; } = [
new Axis
{
CrosshairLabelsBackground = SKColors.DarkOrange.AsLvcColor(),
CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkRed),
CrosshairPaint = new SolidColorPaint(SKColors.DarkOrange, 1),
CrosshairSnapEnabled = true // snapping is also supported
}
];
}
XAML
<UserControl
x:Class="UnoWinUISample.Axes.Crosshairs.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.WinUI"
xmlns:vms="using:ViewModelsSamples.Axes.Crosshairs"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<Grid>
<lvc:CartesianChart
Series="{Binding Series}"
XAxes="{Binding XAxes}"
YAxes="{Binding YAxes}">
</lvc:CartesianChart>
</Grid>
</UserControl>