Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Add an Annotation at Runtime

  • 2 minutes to read

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

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);
        }
    }
}