|
1 |
| -# How-to-create-and-dynamically-update-target-line-for-.NET-MAUI-Cartesian-Chart |
2 |
| -Learn how to add and dynamically update a target line in .NET MAUI Cartesian Chart using Annotation. Customize its appearance and functionality effortlessly |
| 1 | +# How to create and dynamically update target line for .NET MAUI Cartesian Chart |
| 2 | +This article provides a detailed walkthrough on how to add and dynamically update a target line using annotations in [.NET MAUI Cartesian Chart](https://www.syncfusion.com/maui-controls/maui-cartesian-charts). |
| 3 | + |
| 4 | +The [SfCartesianChart](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.SfCartesianChart.html) includes support for [Annotations](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.SfCartesianChart.html#Syncfusion_Maui_Charts_SfCartesianChart_Annotations), enabling the addition of various types of annotations to enhance chart visualization. Using [HorizontalLineAnnotation](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.HorizontalLineAnnotation.html), you can create and dynamically adjust the target line. |
| 5 | + |
| 6 | +The Horizontal Line Annotation includes following property: |
| 7 | + |
| 8 | +* [Y1](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ChartAnnotation.html#Syncfusion_Maui_Charts_ChartAnnotation_Y1) - Gets or sets the Y1 coordinate of the horizontal line annotation. |
| 9 | +* [Stroke](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ShapeAnnotation.html#Syncfusion_Maui_Charts_ShapeAnnotation_Stroke) - Gets or sets the stroke color of the horizontal line annotation. |
| 10 | +* [StrokeWidth](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ShapeAnnotation.html#Syncfusion_Maui_Charts_ShapeAnnotation_StrokeWidth) - Gets or sets the stroke width of the horizontal line annotation. |
| 11 | +* [StrokeDashArray](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ShapeAnnotation.html#Syncfusion_Maui_Charts_ShapeAnnotation_StrokeDashArray) - Gets or sets the stroke dash pattern of the horizontal line annotation. |
| 12 | +* [Text](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ShapeAnnotation.html#Syncfusion_Maui_Charts_ShapeAnnotation_Text) - Gets or sets the annotation text of the horizontal line annotation. |
| 13 | +* [LabelStyle](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ShapeAnnotation.html#Syncfusion_Maui_Charts_ShapeAnnotation_LabelStyle) - Gets or sets the style for customizing the annotation text of the horizontal line annotation. |
| 14 | + |
| 15 | +Learn step-by-step instructions and gain insights to create and dynamically update the target line. |
| 16 | + |
| 17 | +**Step 1:** The layout is created using a grid with two columns. |
| 18 | + |
| 19 | +**XAML** |
| 20 | + |
| 21 | + ```xml |
| 22 | +<Grid> |
| 23 | + |
| 24 | + <Grid.ColumnDefinitions> |
| 25 | + <ColumnDefinition Width="*"></ColumnDefinition> |
| 26 | + <ColumnDefinition Width="200"></ColumnDefinition> |
| 27 | + </Grid.ColumnDefinitions> |
| 28 | + |
| 29 | +</Grid> |
| 30 | + ``` |
| 31 | + |
| 32 | +**Step 2:** In the first column of the grid layout, initialize the [SfCartesianChart](https://help.syncfusion.com/maui/cartesian-charts/getting-started) and add the axes and series as shown below. |
| 33 | + |
| 34 | +**XAML** |
| 35 | + |
| 36 | + ```xml |
| 37 | +<chart:SfCartesianChart Grid.Column="0"> |
| 38 | + |
| 39 | + <chart:SfCartesianChart.XAxes> |
| 40 | + <chart:CategoryAxis ShowMajorGridLines="False"> |
| 41 | + ..... |
| 42 | + </chart:CategoryAxis> |
| 43 | + </chart:SfCartesianChart.XAxes> |
| 44 | + |
| 45 | + <chart:SfCartesianChart.YAxes> |
| 46 | + <chart:NumericalAxis x:Name="Y_Axis" Minimum="0" Maximum="20000" Interval="5000" ShowMajorGridLines="False" PlotOffsetEnd="30"> |
| 47 | + ..... |
| 48 | + </chart:NumericalAxis> |
| 49 | + </chart:SfCartesianChart.YAxes> |
| 50 | + |
| 51 | + <chart:ColumnSeries ItemsSource="{Binding Data}" |
| 52 | + XBindingPath="Months" |
| 53 | + YBindingPath="Revenue" |
| 54 | + PaletteBrushes="{Binding CustomBrushes}" |
| 55 | + Opacity="0.7"/> |
| 56 | + |
| 57 | +</chart:SfCartesianChart> |
| 58 | + ``` |
| 59 | + |
| 60 | +**Step 3:** The [HorizontalLineAnnotation](https://help.syncfusion.com/maui/cartesian-charts/annotation#vertical-and-horizontal-line-annotations) is initialized within the [Annotations](https://help.syncfusion.com/maui/cartesian-charts/annotation) collection of the [SfCartesianChart](https://help.syncfusion.com/maui/cartesian-charts/getting-started) to mark a dynamic target value on the Y-axis. The Y1 property is data-bound to the ViewModel, allowing the target line to adjust dynamically when the value changes. |
| 61 | + |
| 62 | +**XAML** |
| 63 | + |
| 64 | + ```xml |
| 65 | +<chart:SfCartesianChart Grid.Column="0"> |
| 66 | +..... |
| 67 | + <chart:SfCartesianChart.Annotations> |
| 68 | + <chart:HorizontalLineAnnotation Y1="{Binding Y1}" |
| 69 | + Stroke="Black" |
| 70 | + StrokeWidth="2" |
| 71 | + StrokeDashArray="5,2,2" |
| 72 | + Text="Target"> |
| 73 | + ...... |
| 74 | + </chart:HorizontalLineAnnotation> |
| 75 | + </chart:SfCartesianChart.Annotations> |
| 76 | +..... |
| 77 | +</chart:SfCartesianChart> |
| 78 | + ``` |
| 79 | + |
| 80 | +**C#** |
| 81 | + |
| 82 | + ```csharp |
| 83 | +internal class ViewModel : INotifyPropertyChanged |
| 84 | +{ |
| 85 | + private double y1; |
| 86 | + public double Y1 |
| 87 | + { |
| 88 | + get => y1; |
| 89 | + set |
| 90 | + { |
| 91 | + if(y1 != value) |
| 92 | + { |
| 93 | + y1 = value; |
| 94 | + OnPropertyChanged(nameof(Y1)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 100 | + |
| 101 | + protected void OnPropertyChanged(string name) |
| 102 | + { |
| 103 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); |
| 104 | + } |
| 105 | + |
| 106 | + ..... |
| 107 | + |
| 108 | + public ViewModel() |
| 109 | + { |
| 110 | + Y1 = 12000; |
| 111 | + ..... |
| 112 | + } |
| 113 | +} |
| 114 | + ``` |
| 115 | + |
| 116 | +**Step 4:** The second column of the grid layout contains a VerticalStackLayout with a Slider and an Entry box, allowing the user to change the annotation value dynamically. The Entry_TextChanged event validates input, ensuring values stay within the bounds defined by the Y_Axis. |
| 117 | + |
| 118 | +**XAML** |
| 119 | + |
| 120 | + ```xml |
| 121 | +<VerticalStackLayout Spacing="5" Grid.Column="1" Padding="10"> |
| 122 | + |
| 123 | + <Label Text="Adjust Target Line" FontSize="16" FontAttributes="Bold" HorizontalOptions="Center"/> |
| 124 | + <Entry Text="{Binding Y1}" Keyboard="Numeric" TextChanged="Entry_TextChanged"/> |
| 125 | + <Slider Minimum="{Binding Minimum, Source={x:Reference Y_Axis}}" Maximum="{Binding Maximum, Source={x:Reference Y_Axis}}" Value="{Binding Y1}"/> |
| 126 | + |
| 127 | +</VerticalStackLayout> |
| 128 | + ``` |
| 129 | + |
| 130 | +This code handles the TextChanged event for an Entry, validating input to ensure it is a numeric value within the maximum axis range. If invalid input is detected, the text reverts to the previous value to maintain consistency. |
| 131 | + |
| 132 | +**C#** |
| 133 | + |
| 134 | + ```csharp |
| 135 | +private void Entry_TextChanged(object sender, TextChangedEventArgs e) |
| 136 | +{ |
| 137 | + if(Y_Axis == null) return; |
| 138 | + var maxValue = Y_Axis.Maximum; |
| 139 | + |
| 140 | + if (sender is Entry entry) |
| 141 | + { |
| 142 | + if (string.IsNullOrWhiteSpace(entry.Text)) |
| 143 | + { |
| 144 | + viewModel.Y1 = double.MinValue; |
| 145 | + entry.Text = string.Empty; |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + if (double.TryParse(e.NewTextValue, out double newValue)) |
| 150 | + { |
| 151 | + if (newValue > maxValue) |
| 152 | + { |
| 153 | + entry.Text = e.OldTextValue; |
| 154 | + } |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + entry.Text = e.OldTextValue; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + ``` |
| 164 | + |
| 165 | +**Step 5:** This code defines a [HorizontalLineAnnotation](https://help.syncfusion.com/maui/cartesian-charts/annotation#vertical-and-horizontal-line-annotations) in a [SfCartesianChart](https://help.syncfusion.com/maui/cartesian-charts/getting-started), marking a specific Y-axis value (Y1) with a styled dashed line and label. The label's appearance is customized using a [ChartAnnotationLabelStyle](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ChartAnnotationLabelStyle.html) for text color, size, font attributes, and alignment. |
| 166 | + |
| 167 | +**XAML** |
| 168 | + |
| 169 | + ```xml |
| 170 | +<chart:SfCartesianChart Grid.Column="0"> |
| 171 | + ...... |
| 172 | + |
| 173 | + <chart:SfCartesianChart.Annotations> |
| 174 | + <chart:HorizontalLineAnnotation Y1="{Binding Y1}" |
| 175 | + Stroke="Black" |
| 176 | + StrokeWidth="2" |
| 177 | + StrokeDashArray="5,2,2" |
| 178 | + Text="Target"> |
| 179 | + <chart:HorizontalLineAnnotation.LabelStyle> |
| 180 | + <chart:ChartAnnotationLabelStyle TextColor="Black" FontSize="14" FontAttributes="Bold" HorizontalTextAlignment="Start" VerticalTextAlignment="Start"/> |
| 181 | + </chart:HorizontalLineAnnotation.LabelStyle> |
| 182 | + </chart:HorizontalLineAnnotation> |
| 183 | + </chart:SfCartesianChart.Annotations> |
| 184 | + |
| 185 | + ..... |
| 186 | + </chart:SfCartesianChart> |
| 187 | + ``` |
| 188 | + |
| 189 | +**Output:** |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | +**Troubleshooting** |
| 194 | + |
| 195 | +Path too long exception |
| 196 | + |
| 197 | +If you are facing a path too long exception when building this example project, close Visual Studio and rename the repository to a shorter name before building the project. |
| 198 | + |
| 199 | +For more details, refer to the KB on [how to create and dynamically update target line for .NET MAUI Cartesian Chart](https://support.syncfusion.com/kb/article/18517/how-to-create--dynamically-update-target-line-for-net-maui-cartesian-chart). |
| 200 | + |
0 commit comments