Microsoft WinForms Chart Control - TitleBox

195 Views Asked by At

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.

enter image description here

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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!

enter image description here

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

enter image description here

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..