Skip to content

Commit 2226d34

Browse files
authored
Update README.md
1 parent 52cea8d commit 2226d34

File tree

1 file changed

+108
-6
lines changed

1 file changed

+108
-6
lines changed

README.md

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The Horizontal Line Annotation includes following property:
1212

1313
Learn step-by-step instructions and gain insights to create and dynamically update the target line.
1414

15-
**Step 1:** The layout is created using a Grid with two columns.
15+
**Step 1:** The layout is created using a grid with two columns.
1616

1717
**XAML**
1818

@@ -27,7 +27,7 @@ Learn step-by-step instructions and gain insights to create and dynamically upda
2727
</Grid>
2828
```
2929

30-
**Step 2:** In first column of grid layout, initialize the [SfChart](https://help.syncfusion.com/wpf/charts/getting-started) and add the axes and series to the [SfChart](https://help.syncfusion.com/wpf/charts/getting-started) as shown below.
30+
**Step 2:** In first column of grid layout, initialize the [SfChart](https://help.syncfusion.com/wpf/charts/getting-started)and add the axes and series as shown below.
3131

3232
**XAML**
3333

@@ -50,7 +50,7 @@ Learn step-by-step instructions and gain insights to create and dynamically upda
5050
<chart:ColumnSeries.ColorModel>
5151
<chart:ChartColorModel>
5252
<chart:ChartColorModel.CustomBrushes>
53-
.....
53+
......
5454
</chart:ChartColorModel.CustomBrushes>
5555
</chart:ChartColorModel>
5656
</chart:ColumnSeries.ColorModel>
@@ -59,7 +59,8 @@ Learn step-by-step instructions and gain insights to create and dynamically upda
5959
</chart:SfChart>
6060
```
6161

62-
**Step 3:** The [HorizontalLineAnnotation](https://help.syncfusion.com/wpf/charts/annotations#vertical-and-horizontal-line-annotation) is initialized within the [Annotations](https://help.syncfusion.com/wpf/charts/annotations) collection of the [SfChart](https://help.syncfusion.com/wpf/charts/getting-started) to mark a dynamic target value on the Y-axis. The Y1 value is data-bound, enabling the target line to update dynamically based on the ViewModel.
62+
**Step 3:** The [HorizontalLineAnnotation](https://help.syncfusion.com/wpf/charts/annotations#vertical-and-horizontal-line-annotation) is initialized within the [Annotations](https://help.syncfusion.com/wpf/charts/annotations) collection of the [SfChart](https://help.syncfusion.com/wpf/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.
63+
6364

6465
**XAML**
6566

@@ -118,7 +119,7 @@ internal class ViewModel : INotifyPropertyChanged
118119
}
119120
```
120121

121-
**Step 4:** The second column of the grid layout contains a StackPanel with a Slider, TextBox and TextBlock, allowing the user to change the annotation value dynamically.
122+
**Step 4:** The second column of the grid layout contains a StackPanel with a Slider, TextBox and TextBlock, allowing the user to change the annotation value dynamically. The TextBox_TextChanged event ensures valid input by clamping values between 0 and the maximum of the Y_Axis.
122123

123124
**XAML**
124125

@@ -134,6 +135,107 @@ internal class ViewModel : INotifyPropertyChanged
134135
</StackPanel>
135136
```
136137

138+
**C#**
139+
140+
```csharp
141+
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
142+
{
143+
if (Y_Axis == null) return;
144+
var maxValue = Y_Axis.Maximum;
145+
146+
if (sender is TextBox textBox)
147+
{
148+
textBox.TextChanged -= TextBox_TextChanged;
149+
150+
if (string.IsNullOrWhiteSpace(textBox.Text))
151+
{
152+
viewModel.Y1 = double.MinValue;
153+
textBox.Text = string.Empty;
154+
}
155+
else
156+
{
157+
if (int.TryParse(textBox.Text, out int newValue))
158+
{
159+
if (newValue > maxValue)
160+
newValue = (int)maxValue;
161+
else if (newValue < 0)
162+
newValue = 0;
163+
164+
viewModel.Y1 = newValue;
165+
166+
textBox.Text = newValue.ToString();
167+
textBox.CaretIndex = textBox.Text.Length;
168+
}
169+
else
170+
{
171+
textBox.Text = ((int)viewModel.Y1).ToString();
172+
textBox.CaretIndex = textBox.Text.Length;
173+
}
174+
}
175+
176+
textBox.TextChanged += TextBox_TextChanged;
177+
}
178+
}
179+
```
180+
181+
**Step 5:** This XAML code for creates a grid layout with a chart and controls for adjusting a target line. The [SfChart](https://help.syncfusion.com/wpf/charts/getting-started) displays revenue data with a horizontal annotation line, adjustable using a TextBox and Slider in the adjacent StackPanel.
182+
183+
**XAML**
184+
185+
```
186+
<Grid>
187+
<Grid.ColumnDefinitions>
188+
<ColumnDefinition Width="*"></ColumnDefinition>
189+
<ColumnDefinition Width="200"></ColumnDefinition>
190+
</Grid.ColumnDefinitions>
191+
192+
<chart:SfChart Grid.Column="0">
193+
194+
<chart:SfChart.PrimaryAxis>
195+
<chart:CategoryAxis EdgeLabelsDrawingMode="Fit" ShowGridLines="False" Header="Months"/>
196+
</chart:SfChart.PrimaryAxis>
197+
198+
<chart:SfChart.SecondaryAxis>
199+
<chart:NumericalAxis x:Name="Y_Axis" Minimum="0" Maximum="20000" Interval="5000" ShowGridLines="False" Header="Revenue" LabelFormat="'$'0" PlotOffsetEnd="30"/>
200+
</chart:SfChart.SecondaryAxis>
201+
202+
<chart:SfChart.Annotations>
203+
<chart:HorizontalLineAnnotation Y1="{Binding Y1}"
204+
Stroke="Black"
205+
StrokeThickness="2"
206+
StrokeDashArray="5,2,2"
207+
Text="Target"
208+
FontSize="14"
209+
FontWeight="Bold"
210+
HorizontalTextAlignment="Left"
211+
VerticalTextAlignment="Top">
212+
</chart:HorizontalLineAnnotation>
213+
</chart:SfChart.Annotations>
214+
215+
<chart:ColumnSeries ItemsSource="{Binding Data}"
216+
XBindingPath="Months"
217+
YBindingPath="Revenue"
218+
Palette="Custom"
219+
Opacity="0.7">
220+
<chart:ColumnSeries.ColorModel>
221+
<chart:ChartColorModel>
222+
.....
223+
</chart:ChartColorModel>
224+
</chart:ColumnSeries.ColorModel>
225+
</chart:ColumnSeries>
226+
227+
</chart:SfChart>
228+
229+
<StackPanel Orientation="Vertical" Margin="10" Grid.Column="1">
230+
<TextBlock Text="Adjust Target Line" FontSize="16" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,20"/>
231+
<TextBox Text="{Binding Y1}" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextChanged="TextBox_TextChanged" Margin="0,0,0,20" Padding="10"/>
232+
<Slider Minimum="{Binding Minimum, Source={x:Reference Y_Axis}}"
233+
Maximum="{Binding Maximum, Source={x:Reference Y_Axis}}"
234+
Value="{Binding Y1}" HorizontalAlignment="Stretch"/>
235+
</StackPanel>
236+
</Grid>
237+
```
238+
137239

138240
**Output:**
139241

@@ -145,4 +247,4 @@ Path too long exception
145247

146248
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.
147249

148-
For more details, refer to the KB on [how to create and dynamically update target line for WPF Chart?](https://support.syncfusion.com/agent/kb/18542).
250+
For more details, refer to the KB on [how to create and dynamically update target line for WPF Chart](https://support.syncfusion.com/agent/kb/18542).

0 commit comments

Comments
 (0)