)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to place the Xamarin.Forms Chart DataMarker label at the top and bottom of the marker alternatively?

Platform: Xamarin.Forms |
Control: SfChart |
Published Date: July 26, 2019 |
Last Revised Date: August 21, 2019

You can place the ChartDataMarkerLabel at the top and bottom positions of the marker alternatively using the converter and the ChartDataMarker LabelTemplate.

 

The following code sample demonstrates how to place the data marker label at the top and bottom positions of the marker alternatively.

 

XAML

<ContentPage.Resources>
        <ResourceDictionary>
                <local:MarginConverter x:Key="marginConverter"/>
        </ResourceDictionary>
</ContentPage.Resources>
 
<chart:SfChart>
      ……          
      <chart:LineSeries x:Name="series" ItemsSource="{Binding Data}">
                <chart:LineSeries.DataMarker>
                        <chart:ChartDataMarker ShowMarker="True" MarkerHeight="10"                        
                                            MarkerWidth="10" MarkerColor="Blue">
                              <chart:ChartDataMarker.LabelStyle>
                                     <chart:DataMarkerLabelStyle LabelPosition="Center"/>
                              </chart:ChartDataMarker.LabelStyle>
                              <chart:ChartDataMarker.LabelTemplate>
                                     <DataTemplate>
                                            <StackLayout BackgroundColor="Transparent">
                                                  <Label Text="{Binding YValue}" BackgroundColor="Aqua" 
                                                              Margin="{Binding Converter={StaticResource      
                                                                         marginConverter}, Mode=Default}"/>
                                            </StackLayout>
                                     </DataTemplate>
                              </chart:ChartDataMarker.LabelTemplate>
                        </chart:ChartDataMarker>
                </chart:LineSeries.DataMarker>
      </chart:LineSeries>
</chart:SfChart>

 

C#

public class MarginConverter : IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var data = value as Model;
 
                if (data.Index % 2 == 0)
                {
                    value = new Thickness(0, 0, 0, 30);
                }
                else
                {
                    value = new Thickness(0, 30, 0, 0);
                }
            }
            return value;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
}

Output

Placing SfChart DataMarker label position at top and bottom of the marker alternatively

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon