Annotations are broken. Can't draw on the canvas.

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Annotations are broken. Can't draw on the canvas.

Post by TLC » Tue Oct 29, 2024 5:36 pm

Steema.TeeChart.NET.MAUI v6.2024.8.29

Seems that the following is the culprit:

Code: Select all

Microsoft.Maui.Graphics.Platform.PlatformCanvas

Seems like the CurrentState is null. It should be an instance of PlatformCanvasState.

Code: Select all

	public override void FillRectangle(float x, float y, float width, float height)
	{
		var rectX = x;
		var rectY = y;
		var rectWidth = width;
		var rectHeight = height;

		_canvas.DrawRect(rectX, rectY, rectX + rectWidth, rectY + rectHeight, CurrentState.FillPaintWithAlpha);
	}
AnnotationTool is a sub-classed Steema.TeeChart.Tools.RectangleTool in the stack trace below.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Maui.Graphics.Platform.PlatformCanvas.FillRectangle(Single x, Single y, Single width, Single height) in /_/src/Graphics/src/Graphics/Platforms/Android/PlatformCanvas.cs:line 473
at Microsoft.Maui.Graphics.ScalingCanvas.FillRectangle(Single x, Single y, Single width, Single height) in /_/src/Graphics/src/Graphics/ScalingCanvas.cs:line 167
at Microsoft.Maui.Graphics.CanvasExtensions.FillRectangle(ICanvas target, Rect rect) in /_/src/Graphics/src/Graphics/CanvasExtensions.cs:line 22
at Steema.TeeChart.Maui.Drawing.CanvasMaui.Rectangle(Rect r)
at <project>.AnnotationTool.DrawText(CanvasMaui& graphics, ReadOnlyRectangle& bounds) in <project>\Graphs\AnnotationTool.cs:line 241
at <project>.AnnotationTool.Draw(CanvasMaui& graphics, Line& line, DashboardResult& result, ReadOnlyPoint& target, ReadOnlyPoint& source, Int32& currentPointIndex, Boolean& floatingAnnotations, Double& annotationOffset, CultureInfo& culture) in <project>\Graphs\AnnotationTool.cs:line 189
Visual Studio 2022 versions.7z
(2.83 KiB) Downloaded 362 times

Edu
Newbie
Newbie
Posts: 39
Joined: Tue Nov 28, 2023 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by Edu » Mon Nov 04, 2024 10:37 am

Hello,

I have tried to reproduce your issue but creating and setting up Annotations seems to work as expected on my end.
Could you please share additional details or code context so we can assist you further?

Best regards,
Edu
Edu
Steema Support

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Tue Nov 05, 2024 4:16 pm

As a part of the Annotation, I need to draw an arrow from the corner of the popup to the point in question. A lot of math is done prior to this, but the following is crashing due the stack trace bellow. I tried the native method and doing a custom arrow. Both crash.

Code: Select all

    private void DrawArrow( ref readonly CanvasMaui    graphics,
                            ref readonly ReadOnlyPoint target,
                            ref readonly ReadOnlyPoint source,
                            double                     arrowWidth  = 10,
                            double                     arrowHeight = 10 )
    {
        graphics.Line( source, target );

        double        halfArrowWidth = arrowWidth / 2;
        double        dx             = target.X - source.X;
        double        dy             = target.Y - source.Y;
        double        length         = Math.Sqrt( dx * dx + dy * dy );
        double        ux             = dx / length;
        double        uy             = dy / length; // (ux,uy) represents the unit vector in the direction from source(x1,y1) to target(x2​,y2​).
        double        tipX           = target.X + arrowHeight * ux;
        double        tipY           = target.Y + arrowHeight * uy;
        ReadOnlyPoint tip            = new(tipX, tipY);
        ReadOnlyPoint left           = new(target.X + halfArrowWidth * -uy, target.Y - halfArrowWidth * ux);
        ReadOnlyPoint right          = new(target.X + halfArrowWidth * -uy, target.Y - halfArrowWidth * ux);
        PathGeometry  path           = new();

        path.AppendPath( new PathF( tip ) );
        path.AppendPath( new PathF( left ) );
        path.AppendPath( new PathF( right ) );
        path.AppendPath( new PathF( tip ) );

        graphics.FillRegion( ArrowPen.DrawingPen, ArrowPen.DrawingPen, path );

        /*
        graphics.Arrow( true,
                        source,
                        target,
                        arrowWidth,
                        arrowHeight,
                        0 );
                  	
        */
    }

Code: Select all

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Maui.Graphics.Platform.PlatformCanvas.PlatformDrawLine(Single x1, Single y1, Single x2, Single y2)
			in /_/src/Graphics/src/Graphics/Platforms/Android/PlatformCanvas.cs:line 363
   at Microsoft.Maui.Graphics.AbstractCanvas`1[[Microsoft.Maui.Graphics.Platform.PlatformCanvasState, Microsoft.Maui.Graphics,
		Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].DrawLine(Single x1, Single y1, Single x2, Single y2) 
			in /_/src/Graphics/src/Graphics/AbstractCanvas.cs:line 139
   at Microsoft.Maui.Graphics.ScalingCanvas.DrawLine(Single x1, Single y1, Single x2, Single y2)
			in /_/src/Graphics/src/Graphics/ScalingCanvas.cs:line 111
   at Steema.TeeChart.Maui.Drawing.CanvasMaui.Line(Double x0, Double y0, Double x1, Double y1)
   at Steema.TeeChart.Maui.Drawing.Graphics3DBase.Line(Point p0, Point p1)
   at <project>.UI.AnnotationTool.DrawArrow(CanvasMaui& graphics, ReadOnlyPoint& target, 
		ReadOnlyPoint& source, Double arrowWidth, Double arrowHeight)
			in <project>\Graphs\AnnotationTool.cs:line 211
   at <project>.UI.AnnotationTool.Draw(CanvasMaui& graphics, Line& line, DashboardResult& result, 
		ReadOnlyPoint& target, ReadOnlyPoint& source, Int32& currentPointIndex, Boolean& floatingAnnotations, 
		Double& annotationOffset, CultureInfo& culture) 
			in <project>\Graphs\AnnotationTool.cs:line 195

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Tue Nov 05, 2024 5:20 pm

If i try to draw the popup first, it still crashes. The native RectangleTool.DrawText method also crashes. See the previous stack traces. The arrow is never drawn. Oddly though, the popup still is displayed but nothing else, regardless of the call order(text or arrow first)

Code: Select all

    private void DrawText( ref readonly CanvasMaui graphics, ref readonly ReadOnlyRectangle bounds )
    {
        ChartFont font = (ChartFont)Shape.Font;
        graphics.Rectangle( bounds ); // boom here

        double height = graphics.TextHeight( font, Text );
        double x      = bounds.X;
        double y      = bounds.Y + (bounds.Height - height) / 2;

        graphics.TextOut( font, x, y, Text );
    }

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Tue Nov 05, 2024 5:29 pm

Here is a screenshot of so you can see it. The toast on the image is to help debug it.

In this case, the arrow should be from the bottom left corner of the popup to the 3rd point.

Code: Select all

        #if DEBUG
            _ = Dialogs.Toast( $"{nameof(AnnotationTool)}.{nameof(Draw)}.{Active}.{currentPointIndex}.{line.Count}" );
        #endif
90259.jpeg
90259.jpeg (433.39 KiB) Viewed 13255 times

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Annotations are broken. Can't draw on the canvas.

Post by Pep » Fri Nov 08, 2024 10:30 am

Hello,
does the following code works for you ? it should draw an Arrow from left bottom corner to the third point in Chart.

Code: Select all

        private void TChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.IGraphics3D g)
        {
            
            var series = tChart1.Chart.Series[0];

            int pointIndex = 2;  // Index 3 
            double xValue = series.XValues[pointIndex];
            double yValue = series.YValues[pointIndex];

            int xScreen = series.CalcXPos(pointIndex);
            int yScreen = series.CalcYPos(pointIndex);

            int xStart = series.CalcXPosValue(0);
            int yStart = series.CalcYPosValue(0);


            g.Arrow(false, new System.Drawing.Point(xStart, yStart), new System.Drawing.Point(xScreen, yScreen), 3, 20, 0);
        }

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Fri Nov 08, 2024 5:33 pm

Pep wrote:
Fri Nov 08, 2024 10:30 am
Hello,
does the following code works for you ? it should draw an Arrow from left bottom corner to the third point in Chart.

Code: Select all

        private void TChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.IGraphics3D g)
        {
            
            var series = tChart1.Chart.Series[0];

            int pointIndex = 2;  // Index 3 
            double xValue = series.XValues[pointIndex];
            double yValue = series.YValues[pointIndex];

            int xScreen = series.CalcXPos(pointIndex);
            int yScreen = series.CalcYPos(pointIndex);

            int xStart = series.CalcXPosValue(0);
            int yStart = series.CalcYPosValue(0);


            g.Arrow(false, new System.Drawing.Point(xStart, yStart), new System.Drawing.Point(xScreen, yScreen), 3, 20, 0);
        }
The event is triggered, but nothing is drawn. i also tried using the original CanvasMaui vs the event's IGraphics3D g parameter but there was no change; nothing is rendered.

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Annotations are broken. Can't draw on the canvas.

Post by Pep » Mon Nov 11, 2024 8:23 am

Hello,
which platform are you using?
Here it's working fine, just tested on Android or Windows.
canvas-draw.jpg
canvas-draw.jpg (174.02 KiB) Viewed 12653 times

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Mon Nov 11, 2024 5:13 pm

I'm on android. Could you post the code for your demo?

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Annotations are broken. Can't draw on the canvas.

Post by Pep » Tue Nov 12, 2024 2:30 pm

Hello,
sure, here it's the code used:

Code: Select all


        public MainPage()
        {
            InitializeComponent();
                      
            var line = new FastLine(tChart1.Chart);

            line.LinePen.Style = Steema.TeeChart.Drawing.DashStyle.Dash;
            line.Add(10, 10);
            line.Add(20, 40);
            line.Add(30, 20);
            line.Add(40, 80);
            line.Add(50, 40);
            line.Add(60, 50);
            line.Add(70, 30);

            tChart1.AfterDraw += TChart1_AfterDraw;
        }
        
        private void TChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.IGraphics3D g)
        {
            
            var series = tChart1.Chart.Series[0];

            int pointIndex = 2;  // Index 3 
            double xValue = series.XValues[pointIndex];
            double yValue = series.YValues[pointIndex];

            int xScreen = series.CalcXPos(pointIndex);
            int yScreen = series.CalcYPos(pointIndex);

            int xStart = series.CalcXPosValue(0);
            int yStart = series.CalcYPosValue(0);


            g.Arrow(false, new System.Drawing.Point(xStart, yStart), new System.Drawing.Point(xScreen, yScreen), 3, 20, 0);
        }


TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Wed Nov 13, 2024 9:23 pm

Not sure how, but got the arrow to show up. but as related to the issue Steema.TeeChart.Tools.NearestPoint Change event doesn't trigger http://support.steema.com/viewtopic.php ... e7a75ee146, it's always at the first point. doesn't change no matter where i click or drag.

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Annotations are broken. Can't draw on the canvas.

Post by Pep » Mon Nov 18, 2024 11:17 am

Hello,
ok, let me check and prepare an example of using the NearestPoint tool on android.

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Tue Nov 19, 2024 8:07 pm

Somehow, the annotation popup hangs around after it is redrawn/invalidated. Only the most recent previous popup is visible aside from the current one. In otherwords, once you click again, it will remove the 2nd oldest, keeping the previous and draws the new one.
The arrow from is the current one, the other is the old one.
91125.jpeg
91125.jpeg (251.06 KiB) Viewed 8909 times

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Annotations are broken. Can't draw on the canvas.

Post by Pep » Wed Nov 20, 2024 6:06 am

Hello,
which code are you using to draw the annotation?
Drawing the annotation inside the OnAfterDraw event should do the trick.
If you send me code that you're using I can check it here.

Thanks

TLC
Newbie
Newbie
Posts: 73
Joined: Mon Apr 08, 2019 12:00 am

Re: Annotations are broken. Can't draw on the canvas.

Post by TLC » Wed Nov 20, 2024 4:15 pm

Pep wrote:
Wed Nov 20, 2024 6:06 am
Hello,
which code are you using to draw the annotation?
Drawing the annotation inside the OnAfterDraw event should do the trick.
If you send me code that you're using I can check it here.

Thanks
Unfortunately, I can't send the source code as its private. it is possible the AfterDraw event is getting called multiple times?

Post Reply