Page 1 of 1

How do I get SizeScreenCoors when I set the Size of Annotation to Automatic?

Posted: Tue Dec 12, 2023 8:55 am
by a1782680475
Now SizeScreenCoords is always an incorrect value。

Re: How do I get SizeScreenCoors when I set the Size of Annotation to Automatic?

Posted: Tue Dec 12, 2023 4:20 pm
by ArctionKestutis
If you want to ask question, please provide enough information. It is not very useful to say "incorrect".
Please write down exactly how you are setting Annotation's properties and what result you want to get?

Re: How do I get SizeScreenCoors when I set the Size of Annotation to Automatic?

Posted: Wed Dec 13, 2023 1:40 am
by a1782680475
This is the code for setting annotations:

Code: Select all

AnnotationXY annotation = new AnnotationXY(ChartView.ViewXY, ChartView.ViewXY.XAxes[1], ChartView.ViewXY.YAxes[1])
{
    Style = AnnotationStyle.Rectangle,
    LocationCoordinateSystem = CoordinateSystem.RelativeCoordinatesToTarget,
    BorderVisible = false,
    AllowDragging = false,
    AllowResize = false,
    AllowRotate = false,
    AllowTargetMove = false,
    AllowAnchorAdjust = false,
    AllowUserInteraction = false,
    Anchor = new PointDoubleXY(0, 1),
    AutoSizePadding = 2,
    ArrowStyleBegin = ArrowStyle.None,
    ArrowStyleEnd = ArrowStyle.None,
    Behind = true,
    KeepVisible = false,
    Visible = false,
    Sizing = AnnotationXYSizing.Automatic
};
I need to obtain the width and height of the annotation,Here is the code for obtaining dimensions:

Code: Select all

            AnnotationXY annotation = ChartView.ViewXY.Annotations[0];
            var annotationWidth = annotation.SizeScreenCoords.Width;
            var annotationHeight = annotation.SizeScreenCoords.Height;
AnnotationWidth is always 100, and AnnotationHeight is always 50. It is obvious that these two values are incorrect because I roughly estimated them to be around 70 and 20 respectively, and the values obtained will not change when the text content changes.

Re: How do I get SizeScreenCoors when I set the Size of Annotation to Automatic?

Posted: Wed Dec 13, 2023 12:22 pm
by ArctionKestutis
You can not read annotation.SizeScreenCoords property, if you not set it. That is, this property is used only with Sizing = AnnotationXYSizing.ScreenCoordinates. This property does not report rendering size, if your AnnotationXYSizing is 'Automatic' or 'AxisValuesBoundaries'.

If you want 'Automatic' size approximation, then you should measure text (based on Font and string). Below is the code for such estimate:

Code: Select all

            float fMaxWidth = 0;
            float fMaxHeight = 0;
            string[] aContent = annotation.Text.Split('\n');
            for (int i = 0; i < aContent.Length; i++)
            {
                var point = _chart.MeasureTextPX(aContent[i], annotation.TextStyle.Font);
                if (fMaxWidth < point.X)
                    fMaxWidth = point.X;
                fMaxHeight += point.Y;
            }
            size = new Size(fMaxWidth + 2 * annotation.AutoSizePadding, fMaxHeight + 35);
Note that chart should rendered (on the screen) before you can use MeasureTextPX() method.

Re: How do I get SizeScreenCoors when I set the Size of Annotation to Automatic?

Posted: Thu Dec 14, 2023 2:44 am
by a1782680475
Okay, thank you very much