# AnnotationsProvider

RadChartView supports an annotations provider that is used for creating chart annotations in a data-bound scenario. Basically, the provider accepts a collection of models describing the annotations and a descriptor (or descriptors) that defines their type and appearance.

The provider is set through the **AnnotationsProvider** property of the chart (see **Example 1**) and it can be populated with a collection of models through its **Source** property.

**Example 1: Setting the AnnotationsProvider**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.AnnotationsProvider>
        <telerik:ChartAnnotationsProvider Source="{Binding AnnotationModelsCollection}" />
    </telerik:RadCartesianChart.AnnotationsProvider>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
</telerik:RadCartesianChart>
```

You can choose from a few approaches to determine the type and set up the annotations:

* [Defining a Descriptor](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/annotations-provider#defining-a-descriptor)
* [Defining a Descriptor Selector](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/annotations-provider#defining-a-descriptor-selector)
* [Using the AnnotationCreated Event](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/annotations-provider#using-the-annotationcreated-event)

You can find an example of the provider in the [How to Use AnnotationsProvider](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/annotations-provider#how-to-use-annotationsprovider) section.

## Defining a Descriptor

You can define an object of type **ChartAnnotationDescriptor** for the provider (**Example 2**). The descriptor exposes a single property of type Style that determines the type of the annotation. You can use the style to set up the annotation - set its position and visual appearance.

**Example 2: Setting a ChartAnnotationDescriptor**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.AnnotationsProvider>
        <telerik:ChartAnnotationsProvider Source="{Binding AnnotationModelsCollection}">
            <telerik:ChartAnnotationsProvider.AnnotationDescriptors>
                <telerik:ChartAnnotationDescriptor>
                    <telerik:ChartAnnotationDescriptor.Style>
                        <Style TargetType="telerik:CartesianGridLineAnnotation">
                            <Setter Property="Value" Value="{Binding Value}" />
                            <Setter Property="Stroke" Value="{Binding Fill}" />
                        </Style>
                    </telerik:ChartAnnotationDescriptor.Style>
                </telerik:ChartAnnotationDescriptor>
            </telerik:ChartAnnotationsProvider.AnnotationDescriptors>
        </telerik:ChartAnnotationsProvider>
    </telerik:RadCartesianChart.AnnotationsProvider>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
</telerik:RadCartesianChart>
```

The annotation descriptor in *Example 2* will create a CartesianGridLineAnnotation for each item in the Source collection of the provider.

> The data context passed in the Style of the descriptor is an object from the Source collection.

## Defining a Descriptor Selector

The annotations provider allows you to decide what type of annotation to create at runtime. You can do that using a descriptor selector. You can create a custom class that derives from **ChartAnnotationDescriptorSelector** and override its **SelectDescriptor()** method (**Example 3**). Based on a condition, you can return different styles for each item from the Source collection.

**Example 3: Creating ChartAnnotationDescriptorSelector**

```C#
public class MyAnnotationDescriptorSelector : ChartAnnotationDescriptorSelector
{
    public ChartAnnotationDescriptor MarkedZoneAnnotationDescriptor { get; set; }
    public ChartAnnotationDescriptor GridLineAnnotationDescriptor { get; set; }

    public override ChartAnnotationDescriptor SelectDescriptor(ChartAnnotationsProvider provider, object context)
    {
        // Here you can define logic that returns different descriptors based on a condition.
        if (context is ModelA)
        {
            return this.MarkedZoneAnnotationDescriptor;
        }
        else if (context is ModelB)
        {
            return this.GridLineAnnotationDescriptor;
        }
        else
        {
            return base.SelectDescriptor(provider, context);
        }
    }
}
```

**Example 4: Setting the descriptor selector**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.AnnotationsProvider>
        <telerik:ChartAnnotationsProvider Source="{Binding AnnotationModelsCollection}">
            <telerik:ChartAnnotationsProvider.AnnotationDescriptorSelector>
                <local:MyAnnotationDescriptorSelector>
                    <local:MyAnnotationDescriptorSelector.GridLineAnnotationDescriptor>
                        <telerik:ChartAnnotationDescriptor>
                            <telerik:ChartAnnotationDescriptor.Style>
                                <Style TargetType="telerik:CartesianGridLineAnnotation">
                                    <Setter Property="Value" Value="{Binding Value}" />
                                </Style>
                            </telerik:ChartAnnotationDescriptor.Style>
                        </telerik:ChartAnnotationDescriptor>
                    </local:MyAnnotationDescriptorSelector.GridLineAnnotationDescriptor>
                    <local:MyAnnotationDescriptorSelector.MarkedZoneAnnotationDescriptor>
                        <telerik:ChartAnnotationDescriptor>
                            <telerik:ChartAnnotationDescriptor.Style>
                                <Style TargetType="telerik:CartesianMarkedZoneAnnotation">
                                    <Setter Property="HorizontalFrom" Value="{Binding HorizontalFrom}" />
                                    <Setter Property="HorizontalTo" Value="{Binding HorizontalTo}" />
                                </Style>
                            </telerik:ChartAnnotationDescriptor.Style>
                        </telerik:ChartAnnotationDescriptor>
                    </local:MyAnnotationDescriptorSelector.MarkedZoneAnnotationDescriptor>
                </local:MyAnnotationDescriptorSelector>
            </telerik:ChartAnnotationsProvider.AnnotationDescriptorSelector>
        </telerik:ChartAnnotationsProvider>
    </telerik:RadCartesianChart.AnnotationsProvider>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
</telerik:RadCartesianChart>
```

You can find a runnable code example that demonstrates the descriptor selector in the [How to use AnnotationsProvider](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/annotations-provider#how-to-use-annotationsprovider) section of this article.

## Using the AnnotationCreated Event

The **AnnotationCreated** event allows you to set up or modify the created annotations at runtime. Also, you can use the event to replace the created annotation with a new one.

As *Example 5* and *Example 6* demonstrate, you can use the event to create an annotation and let the provider add it in the chart's Annotations collection.

The event arguments are of type **ChartAnnotationCreatedEventArgs** and expose the following properties:

* **Annotation**: A property of type **ChartAnnotation** that holds the created annotation.
* **Context**: A property of type **object** that holds the model of the annotation.

**Example 5: Subscribing for the AnnotationCreated event**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.AnnotationsProvider>
        <telerik:ChartAnnotationsProvider Source="{Binding AnnotationModelsCollection}"
                                  AnnotationCreated="ChartAnnotationsProvider_AnnotationCreated" />
    </telerik:RadCartesianChart.AnnotationsProvider>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
</telerik:RadCartesianChart>
```

**Example 6: Defining the AnnotationCreated event handler**

```C#
private void ChartAnnotationsProvider_AnnotationCreated(object sender, ChartAnnotationCreatedEventArgs e)
{
    object annotationViewModel = e.Context;
    var annotation = new CartesianGridLineAnnotation();
    // set up the annotation
    e.Annotation = annotation;
}
```

## How to Use AnnotationsProvider

The following example demonstrates how to use **AnnotationsProvider** and **AnnotationDescriptorSelector**.

> You can find all of the described features of the provider demonstrated in our [AnnotationsProvider](https://github.com/telerik/xaml-sdk/tree/master/ChartView/WPF/AnnotationsProvider) SDK example.

**Example 7: Setting up the view**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.AnnotationsProvider>
        <telerik:ChartAnnotationsProvider Source="{Binding AnnotationModelsCollection}">
            <telerik:ChartAnnotationsProvider.AnnotationDescriptorSelector>
                <local:MyAnnotationDescriptorSelector>
                    <local:MyAnnotationDescriptorSelector.GridLineAnnotationDescriptor>
                        <telerik:ChartAnnotationDescriptor>
                            <telerik:ChartAnnotationDescriptor.Style>
                                <Style TargetType="telerik:CartesianGridLineAnnotation">
                                    <Setter Property="Value" Value="{Binding Value}" />
                                    <Setter Property="Stroke" Value="{Binding Fill}" />
                                </Style>
                            </telerik:ChartAnnotationDescriptor.Style>
                        </telerik:ChartAnnotationDescriptor>
                    </local:MyAnnotationDescriptorSelector.GridLineAnnotationDescriptor>
                    <local:MyAnnotationDescriptorSelector.MarkedZoneAnnotationDescriptor>
                        <telerik:ChartAnnotationDescriptor>
                            <telerik:ChartAnnotationDescriptor.Style>
                                <Style TargetType="telerik:CartesianMarkedZoneAnnotation">
                                    <Setter Property="HorizontalFrom" Value="{Binding HorizontalFrom}" />
                                    <Setter Property="HorizontalTo" Value="{Binding HorizontalTo}" />
                                    <Setter Property="VerticalFrom" Value="{Binding VerticalFrom}" />
                                    <Setter Property="VerticalTo" Value="{Binding VerticalTo}" />
                                    <Setter Property="Fill" Value="{Binding Fill}" />
                                </Style>
                            </telerik:ChartAnnotationDescriptor.Style>
                        </telerik:ChartAnnotationDescriptor>
                    </local:MyAnnotationDescriptorSelector.MarkedZoneAnnotationDescriptor>
                </local:MyAnnotationDescriptorSelector>
            </telerik:ChartAnnotationsProvider.AnnotationDescriptorSelector>
        </telerik:ChartAnnotationsProvider>
    </telerik:RadCartesianChart.AnnotationsProvider>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.Series>
        <telerik:LineSeries CategoryBinding="Category" ValueBinding="Value" ItemsSource="{Binding Data}" />
    </telerik:RadCartesianChart.Series>
</telerik:RadCartesianChart>
```

*Example 7* will generate the visual result shown in *Figure 1*.

> caption The initial view

![Telerik UI for WPF RadChartView Initial Chart Before Annotations Are Generated by AnnotationsProvider](https://www.telerik.com/products/wpf/documentation/assets/e48380f4e2d162e90f848b4cae9b399b/radchartview-annotations-annotationsprovider-01.png)

The classes defined in *Example 8* will be used as models for the annotations.

**Example 8: Defining the annotation models**

```C#
public class GridLineAnnotationModel
{
    public double Value { get; set; }
    public Brush Fill { get; set; }
}

public class MarkedZoneAnnotationModel
{
    public double VerticalFrom { get; set; }
    public double VerticalTo { get; set; }
    public string HorizontalFrom { get; set; }
    public string HorizontalTo { get; set; }
    public Brush Fill { get; set; }
}
```

You can create a class that will expose a collection of objects that holds the annotation models.

**Example 9: Defining a main view model class**

```C#
public class MainViewModel
{
    public ObservableCollection<object> AnnotationModelsCollection { get; set; }
    public MainViewModel()
    {
        this.AnnotationModelsCollection = new ObservableCollection<object>()
        {
            new MarkedZoneAnnotationModel()
            {
                HorizontalFrom = "February",
                HorizontalTo="April",
                VerticalTo = 5,
                VerticalFrom = 2,
                Fill = Brushes.Green,
            },
            new GridLineAnnotationModel() { Value = 6, Fill = Brushes.Orange, },
        };
    }
}
```

*Example 10* demonstrates a custom **AnnotationDescriptorSelector** that returns a different descriptor based on the type of the model.

**Example 10: Creating the descriptor selector**

```C#
public class MyAnnotationsDescriptorSelector : ChartAnnotationDescriptorSelector
{
    public ChartAnnotationDescriptor MarkedZoneAnnotationDescriptor { get; set; }
    public ChartAnnotationDescriptor GridLineAnnotationDescriptor { get; set; }

    public override ChartAnnotationDescriptor SelectDescriptor(ChartAnnotationsProvider provider, object context)
    {
        if (context is MarkedZoneAnnotationModel)
        {
            return this.MarkedZoneAnnotationDescriptor;
        }
        else if (context is GridLineAnnotationModel)
        {
            return this.GridLineAnnotationDescriptor;
        }

        return base.SelectDescriptor(provider, context);
    }
}
```

*Example 11* demonsrates how to define the AnnotationProvider and set its AnnotationDescriptorSelector.

**Example 11: Setting the annotation descriptor**

```XAML
<telerik:RadCartesianChart>
    <telerik:RadCartesianChart.AnnotationsProvider>
        <telerik:ChartAnnotationsProvider Source="{Binding AnnotationModelsCollection}">
            <telerik:ChartAnnotationsProvider.AnnotationDescriptorSelector>
                <local:MyAnnotationsDescriptorSelector>
                    <local:MyAnnotationsDescriptorSelector.GridLineAnnotationDescriptor>
                        <telerik:ChartAnnotationDescriptor>
                            <telerik:ChartAnnotationDescriptor.Style>
                                <Style TargetType="telerik:CartesianGridLineAnnotation">
                                    <Setter Property="Value" Value="{Binding Value}" />
                                </Style>
                            </telerik:ChartAnnotationDescriptor.Style>
                        </telerik:ChartAnnotationDescriptor>
                    </local:MyAnnotationsDescriptorSelector.GridLineAnnotationDescriptor>
                    <local:MyAnnotationsDescriptorSelector.MarkedZoneAnnotationDescriptor>
                        <telerik:ChartAnnotationDescriptor>
                            <telerik:ChartAnnotationDescriptor.Style>
                                <Style TargetType="telerik:CartesianMarkedZoneAnnotation">
                                    <Setter Property="HorizontalFrom" Value="{Binding HorizontalFrom}" />
                                </Style>
                            </telerik:ChartAnnotationDescriptor.Style>
                        </telerik:ChartAnnotationDescriptor>
                    </local:MyAnnotationsDescriptorSelector.MarkedZoneAnnotationDescriptor>
                </local:MyAnnotationsDescriptorSelector>
            </telerik:ChartAnnotationsProvider.AnnotationDescriptorSelector>
        </telerik:ChartAnnotationsProvider>
    </telerik:RadCartesianChart.AnnotationsProvider>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
</telerik:RadCartesianChart>
```

> caption The final result that shows the generated annotations

![Telerik UI for WPF RadChartView Showing Generated Grid Line and Marked Zone Annotations](https://www.telerik.com/products/wpf/documentation/assets/b0e9e63437b035a26e4b31a513156807/radchartview-annotations-annotationsprovider-02.png)

## See Also

* [Introduction](https://www.telerik.com/products/wpf/documentation/controls/radchartview/getting-started/introduction)
* [Annotations Getting Started](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/getting-started)
* [Create Data-Bound Chart](https://www.telerik.com/products/wpf/documentation/controls/radchartview/populating-with-data/series-databinding)
* [GridLineAnnotation](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/gridline)
* [MarkedZoneAnnotation](https://www.telerik.com/products/wpf/documentation/controls/radchartview/features/annotations/markedzone)
