Inserting TextAnnotation in graph c#

YUI-012 160 Reputation points
2024-10-12T10:42:00.2033333+00:00

User's image

I creating simple graph with line as average (sample only), I have a code in HorizontalLineAnnotation (color red line) and TextAnnotation but the problem is that, the TextAnnotation not working properly.

Here my code in HorizontalLineAnnotation and TextAnnotation .

private void annptaion()
{
//insert line
HorizontalLineAnnotation hl = new HorizontalLineAnnotation();
                    hl.AllowMoving = false;
                    hl.IsInfinitive = true;
                    //hl.LineColor = System.Drawing.Color.Red;
                    hl.AnchorY = Convert.ToDouble(2);//row line
                    hl.AxisX = chart1.ChartAreas[0].AxisX;
                    hl.AxisY = chart1.ChartAreas[0].AxisY;
                    hl.ClipToChartArea = chart1.ChartAreas[0].Name; hl.LineColor = Color.Red; hl.LineWidth = 1;//make line
                    hl.IsSizeAlwaysRelative = false;
                    chart1.Annotations.Add(hl);

//insert text
TextAnnotation ta = new TextAnnotation();
                    
                    ta.AxisX = chart1.ChartAreas[0].AxisX;
                    ta.AxisY = chart1.ChartAreas[0].AxisY;
                    ta.IsSizeAlwaysRelative = false;
                    ta.AnchorAlignment = ContentAlignment.BottomLeft;
                    ta.Text = "average (sample)";
                    ta.ClipToChartArea = chart1.ChartAreas[0].Name; ta.ForeColor = Color.Red;
                    chart1.Annotations.Add(ta);
}



I use the code above for displaying text in the line, but it's not working properly. I want to display the text either inside of the graph or outside of the graph.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-10-14T09:46:56.4366667+00:00

    Hi @YUI-012 , Welcome to Microsoft Q&A,

    It seems that you can achieve the desired effect in two ways:

    One is to anchor to a point to achieve the effect inside the table, and the other is to anchor to the table to achieve the effect outside the table:

    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.DataVisualization.Charting;
    
    namespace xxx
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                AddAnnotationsToChart();
            }
            private void AddAnnotationsToChart()
            {
                Series series = new Series("SampleSeries");
                series.ChartType = SeriesChartType.Line;
    
                series.Points.AddXY(1, 1);
                series.Points.AddXY(2, 2);
                series.Points.AddXY(3, 2);
                series.Points.AddXY(4, 3);  
                series.Points.AddXY(5, 4); 
                chart1.Series.Add(series);
    
                HorizontalLineAnnotation hl = new HorizontalLineAnnotation();
                hl.AllowMoving = false;
                hl.IsInfinitive = true;
                hl.LineColor = Color.Red;
                hl.AnchorY = 3.5;  
                hl.AxisX = chart1.ChartAreas[0].AxisX;
                hl.AxisY = chart1.ChartAreas[0].AxisY;
                hl.ClipToChartArea = chart1.ChartAreas[0].Name;
                hl.LineWidth = 1;
                chart1.Annotations.Add(hl);
    
                TextAnnotation ta = new TextAnnotation();
                ta.Text = "Annotation Text";
                ta.ForeColor = Color.Red;
                ta.SetAnchor(chart1.Series["SampleSeries"].Points[4]);
                chart1.Annotations.Add(ta);
    
                TextAnnotation ta2 = new TextAnnotation();
                ta2.Text = "At 80% width BC";
                ta2.AnchorX = 80;  // 80% of chart width
                ta2.AnchorY = 30;  // 30% of chart height, from top
                chart1.Annotations.Add(ta2);
    
                chart1.ChartAreas[0].AxisX.Minimum = 0;
                chart1.ChartAreas[0].AxisX.Maximum = 6;
                chart1.ChartAreas[0].AxisY.Minimum = 0;
                chart1.ChartAreas[0].AxisY.Maximum = 5;
    
                chart1.Invalidate();
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.