Back to Devexpress

ChartControl.AnnotationItemsSource Property

wpf-devexpress-dot-xpf-dot-charts-dot-chartcontrol-c63c2220.md

latest5.9 KB
Original Source

ChartControl.AnnotationItemsSource Property

Gets or sets the collection of objects used to generate annotations.

Namespace : DevExpress.Xpf.Charts

Assembly : DevExpress.Xpf.Charts.v25.2.dll

NuGet Package : DevExpress.Wpf.Charts

Declaration

csharp
public IEnumerable AnnotationItemsSource { get; set; }
vb
Public Property AnnotationItemsSource As IEnumerable

Property Value

TypeDescription
IEnumerable

A collection that is used to generate annotations. The default is null ( Nothing in Visual Basic).

|

Example

The Chart Control can generate its child elements from their ViewModels. This is useful when the application uses the MVVM architecture and the Chart should contain several chart elements that are bound to data. Use the ChartControl.AnnotationItemsSource property to specify the collection the Chart control utilizes to generate annotations. The ChartControl.AnnotationItemTemplate property allows you to define annotation presentation.

Note

If you have more than one template that can be used to render annotations, you can implement custom logic to choose the required template. To do this, derive from the DataTemplateSelector class, implement the SelectTemplate method that returns a template which meets the required condition, and assign it to the ChartControl.AnnotationItemTemplateSelector property.

xaml
<Window.DataContext>
    <viewModel:ChartViewModel/>
</Window.DataContext>
<Grid>
    <dxc:ChartControl AnnotationItemsSource="{Binding Path=Annotations, Mode=OneTime}" >
        <!-- Other chart settings are skipped. -->
        <dxc:ChartControl.AnnotationItemTemplate>
            <DataTemplate>
                <dxc:Annotation Content="{Binding Content}">
                    <dxc:Annotation.ShapePosition>
                        <dxc:RelativePosition 
                            Angle="{Binding Angle}" 
                            ConnectorLength="{Binding ConnectorLength}"/>
                    </dxc:Annotation.ShapePosition>
                    <dxc:Annotation.AnchorPoint>
                        <dxc:PaneAnchorPoint AxisXCoordinate="{Binding XValue}" 
                                             AxisYCoordinate="{Binding YValue}"/>
                    </dxc:Annotation.AnchorPoint>
                </dxc:Annotation>
            </DataTemplate>
        </dxc:ChartControl.AnnotationItemTemplate>
    </dxc:ChartControl>
</Grid>
csharp
public class ChartViewModel {
    public IEnumerable<MyAnnotation> Annotations { get; private set; }
    public ChartViewModel() {        
        Annotations = new Collection<MyAnnotation> {
            new MyAnnotation("Annotation 1", 2012, 6.203) {
                ConnectorLength = 100,
                Angle = -45
            },
            new MyAnnotation("Annotation 2", 2010, 5.7) {
                ConnectorLength = 100,
                Angle = 0
            }
        };
    }
    public class MyAnnotation {
        public string Content { get; set; }
        public AxisXCoordinate XValue { get; set; }
        public AxisYCoordinate YValue { get; set; }
        public double ConnectorLength { get; set; }
        public double Angle { get; set; }
        public MyAnnotation(string content, double xVal, double yVal) {
            Content = content;
            XValue = new AxisXCoordinate() { AxisValue = xVal };
            YValue = new AxisYCoordinate() { AxisValue = yVal };
        }
    }
}
vb
Public Class ChartViewModel    
    Public Property Annotations As IEnumerable(Of MyAnnotation)
        Get
        End Get
        Set
        End Set
    End Property    
    Public Sub New()
        MyBase.New
        Me.Annotations = New Collection(Of MyAnnotation)() {
            New MyAnnotation("Annotation 1", 2012, 6.203), 
            New MyAnnotation("Annotation 2", 2010, 5.7)
        }
    End Sub    
    Public Class MyAnnotation        
        Public Property Content As String
            Get
            End Get
            Set
            End Set
        End Property        
        Public Property XValue As AxisXCoordinate
            Get
            End Get
            Set
            End Set
        End Property        
        Public Property YValue As AxisYCoordinate
            Get
            End Get
            Set
            End Set
        End Property        
        Public Property ConnectorLength As Double
            Get
            End Get
            Set
            End Set
        End Property        
        Public Property Angle As Double
            Get
            End Get
            Set
            End Set
        End Property        
        Public Sub New(ByVal content As String, ByVal xVal As Double, ByVal yVal As Double)
            MyBase.New
            Me.Content = content
            Me.XValue = New AxisXCoordinate
            Me.YValue = New AxisYCoordinate
        End Sub
    End Class
End Class

See Also

ChartControl Class

ChartControl Members

DevExpress.Xpf.Charts Namespace