简体   繁体   中英

Microsoft WinForms Chart Control - TitleBox

I am looking to produce this type of TitleBox in the Microsoft WinForms Chart Control, so that the titlebox is docked to the top of the chartarea.

在此处输入图片说明

Is there any way to position the standard titlebox on the top of the chartarea like this or can I add such a textbox to the chart control?

Option 1: You can add a Label into the Chart like this:

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;

Or you may want to position the Label by docking it to the top..

You can style the Label to your liking, even add an Image ..

You may need to play with the offset for the label.Height!

在此处输入图片说明

Option 2: You can move the Title box around in the same way:

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

在此处输入图片说明

Again: you will want to play around with the way you position the Title. The ones above happend to work here but you will need to change them with you chart..

Remember that the ElementPosition uses 1/100 of the chart size as its unit; this is nice as it scales but makes it hard to set up at first..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM