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

Need a new feature to LightningChart Ultimate? Post it here and perhaps it will be implemented in the next version...

Moderator: Queue Moderators

Post Reply
a1782680475
Posts: 4
Joined: Tue Dec 12, 2023 8:37 am

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

Post by a1782680475 » Tue Dec 12, 2023 8:55 am

Now SizeScreenCoords is always an incorrect value。

ArctionKestutis
Posts: 555
Joined: Mon Mar 14, 2016 9:22 am

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

Post by ArctionKestutis » Tue Dec 12, 2023 4:20 pm

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?

a1782680475
Posts: 4
Joined: Tue Dec 12, 2023 8:37 am

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

Post by a1782680475 » Wed Dec 13, 2023 1:40 am

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.

ArctionKestutis
Posts: 555
Joined: Mon Mar 14, 2016 9:22 am

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

Post by ArctionKestutis » Wed Dec 13, 2023 12:22 pm

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.

a1782680475
Posts: 4
Joined: Tue Dec 12, 2023 8:37 am

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

Post by a1782680475 » Thu Dec 14, 2023 2:44 am

Okay, thank you very much

Post Reply