~/cartesianChart/tooltips.md

Tooltips

Tooltips are popups that help the user to read a chart as the pointer moves.

tooltips

TooltipPosition property

You can place a tooltip at Top, Bottom, Left, Right, Center or Hidden positions, for now tooltips for the PieChart class only support the Center position, default value is Top.

Notice the Hidden position will disable tooltips in a chart.

<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Top"><!-- mark -->
</lvc:CartesianChart>
<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Bottom"><!-- mark -->
</lvc:CartesianChart>
<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Left"><!-- mark -->
</lvc:CartesianChart>
<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Right"><!-- mark -->
</lvc:CartesianChart>
<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Center"><!-- mark -->
</lvc:CartesianChart>
<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Hidden"><!-- mark -->
</lvc:CartesianChart>

TooltipFindingStrategy property

Every point drawn by the library defines a HoverArea, it defines an area in the chart that "triggers" the point, it is specially important to fire tooltips, a point will be included in a tooltip when the hover area was triggered by the pointer position.

The TooltipFindingStrategy property determines the hover area planes (X or Y) that a chart will use to trigger the HoverArea instances. In a chart, the following options are available:

CompareAll: Selects all the points whose hover area contain the pointer position.

tooltips

CompareOnlyX: Selects all the points whose hover area contain the pointer position, but it ignores the Y plane.

tooltips

CompareOnlyY: Selects all the points whose hover area contain the pointer position, but it ignores the X plane.

tooltips

CompareAllTakeClosest: Selects all the points whose hover area contain the pointer position, it only takes the closest point to the pointer, one per series.

CompareOnlyXTakeClosest: Selects all the points whose hover area contain the pointer position, but it ignores the Y plane, it only takes the closest point to the pointer, one per series.

CompareOnlyYTakeClosest: Selects all the points whose hover area contain the pointer position, but it ignores the X plane, it only takes the closest point to the pointer, one per series.

Automatic (default): Based on the series in the chart, LiveCharts will determine a finding strategy (one of the previous mentioned), all the series have a preferred finding strategy, normally vertical series prefer the CompareOnlyXTakeClosest strategy, horizontal series prefer CompareOnlyYTakeClosest, and scatter series prefers CompareAllTakeClosest, if all the series prefer the same strategy, then that strategy will be selected for the chart, if any series differs then the CompareAllTakeClosest strategy will be used.

Notice that the Axis.UnitWidth property might affect the tooltips in DateTime scaled charts, ensure your chart axis is using the properly unit width.

<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipFindingStrategy="CompareOnlyX"><!-- mark -->
</lvc:CartesianChart>

Tooltip point text

You can define the text the tooltip will display for a given point, using the Series.TooltipLabelFormatter property, this property is of type Func<ChartPoint, string> this means that is is a function, that takes a point as parameter and returns a string, the point will be injected by LiveCharts in this function to get a string out of it when it requires to build the text for a point in a tooltip, the injected point will be different as the user moves the pointer over the user interface.

By default the library already defines a default TooltipLabelFormatter for every series, all the series have a different formatter, but generally the default value uses the Series.Name and the ChartPoint.PrimaryValue properties, the following code snippet illustrates how to build a custom tooltip formatter.

new LineSeries<double>
{
    Name = "Sales",
    Values = new ObservableCollection<double> { 200, 558, 458 },
    // for the following formatter
    // when the pointer is over the first point (200), the tooltip will display:
    // Sales: 200
    TooltipLabelFormatter =
        (chartPoint) => $"{chartPoint.Context.Series.Name}: {chartPoint.PrimaryValue}"
},

new ColumnSeries<double>
{
    Name = "Sales 2",
    Values = new ObservableCollection<double> { 250, 350, 240 },
    // now it will use a currency formatter to display the primary value
    // result: Sales 2: $200.00
    TooltipLabelFormatter =
        (chartPoint) => $"{chartPoint.Context.Series.Name}: {chartPoint.PrimaryValue:C2}"
},

new StepLineSeries<ObservablePoint>
{
    Name = "Average",
    Values = new ObservableCollection<ObservablePoint>
    {
        new ObservablePoint(10, 5),
        new ObservablePoint(5, 8)
    },
    // We can also display both coordinates (X and Y in a cartesian coordinate system)
    // result: Average: 10, 5
    TooltipLabelFormatter =
        (chartPoint) => $"{chartPoint.Context.Series.Name}: {chartPoint.SecondaryValue}, {chartPoint.PrimaryValue}"
},

new ColumnSeries<ObservablePoint>
{
    Values = new ObservableCollection<double> { 250, 350, 240 },
    // or anything...
    // result: Sales at this moment: $200.00
    TooltipLabelFormatter =
        (chartPoint) => $"Sales at this moment: {chartPoint.PrimaryValue:C2}"
}

Styling tooltips

A chart exposes many properties to quickly style a tooltip:

<lvc:CartesianChart
    Series="{Binding Series}"
    TooltipPosition="Left"
    TooltipFontFamily="Courier New"
    TooltipFontSize="25"
    TooltipTextBrush="#f2f4c3"
    TooltipBackground="#480032">
</lvc:CartesianChart>

The code above would result in the following tooltip:

zooming

Custom template

If you need to customize more, you can also pass your own template:

<UserControl
    x:Class="WinUISample.General.TemplatedTooltips.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.General.TemplatedTooltips"
    mc:Ignorable="d">

    <UserControl.DataContext>
        <vms:ViewModel/>
    </UserControl.DataContext>
    <Grid>
        <lvc:CartesianChart Series="{Binding Series}" TooltipPosition="Top" >
            <!-- mark -untilCloses CartesianChart.TooltipTemplate -->
            <lvc:CartesianChart.TooltipTemplate>
                <DataTemplate>
                    <Border Background="#303030">
                        <ItemsControl ItemsSource="{Binding Points}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border Padding="7 5">
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock
                                                Text="{Binding ChartPoint.AsTooltipString}"
                                                FontFamily="{Binding FontFamily}"
                                                Foreground="AntiqueWhite"
                                                FontSize="{Binding FontSize}"
                                                FontStyle="{Binding FontStyle}"
                                                FontStretch="{Binding FontStretch}"
                                                Margin="0 0 8 0"
                                                VerticalAlignment="Center"/>
                                            <lvc:MotionCanvas
                                                Margin="0 0 8 0" 
                                                PaintTasks="{Binding ChartPoint.Context.Series.CanvasSchedule.PaintSchedules}"
                                                Width="{Binding ChartPoint.Context.Series.CanvasSchedule.Width}"
                                                Height="{Binding ChartPoint.Context.Series.CanvasSchedule.Height}"
                                                VerticalAlignment="Center"/>
                                        </StackPanel>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Border>
                </DataTemplate>
            </lvc:CartesianChart.TooltipTemplate>
        </lvc:CartesianChart>
    </Grid>

</UserControl>

You can find a another example at the source code of the DefaultTooltip class (xaml, code).

custom tooltip