Skip to content

Commit 00ccc9b

Browse files
author
tineshsf4652
authored
Update README.md
1 parent 239b60c commit 00ccc9b

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

README.md

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# How to create and dynamically update target line for .NET MAUI Cartesian Chart
2-
This article provides a detailed walkthrough on how to add arrows to the axis using Annotations in [.NET MAUI Cartesian Chart](https://www.syncfusion.com/maui-controls/maui-cartesian-charts).
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).
33

44
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.
55

@@ -115,7 +115,7 @@ internal class ViewModel : INotifyPropertyChanged
115115
}
116116
```
117117

118-
**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.
118+
**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.
119119

120120
**XAML**
121121

@@ -129,6 +129,96 @@ internal class ViewModel : INotifyPropertyChanged
129129
</VerticalStackLayout>
130130
```
131131

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 XAML code defines a grid layout with a [SfCartesianChart](https://help.syncfusion.com/maui/cartesian-charts/getting-started) for displaying revenue data and a vertical control panel for adjusting a dynamic target line. The chart includes a horizontal annotation line bound to Y1, adjustable via an Entry and a Slider in the adjacent VerticalStackLayout.
166+
167+
**XAML**
168+
169+
```xml
170+
<Grid>
171+
<Grid.ColumnDefinitions>
172+
<ColumnDefinition Width="*"></ColumnDefinition>
173+
<ColumnDefinition Width="200"></ColumnDefinition>
174+
</Grid.ColumnDefinitions>
175+
176+
<Grid.BindingContext>
177+
<local:ViewModel x:Name="viewModel"/>
178+
</Grid.BindingContext>
179+
180+
<chart:SfCartesianChart Grid.Column="0">
181+
182+
<chart:SfCartesianChart.XAxes>
183+
<chart:CategoryAxis ShowMajorGridLines="False">
184+
.....
185+
</chart:CategoryAxis>
186+
</chart:SfCartesianChart.XAxes>
187+
188+
<chart:SfCartesianChart.YAxes>
189+
<chart:NumericalAxis x:Name="Y_Axis" Minimum="0" Maximum="20000" Interval="5000" ShowMajorGridLines="False" PlotOffsetEnd="30">
190+
.....
191+
</chart:NumericalAxis>
192+
</chart:SfCartesianChart.YAxes>
193+
194+
<chart:SfCartesianChart.Annotations>
195+
<chart:HorizontalLineAnnotation Y1="{Binding Y1}"
196+
Stroke="Black"
197+
StrokeWidth="2"
198+
StrokeDashArray="5,2,2"
199+
Text="Target">
200+
<chart:HorizontalLineAnnotation.LabelStyle>
201+
<chart:ChartAnnotationLabelStyle TextColor="Black" FontSize="14" FontAttributes="Bold" HorizontalTextAlignment="Start" VerticalTextAlignment="Start"/>
202+
</chart:HorizontalLineAnnotation.LabelStyle>
203+
</chart:HorizontalLineAnnotation>
204+
</chart:SfCartesianChart.Annotations>
205+
206+
<chart:ColumnSeries ItemsSource="{Binding Data}"
207+
XBindingPath="Months"
208+
YBindingPath="Revenue"
209+
PaletteBrushes="{Binding CustomBrushes}"
210+
Opacity="0.7"/>
211+
212+
</chart:SfCartesianChart>
213+
214+
<VerticalStackLayout Spacing="5" Grid.Column="1" Padding="10">
215+
<Label Text="Adjust Target Line" FontSize="16" FontAttributes="Bold" HorizontalOptions="Center"/>
216+
<Entry Text="{Binding Y1}" Keyboard="Numeric" TextChanged="Entry_TextChanged"/>
217+
<Slider Minimum="{Binding Minimum, Source={x:Reference Y_Axis}}" Maximum="{Binding Maximum, Source={x:Reference Y_Axis}}" Value="{Binding Y1}"/>
218+
</VerticalStackLayout>
219+
</Grid>
220+
```
221+
132222

133223
**Output:**
134224

@@ -140,5 +230,5 @@ Path too long exception
140230

141231
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.
142232

143-
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/agent/kb/18517).
233+
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/agent/kb/18517).
144234

0 commit comments

Comments
 (0)