Search code examples
vb.netmschart

Annotation is Not Show on Chart Area After Grouping


I have two annotations text and line and want to group into single annotation using AnnotationGroup Class. I used the below code:

Dim line As New HorizontalLineAnnotation
With line
    .IsSizeAlwaysRelative = False
    .ClipToChartArea = Chart1.ChartAreas(0).Name
    .IsInfinitive = True
    .LineColor = Color.Black
    .LineDashStyle = ChartDashStyle.Dash
    .LineWidth = 2
End With
Dim text As New TextAnnotation
With text
    .IsSizeAlwaysRelative = False
    .Text = "something"
    .ForeColor = line.LineColor
End With
Dim group As New AnnotationGroup
With group
    .AxisX = Chart1.ChartAreas(0).AxisX
    .AxisY = Chart1.ChartAreas(0).AxisY
    .AnchorX = 0
    .AnchorY = 10
    .Annotations.Add(line)
    .Annotations.Add(text)
End With
Chart1.Annotations.Add(group)

The code compiled without any error but nothing shown on the chart area. I m unable to figure out. Also the MSDN does say nothing about this.

Thanks.


Solution

  • Congratulations, you are one of the very few who use AnnotationGroups.

    They are quite useful to keep Annotations grouped so that you can move them around or hide&show them together. In order to make any Annotations show up you need to set a few more properties, though.

    Most notably all annotations need to hava a size and a position. The latter can be in value units or in position percentages and can be absolute or anchored with or without an offset. You can also mix those systems!

    This is true for the Line- and Text- or RectangleAnnotations but also for the AnnotationGroup, so after setting sizes and positions in some way they should show up.

    Do read the docs on all related properties to find out which kind of positioning you want..! - As you have set the axes for your annotation group its position will be in axis units.

    Note that the positional values of the Annotations are relative to the AnnotationGroup they belong to!

    If you need more help to say so; I only do c# but the code should be easy to translate..