|
1 | | -# How-to-Add-Annotation-in-Axis-Label-Click-in-WPF-SfChart |
2 | | -Learn how to add an annotation to a WPF SfChart when an axis label is clicked. This functionality enhances the interactivity of your chart by highlighting specific data points and providing additional information. |
| 1 | +# How to Add Annotation in Axis Label Click in WPF SfChart? |
| 2 | + |
| 3 | +This article provides a detailed walkthrough on how to add an annotation to a [WPF SfChart](https://www.syncfusion.com/wpf-controls/charts) using [LabelClicked](https://help.syncfusion.com/wpf/charts/axis#labelclicked) event when an axis label is clicked. This functionality enhances the interactivity of your chart by displaying specific information about the clicked label. |
| 4 | + |
| 5 | +Learn step-by-step instructions and gain insights on using the Label Clicked event in a WPF SfChart. |
| 6 | + |
| 7 | +**Step 1:** Initialize the SfChart with Primary and Secondary axes by referring to the WPF charts [documentation](https://help.syncfusion.com/wpf/charts/getting-started). Trigger the [LabelClicked](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ChartAxisBase2D.html#events) event on the primary axis. |
| 8 | + |
| 9 | +XAML |
| 10 | + ```xml |
| 11 | +<chart:SfChart x:Name="chart"> |
| 12 | + |
| 13 | + <chart:SfChart.PrimaryAxis> |
| 14 | + <chart:CategoryAxis LabelClicked="Axis_LabelClicked"/> |
| 15 | + </chart:SfChart.PrimaryAxis> |
| 16 | + |
| 17 | + <chart:SfChart.SecondaryAxis> |
| 18 | + <chart:NumericalAxis/> |
| 19 | + </chart:SfChart.SecondaryAxis> |
| 20 | + |
| 21 | + <chart:ColumnSeries ItemsSource="{Binding DataSource}" |
| 22 | + XBindingPath="Company" |
| 23 | + YBindingPath="Revenue"/> |
| 24 | + |
| 25 | +</chart:SfChart> |
| 26 | + ``` |
| 27 | + |
| 28 | +**Step 2:** Implement the event handler for the **LabelClicked** event of the primary axis. This event handler will be triggered when a label on the primary axis is clicked. The [LabelContent](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ChartAxisLabel.html#Syncfusion_UI_Xaml_Charts_ChartAxisLabel_LabelContent) and [Position](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ChartAxisLabel.html#Syncfusion_UI_Xaml_Charts_ChartAxisLabel_Position) of the clicked label can be retrieved from the [AxisLabelClickedEventArgs](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.AxisLabelClickedEventArgs.html). |
| 29 | + |
| 30 | +C# |
| 31 | + ```csharp |
| 32 | +public partial class MainWindow : Window |
| 33 | +{ |
| 34 | + public MainWindow() |
| 35 | + { |
| 36 | + InitializeComponent(); |
| 37 | + } |
| 38 | + |
| 39 | + private void Axis_LabelClicked(object sender, AxisLabelClickedEventArgs e) |
| 40 | + { |
| 41 | + string labelContent = e.AxisLabel.LabelContent.ToString(); |
| 42 | + int labelPosition = (int)e.AxisLabel.Position; |
| 43 | + } |
| 44 | +} |
| 45 | + ``` |
| 46 | + |
| 47 | +**Step 3:** Create and add an [Annotation](https://help.syncfusion.com/wpf/charts/annotations) to the chart in the event handler based on the label position obtained from the event arguments. Populate the data list from the ItemsSource to diplay the revenue values. |
| 48 | + |
| 49 | + C# |
| 50 | + ```csharp |
| 51 | +public partial class MainWindow : Window |
| 52 | +{ |
| 53 | + List<double> data = new(); |
| 54 | + |
| 55 | + public MainWindow() |
| 56 | + { |
| 57 | + InitializeComponent(); |
| 58 | + |
| 59 | + foreach (var value in viewModel.DataSource) |
| 60 | + { |
| 61 | + data.Add(value.Revenue); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void Axis_LabelClicked(object sender, AxisLabelClickedEventArgs e) |
| 66 | + { |
| 67 | + string labelContent = e.AxisLabel.LabelContent.ToString(); |
| 68 | + int labelPosition = (int)e.AxisLabel.Position; |
| 69 | + |
| 70 | + chart.Annotations.Clear(); |
| 71 | + |
| 72 | + RectangleAnnotation annotation = new() |
| 73 | + { |
| 74 | + X1 = labelPosition, |
| 75 | + Y1 = data[labelPosition] + 10, |
| 76 | + X2 = labelPosition + 1, |
| 77 | + Y2 = data[labelPosition] + 110, |
| 78 | + CoordinateUnit = CoordinateUnit.Axis, |
| 79 | + HorizontalAlignment = HorizontalAlignment.Center, |
| 80 | + VerticalTextAlignment = VerticalAlignment.Center, |
| 81 | + Text = $"Revenue: {data[labelPosition]} USD", |
| 82 | + FontWeight = FontWeights.Bold, |
| 83 | + Fill = new SolidColorBrush(Colors.SkyBlue), |
| 84 | + Stroke = new SolidColorBrush(Colors.Transparent) |
| 85 | + }; |
| 86 | + |
| 87 | + chart.Annotations.Add(annotation); |
| 88 | + } |
| 89 | +} |
| 90 | + ``` |
| 91 | + |
| 92 | +**Step 4:** After deploying the application, click on the axis label as shown in the output below; the annotations will be displayed, showing the values corresponding to the label. |
| 93 | + |
| 94 | +**Output:** |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +## Troubleshooting |
| 99 | +### Path too long exception |
| 100 | +If you are facing a path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project. |
| 101 | + |
| 102 | +For more details, refer to the KB on [How-to-Add-Annotation-in-Axis-Label-Click-in-WPF-SfChart](). |
0 commit comments