|
1 | | -# How-to-create-custom-annotation-in-WPF-Chart-SfChart |
2 | | -How to create custom annotation in WPF Chart-SfChart |
| 1 | +# How to create custom annotation in WPF Chart (SfChart)? |
| 2 | + |
| 3 | +This article explains how to display custom content in WPF SfChart Annotation by following the steps. |
| 4 | + |
| 5 | +**Step 1:** Write a custom class inheriting from [RectangleAnnotation](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.RectangleAnnotation.html) and add the Content property in this class. |
| 6 | + |
| 7 | +**[C#]** |
| 8 | +``` |
| 9 | +public class CustomTextAnnotation : RectangleAnnotation |
| 10 | +{ |
| 11 | + public object Content |
| 12 | + { |
| 13 | + get { return GetValue(ContentProperty); } |
| 14 | + set { SetValue(ContentProperty, value); } |
| 15 | + } |
| 16 | +
|
| 17 | + // Using DependencyProperty as the backing store for TextString. This enables animation, styling, binding, etc... |
| 18 | + public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(CustomTextAnnotation), new PropertyMetadata(null)); |
| 19 | +
|
| 20 | + protected override void SetBindings() |
| 21 | + { |
| 22 | + base.SetBindings(); //You need to invoke this method to make all the default bindings |
| 23 | + if (TextElement != null) |
| 24 | + { |
| 25 | + Binding textBinding = new Binding { Path = new PropertyPath("Content"), Source = this }; |
| 26 | + //TextElement is content control define internally to display text. |
| 27 | + TextElement.SetBinding(ContentControl.ContentProperty, textBinding); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +**Step 2:** Add the necessary content in the Content property of Annotation. |
| 34 | + |
| 35 | +**[XAML]** |
| 36 | +``` |
| 37 | +<chart:SfChart x:Name="chart" Margin="10" > |
| 38 | + <chart:SfChart.PrimaryAxis> |
| 39 | + <chart:CategoryAxis/> |
| 40 | + </chart:SfChart.PrimaryAxis> |
| 41 | +
|
| 42 | + <chart:SfChart.SecondaryAxis> |
| 43 | + <chart:NumericalAxis/> |
| 44 | + </chart:SfChart.SecondaryAxis> |
| 45 | +
|
| 46 | + <chart:SfChart.Annotations> |
| 47 | + <local:CustomTextAnnotation HorizontalTextAlignment="Stretch" VerticalTextAlignment="Stretch" X1="1.6" X2="1.6" Y1="18" Y2="20" Fill="White" IsHitTestVisible="False" CanDrag="True" CanResize="True"> |
| 48 | + <local:CustomTextAnnotation.Content> |
| 49 | + <StackPanel Orientation="Horizontal"> |
| 50 | + <Path Data="M16.710899,36.727C20.7409,39.759457 25.747499,41.558827 31.1693,41.558827 36.588501, |
| 51 | + 41.558827 41.596401,39.759457 45.627602,36.727 55.046902,37.911882 62.335999, |
| 52 | + 45.946764 62.335999,55.688923L62.335999,64 0,64 0,55.688923C0,45.946764,7.2904,37.911882, |
| 53 | + 16.710899,36.727z M31.169201,0C40.807137,0 48.622002,7.8164558 48.622002, |
| 54 | + 17.455803 48.622002,27.095258 40.807137,34.909003 31.169201,34.909003 21.528767, |
| 55 | + 34.909003 13.715,27.095258 13.715,17.455803 13.715,7.8164558 21.528767,0 31.169201,0z" |
| 56 | + Stretch="Uniform" Fill="#606060" Height="50" Width="50" |
| 57 | + RenderTransformOrigin="0.5,0.5"/> |
| 58 | + <Button Content="Click Me" Width="145" Height="50" Click="Button_Click"/> |
| 59 | + </StackPanel> |
| 60 | + </local:CustomTextAnnotation.Content> |
| 61 | + </local:CustomTextAnnotation> |
| 62 | + </chart:SfChart.Annotations> |
| 63 | +
|
| 64 | + <chart:ColumnSeries ItemsSource="{Binding Collection}" XBindingPath="XValue" YBindingPath="YValue"/> |
| 65 | +</chart:SfChart> |
| 66 | +``` |
| 67 | + |
| 68 | +## Output: |
| 69 | + |
| 70 | + |
0 commit comments