Microsoft WinForms图表控件 - TitleBox

时间:2014-12-19 21:08:55

标签: c# winforms microsoft-chart-controls

我希望在Microsoft WinForms图表控件中生成这种类型的TitleBox,以便将标题框停靠在图表区域的顶部。

enter image description here

有没有办法将标准标题框放在图表区域的顶部,或者我可以将这样的文本框添加到图表控件中吗?

1 个答案:

答案 0 :(得分:2)

选项1:您可以Label添加Chart,如下所示:

int lh = (int)(label1.Height / chart1.Height * 100f);
int cw = chart1.Width;

ChartArea CA = chart1.ChartAreas[0];
ElementPosition EP = CA.InnerPlotPosition;
CA.InnerPlotPosition = new ElementPosition(EP.X, EP.Y + lh, EP.Width, EP.Height - lh);
label1.Location = new Point((int)(EP.X * cw / 100f) + 10, 0);
label1.Width = (int)(EP.Width * cw / 100f) - 20;
label1.Height -= 2;
label1.Parent = chart1;

或者您可能希望通过将标签对接到顶部来定位标签..

您可以根据自己的喜好设置Label的样式,甚至添加Image ..

您可能需要使用label.Height!

的偏移量

enter image description here

选项2:您可以以相同的方式移动标题框:

chart1.Titles.Add("TiltelBox");
Title T = chart1.Titles[0];
ChartArea CA = chart1.ChartAreas[0];

T.DockedToChartArea = CA.Name;
T.BackColor = Color.Wheat;
T.Docking = Docking.Top;
T.IsDockedInsideChartArea = true;
ElementPosition EP = T.Position;
T.Position = new ElementPosition
                (EP.X + 10f, EP.Y -0.5f, EP.Width + 83.5f, EP.Height + 9f);

enter image description here

再说一遍:你会想要用你的标题来定位。上面的那些在这里发挥作用,但你需要用你的图表来改变它们。

请记住,ElementPosition使用图表大小的1/100作为单位;这很好,因为它可以扩展,但一开始很难设置..