oxyplot / oxyplot-xamarin

OxyPlot for Xamarin.Mac and Xamarin.Forms
MIT License
64 stars 59 forks source link

System.NullReferenceException in OxyPlot.dll!OxyPlot.PlotElementUtilities.GetClippingRect after upgrading to 2.1.0-unstable.1455 #112

Closed edwwsw closed 1 year ago

edwwsw commented 3 years ago

Upgraded from an unstable 2.1 build from a year ago. One of the applications that uses OxyPlot crashes now (worked before). I haven't completely isolated the issue, but I believe the cause is related to adding annotations before the OxyPlot grid is first rendered. Call stack below.

OxyPlot.dll!OxyPlot.PlotElementUtilities.GetClippingRect(OxyPlot.IXyAxisPlotElement element) Unknown OxyPlot.dll!OxyPlot.Annotations.TransposableAnnotation.GetClippingRect() Unknown OxyPlot.dll!OxyPlot.PlotModel.RenderPlotElements(System.Collections.Generic.IEnumerable plotElements, OxyPlot.IRenderContext rc, System.Action renderAction) Unknown OxyPlot.dll!OxyPlot.PlotModel.RenderAnnotations(OxyPlot.IRenderContext rc, OxyPlot.Annotations.AnnotationLayer layer) Unknown OxyPlot.dll!OxyPlot.PlotModel.RenderOverride(OxyPlot.IRenderContext rc, OxyPlot.OxyRect rect) Unknown OxyPlot.dll!OxyPlot.PlotModel.OxyPlot.IPlotModel.Render(OxyPlot.IRenderContext rc, OxyPlot.OxyRect rect) Unknown OxyPlot.Wpf.Shared.dll!OxyPlot.Wpf.PlotViewBase.RenderOverride() Unknown OxyPlot.Wpf.dll!OxyPlot.Wpf.PlotView.RenderOverride() Unknown OxyPlot.Wpf.Shared.dll!OxyPlot.Wpf.PlotViewBase.Render() Unknown OxyPlot.Wpf.dll!OxyPlot.Wpf.PlotView.OnRender(System.Windows.Media.DrawingContext drawingContext) Unknown PresentationCore.dll!System.Windows.UIElement.Arrange(System.Windows.Rect finalRect) Unknown

tomwimmenhove commented 2 years ago

I'm having the same issue. In my case it happens when the element argument to GetClippingRect() is a LineSeries. It's fine when the LineSeries is instantiated at the same time with the plot, but it seems to crash whenever it's created at some later point in time. Haven't been able to find a work-around yet.

tomwimmenhove commented 2 years ago

It seems like LineSeries.EnsureAxis() isn't always called, resulting in nullreference exceptions.

rifaia commented 2 years ago

@tomwimmenhove , I do have the same problem. After adding new LineSeries, calling InvalidatePlot(true) seems to solve the problem in my case.

janusw commented 1 year ago

I released OxyPlot.Xamarin.Forms version 2.1.0 yesterday, which should work well with OxyPlot.Core 2.1.0 and hopefully fix problems like this.

@edwwsw @tomwimmenhove @rifaia Can anyone of you confirm that it is fixed?

fguiro commented 1 year ago

It is fixed. I had the same problem.

` var IndicatorOutputValuesGrouped = IndicatorOutputValues.GroupBy(o => o.Name);

        foreach (var dailyRecordItemGroup in IndicatorOutputValuesGrouped)
        {
            var indicateurLineSeries = new LineSeries()
            {
                Title = dailyRecordItemGroup.Key,
                LineLegendPosition = LineLegendPosition.None,
                InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline
            };

            foreach (var dailyRecordItem in dailyRecordItemGroup)
            {
                indicateurLineSeries.Points.Add(new DataPoint(dailyRecordItem.NumeroOrdre, RandomHelper.GetRandom(1, 10)));
            }

            IndicateurPlotModel.Series.Add(indicateurLineSeries);
        }

        IndicateurPlotModel.Legends.Add(new Legend()
        {
            LegendPosition = LegendPosition.TopRight,
            LegendItemAlignment = HorizontalAlignment.Right,
            LegendOrientation = LegendOrientation.Horizontal,
            LegendPlacement = LegendPlacement.Outside,
        });

It's not necessary to addInvalidatePlot(true);`

Thanks lot of!

janusw commented 1 year ago

It is fixed. I had the same problem.

Awesome. Thanks for the confirmation! 🥳

jebissey commented 1 year ago

Hello,

The same problem still exists for OxyPlot.Wpf 2.1.2

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=OxyPlot
  StackTrace:
   at OxyPlot.PlotElementUtilities.GetClippingRect(IXyAxisPlotElement element) in C:\Users\Murdo\source\repos\oxyplot\Source\OxyPlot\PlotModel\PlotElementUtilities.cs:line 21

InvalidatePlot(true) works here too.

janusw commented 1 year ago

The same problem still exists for OxyPlot.Wpf 2.1.2

Well, WPF is in the main oxyplot repository, not oxyplot-xamarin. Please report this at https://github.com/oxyplot/oxyplot/issues.