Articles in this section
Category / Section

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

2 mins read

You can place the Xamarin Chart 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

 

Conclusion

I hope you enjoyed learning about how to place the Xamarin.Forms Chart DataMarker label at the top and bottom of the marker alternatively.

You can refer to our Xamarin Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin Chart Documentation to understand how to present and manipulate data.

For current customers, you can check out our Xamarin components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our  Xamarin Chart and other Xamarin components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied