Articles in this section
Category / Section

How to add benchmark line in Xamarin.Forms Chart?

1 min read

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

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