Question C# MSChart. How do I find the XY position of the Series Label for a StackedColumn ChartType?

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
136
Programming Experience
10+
I have a StackedColumn Chart. I would like to draw text at the X position of the Series centerpoint and a Y position near the top of the chart. I am having trouble figuring out where the Series XY position is.

The closest thing I have been able to achieve is shown below:

1733264834235.png


I achieved this by drawing the labels as follows:

C#:
    startX = chartRect.Left + (float)(chartDailyBuildRateDisplay.ChartAreas[0].AxisX.ValueToPixelPosition(0));
    startY = chartRect.Top + 5;
    g.DrawRectangle(whitePen, startX, startY + 15, 120, 40);
    g.FillRectangle(steelBlueBrush, startX, startY + 15, 120, 40);
    g.DrawString(" Goal: " + LVDTGoal.ToString(), font, whiteBrush, startX, startY + 20);

    startX = chartRect.Left + (float)(chartDailyBuildRateDisplay.ChartAreas[0].AxisX.ValueToPixelPosition(1));
    g.DrawRectangle(whitePen, startX, startY + 15, 120, 40);
    g.FillRectangle(steelBlueBrush, startX, startY + 15, 120, 40);
    g.DrawString(" Goal: " + RVDTGoal.ToString(), font, whiteBrush, startX, startY + 20);

I must be missing something because as you can see, the labels are shifted.
Obviously I need to include something else like the Axis size or something like that? Any help would be appreciated.
 
Solution
I think that you are on the right track regarding the axis size needing to be factored in. I have no idea how you would go about determining that, though.

You were kind of correct. Apparently, ValueToPixelPosition(0) is not the first Bar. Changing the code to ValueToPixelPosition(1) for the first bar and subtracting 1/2 the Label width solved it.

Thanks for the help.
I think that you are on the right track regarding the axis size needing to be factored in. I have no idea how you would go about determining that, though.
 
I think that you are on the right track regarding the axis size needing to be factored in. I have no idea how you would go about determining that, though.

You were kind of correct. Apparently, ValueToPixelPosition(0) is not the first Bar. Changing the code to ValueToPixelPosition(1) for the first bar and subtracting 1/2 the Label width solved it.

Thanks for the help.
 
Solution
Back
Top Bottom