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="AvaloniaSample.Axes.Crosshairs.View"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia"
    xmlns:vms="using:ViewModelsSamples.Axes.Crosshairs"
    x:DataType="vms:ViewModel">

    <UserControl.DataContext>
        <vms:ViewModel/>
    </UserControl.DataContext>

    <lvc:CartesianChart>

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

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

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

    </lvc:CartesianChart>
</UserControl>

Articles you might also find useful: