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

tim8w

Active member
Joined
Oct 26, 2010
Messages
42
Programming Experience
5-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:

1733264085428.png


I achieved this by drawing the labels as follows:

VB.NET:
    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 did this with a sample chart with x values 1 and 2:
VB.NET:
Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    Dim g = e.ChartGraphics.Graphics
    Dim xAxis = Chart1.ChartAreas(0).AxisX
    Using fmt = StringFormat.GenericTypographic
        fmt.Alignment = StringAlignment.Center
        fmt.LineAlignment = StringAlignment.Center
        For value = 1 To 2
            Dim w = 90
            Dim x = xAxis.ValueToPixelPosition(value) - w / 2
            Dim r As New RectangleF(x, 5, w, 20)
            g.FillRectangle(Brushes.LightBlue, r)
            g.DrawString("Goal 10", Font, Brushes.Black, r, fmt)
        Next
    End Using
End Sub
and got this result:
1733325240014.png
ValueToPixelPosition should be center of the value on x axis, so if you want a rectangle centered you have to subtract half the width of that rectangle.

Also note "This method only works in paint events", in case you're not using e.Graphics there.
 
ValueToPixelPosition should be center of the value on x axis, so if you want a rectangle centered you have to subtract half the width of that rectangle.

Also note "This method only works in paint events", in case you're not using e.Graphics there.

Yes, I am doing the writing in PostPaint. And for the record, the "Goal" labels above are already shifted too far to the left, so subtracting the width would only shift them more to the left. That's why I mentioned that maybe I need to add in the Axis position or something like that...
 
I did this with a sample chart with x values 1 and 2:
VB.NET:
Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    Dim g = e.ChartGraphics.Graphics
    Dim xAxis = Chart1.ChartAreas(0).AxisX
    Using fmt = StringFormat.GenericTypographic
        fmt.Alignment = StringAlignment.Center
        fmt.LineAlignment = StringAlignment.Center
        For value = 1 To 2
            Dim w = 90
            Dim x = xAxis.ValueToPixelPosition(value) - w / 2
            Dim r As New RectangleF(x, 5, w, 20)
            g.FillRectangle(Brushes.LightBlue, r)
            g.DrawString("Goal 10", Font, Brushes.Black, r, fmt)
        Next
    End Using
End Sub
and got this result:
1733325240014.png
 
Solution
ValueToPixelPosition should be center of the value on x axis, so if you want a rectangle centered you have to subtract half the width of that rectangle.

Also note "This method only works in paint events", in case you're not using e.Graphics there.

Ah, I see the problem. I started with ValueToPixelPosition(0) which I thought was the first Bar. It is not. I assume that is the xAxis itself. Changing my code to start at 1 for the first bar, solved the problem. Thanks for the help.
 
It depends on your x axis values, it is the datapoint x values you pass to ValueToPixelPosition. This example has x values 1.5, 2.5 and 4.0:
VB.NET:
For Each point In Chart1.Series("A").Points
    Dim w = 70
    Dim x = xAxis.ValueToPixelPosition(point.XValue) - w / 2
    Dim r As New RectangleF(x, 5, w, 20)
    g.FillRectangle(Brushes.LightBlue, r)
    g.DrawString("Goal 10", Font, Brushes.Black, r, fmt)
Next
1733331017620.png
 
If the x values are strings point.XValue as in last example won't work, in that case it looks like they align on values 1, 2, 3 etc.
 
Back
Top