Version

XamCategoryChart Tooltips

This topic explains, with code examples, how to configure the tooltips in the XamCategoryChart control.

Introduction

In the XamCategoryChart, tooltips are displayed when the tap and hold gesture is performed within the chart’s plot area. Also, tapping and holding and then moving the finger over other parts of the plot area without releasing will update tooltip values.

Required Background

The following topics are prerequisites to understanding this topic:

Topic Purpose

XamCategoryChart Overview

This topic provides conceptual information about the XamCategoryChart control including its main features, minimum requirements, and user functionality.

Binding to Data

This topic explains how to bind data to the XamCategoryChart control.

In this Topic

This topic contains the following sections:

Tooltips Overview

The tooltip content is provided through the ToolTipTemplate property of the XamCategoryChart object. In Xamarin.Forms this is done using a DataTemplate, which contains the visual elements that make up the tooltip.

The tooltip in the XamCategoryChart will represent each of the series plotted in the chart. As such, unless you are using a hierarchical structure where your value property names are the same, a DataTemplateSelector may be a wise choice to use with the ToolTipTemplate property.

The elements placed in the DataTemplate used for the ToolTipTemplate will have a binding context of a DataContext object. This object can access the underlying data item for that particular tooltip via its Item property and the Series that it is representing via its Series property. The usage of this DataContext class and the DataTemplateSelector are shown in the code snippet below.

Property Settings

You can further customize the look and feel of the XamCategoryChart control’s tooltips by setting the following properties in addition to the ToolTipTemplate property:

Property Name Property Type Description

ToolTipBackgroundColor

Brush

Gets or sets background color for the tooltip.

ToolTipBorderColor

Brush

Gets or sets border color for the tooltip.

ToolTipBorderThickness

double

Gets or sets border thickness for the tooltip.

Code Snippet

The following code example shows how to customize the tooltips in the XamCategoryChart control using the properties above, along with a DataTemplateSelector:

In XAML

xmlns:ig="clr-namespace:Infragistics.XamarinForms.Controls.Charts;assembly=Infragistics.XF.Charts"
xmlns:local="clr-namespace:MyAppNamespace"

In XAML

<ig:XamCategoryChart ItemsSource="{Binding}"
                     ChartType="Column"
                     ToolTipBackgroundColor="LightGray"
                     ToolTipBorderColor="DarkOrange"
                     ToolTipBorderThickness="2">
    <ig:XamCategoryChart.ToolTipTemplate>
        <local:TooltipSelector/>
    </ig:XamCategoryChart.ToolTipTemplate>
</ig:XamCategoryChart>

In C#

namespace MyAppNamespace
{
    public class TooltipSelector : DataTemplateSelector
    {
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            if (container is ColumnSeries)
            {
                ColumnSeries series = container as ColumnSeries;
                DataTemplate template = CreateDataTemplate(series);
                return template;
            }
            else
            {
                return null;
            }
        }

        private DataTemplate CreateDataTemplate(ColumnSeries series)
        {
            DataTemplate template = new DataTemplate(() =>
            {
                StackLayout layout = new StackLayout() { Orientation = StackOrientation.Horizontal, Margin = new Thickness(5, 2) };

                Label titleLabel = new Label() { TextColor = ((SolidColorBrush)series.ActualBrush).Color };
                titleLabel.Text = series.ValueMemberPath + ": ";

                Label valueLabel = new Label() { TextColor = ((SolidColorBrush)series.ActualBrush).Color };
                valueLabel.SetBinding(Label.TextProperty, new Binding("Item." + series.ValueMemberPath));

                layout.Children.Add(titleLabel);
                layout.Children.Add(valueLabel);

                return layout;
            });

            return template;
        }
    }
}
categorychart tooltip.png

Related Content

Topic Purpose

Adding Category Chart

This article will get you up and running with the XamCategoryChart control.