0

I'm making a chart in VisualStudio WinForms with some data columns and I want to place labels for some of them. Main problem is that labels for first and last columns are not shown. Min and Max for chart is fixed and datapoints can be at min and max.

For example I made a chart with series[0].ChartType = "Column" and assigned labels by setting series[0].Label property to show X,Y,index and some text string, also set series[0].LabelBackColor and series[0].LabelBorderColor to make a box.
That is what I got:
Column type
Text is outside the background box, labels are overlapping borders but AxisY(on the left) is still visible; and labels for x=0 and x=10 are not shown at all! Also with Column type all the opertaions written below had no effect.

Actually I need StackedColumn type, so: then I changed type to series[0].ChartType = "StackedColumn" and got this:
StackedColumn
Boxes are bigger but still overlapping with AxisY line and numbers on top, and still no first and last labels.

I tried to set series[0].SmartLabelStyle.IsOverlappingHidden = false and got this:
StackedColumn with no hidden overlapping
AxisX and AxisY lines and markers are shown above labels. Thats no good.

Then I tried to disable SmartLabelStyle at all by setting: series[0].SmartLabelStyle.Enabled = false and got this:
StackedColumn with SmartLabelStyle turned off
Not much better. Even labels are overlapping now.

At last I tried to force Labels to move by enabling SmartLabelStyle and setting series[0].SmartLabelStyle.MovingDirection = "Right | Left" and got this:
StackedColumn with MovingDirection
Now Text in Labels is moved properly, but background box is not. If that box moved properly as well that would be enough, but it didn't. I'm out of ideas how to make DataPoint Labels show properly on the left and right sides of the chart.
Is there a way to set Label position for it text and box? How to set Label box to be proper size for text?
I definitely want to avoid chart.Paint methods.

Prain
  • 95
  • 7

1 Answers1

0

Would you be willing to dynamically add Annotations? This can be done before any chart.Paint methods are called:

For Each point As DataPoint In Chart1.Series(0).Points
    Dim ann As New RectangleAnnotation
    ann.Text = "X=" & point.XValue & vbNewLine & " Y=" & point.YValues(0) &
        vbNewLine & "a very long text string"
    ann.AnchorDataPoint = point
    Chart1.Annotations.Add(ann)
Next

This shows the resulting chart

JorisJ1
  • 922
  • 2
  • 12
  • 22