Back to Devexpress

How to: Add an Annotation at Runtime

windowsforms-9760-controls-and-libraries-chart-control-examples-chart-elements-how-to-add-an-annotation-at-runtime.md

latest3.0 KB
Original Source

How to: Add an Annotation at Runtime

  • Nov 13, 2018
  • 2 minutes to read

This example illustrates how to initialize a TextAnnotation object at runtime, and enable its Annotation.RuntimeMoving option.

csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraCharts;

namespace TextAnnotationRuntime {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {

            // Create a text annotation. 
            SeriesPoint sp = this.chartControl1.Series["Series 1"].Points[1];
            TextAnnotation annotation = new TextAnnotation("Annotation 1", "test");

            // Change the text annotation font style to bold.  
            if (annotation != null)
                annotation.Font = new Font(annotation.Font.FontFamily, annotation.Font.Size, FontStyle.Bold);

            // Specify the text annotation position. 
            annotation.AnchorPoint = new SeriesPointAnchorPoint(sp);
            annotation.ShapePosition = new RelativePosition();
            RelativePosition position = annotation.ShapePosition as RelativePosition;
            position.ConnectorLength = 50;
            position.Angle = 270;
            annotation.RuntimeMoving = true;

            // Add an annotaion to the annotation repository. 
            this.chartControl1.AnnotationRepository.Add(annotation);
        }
    }
}
vb
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraCharts

Namespace TextAnnotationRuntime
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

            ' Create a text annotation. 
            Dim sp As SeriesPoint = Me.chartControl1.Series("Series 1").Points(1)
            Dim annotation As New TextAnnotation("Annotation 1", "test")

            ' Change the text annotation font style to bold.  
            If annotation IsNot Nothing Then
                annotation.Font = New Font(annotation.Font.FontFamily, annotation.Font.Size, FontStyle.Bold)
            End If

            ' Specify the text annotation position. 
            annotation.AnchorPoint = New SeriesPointAnchorPoint(sp)
            annotation.ShapePosition = New RelativePosition()
            Dim position As RelativePosition = TryCast(annotation.ShapePosition, RelativePosition)
            position.ConnectorLength = 50
            position.Angle = 270
            annotation.RuntimeMoving = True

            ' Add an annotaion to the annotation repository. 
            Me.chartControl1.AnnotationRepository.Add(annotation)
        End Sub
    End Class
End Namespace