Crosshairs

This sample uses C# 13 preview features such as partial properties, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.

sample image

View model

using System;

namespace ViewModelsSamples.Axes.Crosshairs;

public class ViewModel
{
    public double[] Values { get; set; } =
        [200, 558, 458, 249, 457, 339, 587];

    public Func<double, string> LabelFormatter { get; set; } =
        value => value.ToString("N2");
}

XAML

<UserControl
    x:Class="WinUISample.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>

    <lvc:CartesianChart>

        <lvc:CartesianChart.Series>
            <lvc:SeriesCollection>
                <lvc:XamlLineSeries Values="{Binding Values}" />
            </lvc:SeriesCollection>
        </lvc:CartesianChart.Series>

        <lvc:CartesianChart.XAxes>
            <lvc:AxesCollection>
                <lvc:XamlAxis
                    CrosshairLabelsBackground="{lvc:Color Hex='#FF8C00'}"
                    CrosshairLabelsPaint="{lvc:SolidColorPaint Color='#8B0000'}"
                    CrosshairPaint="{lvc:SolidColorPaint Color='#FF8C00'}"
                    Labeler="{Binding LabelFormatter}"/>
            </lvc:AxesCollection>
        </lvc:CartesianChart.XAxes>

        <lvc:CartesianChart.YAxes>
            <lvc:AxesCollection>
                <lvc:XamlAxis
                    CrosshairLabelsBackground="{lvc:Color Hex='#FF8C00'}"
                    CrosshairLabelsPaint="{lvc:SolidColorPaint Color='#8B0000'}"
                    CrosshairPaint="{lvc:SolidColorPaint Color='#FF8C00'}"
                    CrosshairSnapEnabled="True"
                    Labeler="{Binding LabelFormatter}"/>
            </lvc:AxesCollection>
        </lvc:CartesianChart.YAxes>

    </lvc:CartesianChart>
</UserControl>

Articles you might also find useful: