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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add benchmark line in Xamarin.Forms Chart?

Platform: Xamarin.Forms |
Control: SfChart |
Published Date: September 19, 2018 |
Last Revised Date: August 21, 2019

You can represent a benchmark line in a chart using the annotation feature and set the target value to the Y1 position of HorizontalLineAnnotation. For example, the target value has been set to 62 in the following data.

My data look like this:

S.No

XValue

YValue

1

Group A

50%

2

Group B

60%

3

Group C

70%

 

Target Value: 62%

The following code demonstrates how to add benchmark line in a chart using the annotation feature and you can download the complete sample here.

XAML:

<chart:SfChart>
…
 
<chart:SfChart.Series>
      <chart:ColumnSeries ItemsSource="{Binding SeriesData}" XBindingPath="XValue" YBindingPath="YValue">
                <chart:ColumnSeries.DataMarker>
                    <chart:ChartDataMarker>
                        <chart:ChartDataMarker.LabelStyle>
                            <chart:DataMarkerLabelStyle LabelPosition="Inner" LabelFormat="0'%'" />
                        </chart:ChartDataMarker.LabelStyle>
                    </chart:ChartDataMarker>
                </chart:ColumnSeries.DataMarker>
        </chart:ColumnSeries>
 </chart:SfChart.Series> 
 
 <chart:SfChart.ChartAnnotations>
     <chart:HorizontalLineAnnotation Y1="62"  Text = "Benchmark" StrokeColor="Orange" StrokeWidth = "6">
         <chart:HorizontalLineAnnotation.LabelStyle>
             <chart:ChartAnnotationLabelStyle Font="12" HorizontalTextAlignment="Start" VerticalTextAlignment="End"/>
         </chart:HorizontalLineAnnotation.LabelStyle>
     </chart:HorizontalLineAnnotation>
</chart:SfChart.ChartAnnotations>
 
</chart:SfChart>

 

C#:

SfChart chart = new SfChart();
…
 
ColumnSeries series = new ColumnSeries();
series.ItemsSource = view.SeriesData;
series.XBindingPath = "XValue";
series.YBindingPath = "YValue";
series.DataMarker = new ChartDataMarker();
series.DataMarker.LabelStyle = new DataMarkerLabelStyle();
series.DataMarker.LabelStyle.LabelPosition = DataMarkerLabelPosition.Inner;
series.DataMarker.LabelStyle.LabelFormat = "0'%'";
chart.Series.Add(series);
 
HorizontalLineAnnotation horizontalLine = new HorizontalLineAnnotation();
horizontalLine.Y1 = 62;
horizontalLine.Text = "Benchmark";
horizontalLine.StrokeColor = Color.Orange;
horizontalLine.StrokeWidth = 6;
horizontalLine.LabelStyle = new ChartAnnotationLabelStyle();
horizontalLine.LabelStyle.Font = Font.SystemFontOfSize(12);
horizontalLine.LabelStyle.HorizontalTextAlignment = ChartAnnotationAlignment.Start;
horizontalLine.LabelStyle.VerticalTextAlignment = ChartAnnotationAlignment.End;
chart.ChartAnnotations.Add(horizontalLine);
 

 

ViewModel:

public class ViewModel
 {
     public ObservableCollection<Model> SeriesData
     {
         get;
         set;
     }
 
     public ViewModel()
     {
         SeriesData = new ObservableCollection<Model>();
         SeriesData.Add(new Model { XValue = "Group A", YValue = 50 });
         SeriesData.Add(new Model { XValue = "Group B", YValue = 60 });
         SeriesData.Add(new Model { XValue = "Group C", YValue = 70 });
     }
 }

 

 

C:\Users\sakthivel.palaniyapp\AppData\Local\Microsoft\Windows\INetCache\Content.Word\Benchmark.jpg

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