在图c#中插入TextAnnotation

问题描述 投票:0回答:1

图片

我创建了以线为平均值的简单图表(仅示例),我在 HorizontalLineAnnotation (红色线)和 TextAnnotation 中有代码,但问题是,TextAnnotation 无法正常工作。

这是我在 HorizontalLineAnnotation 和 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);
}

我使用上面的代码来显示行中的文本,但它无法正常工作。我想在图表内部或图表外部显示文本。

c# visual-studio charts
1个回答
0
投票

在您的代码中,您仅将注释绑定到轴,但没有指定其确切位置。

添加:

ta.annotation.AnchorX = 2;
ta.annotation.AnchorY = 0.5;

AnchorX/Y:获取或设置注释锚定的 X/Y 坐标。

© www.soinside.com 2019 - 2024. All rights reserved.