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.