Skip to content

FormsPlot: Fix exception when right mouse clicking on a plot with Crosshair.#1794

Merged
swharden merged 6 commits intoScottPlot:mainfrom
MareMare:1791-getlegenditems-maybe-null
Apr 30, 2022
Merged

FormsPlot: Fix exception when right mouse clicking on a plot with Crosshair.#1794
swharden merged 6 commits intoScottPlot:mainfrom
MareMare:1791-getlegenditems-maybe-null

Conversation

@MareMare
Copy link
Contributor

Purpose:
I tried solve #1791.

Fixed a NullReferenceException exception raised by right mouse click on a WinForms Plot with Crosshair.

@StendProg
Copy link
Contributor

An alternative solution would be return a default legend object in Сrosshair Plottable with a null or empty label field. Then there would be no need to change the existing filter.

@MareMare
Copy link
Contributor Author

@StendProg, Thank you for your reply.

An alternative solution would be return a default legend object in Сrosshair Plottable with a null or empty label field.

So you mean, for example, the following?

public LegendItem[] GetLegendItems() => Array.Empty<LegendItem>();

I thought about that too, but I did the same with the Legend.UpdateLegendItems method as it does something similar.

Shall I change to the solution you suggested?

@StendProg
Copy link
Contributor

I mean something like this:

public class Crosshair
{
...
 public LegendItem[] GetLegendItems() => new LegendItem[]{ new LegendItem(this){label = null}};
...
}

We only have 1 Plottable which is out of concept and returns null instead of a LegendItems. Instead of adapting the concept of processing (filters that may be or will appear elsewhere) to a single exception (Crosshair), it may be better to adapt this exception to an existing concept.
I don't know which is better for sure, so the decision is yours, I just pointed out an alternative.

@MareMare
Copy link
Contributor Author

MareMare commented Apr 22, 2022

Sorry for the delay.
That's what you meant. Thank you for telling me correctly.

public interface IPlottable
{
  ...
  /// <summary>
  /// Returns items to show in the legend. Most plottables return a single item. in this array will appear in the legend.
  /// Plottables which never appear in the legend can return null.
  /// </summary>
  LegendItem[] GetLegendItems();
  ...
}

As stated in the documentation comments for the IPlottable.GetLegendItems() method, I believe that cross hairs are never put out in the legend.

Therefore, I would like to return null for the said method (Crosshair.GetLegendItems()) as it is now, and use it through a filter.

@swharden
Copy link
Member

Thank you @MareMare and @StendProg!

We only have 1 Plottable which is out of concept and returns null instead of a LegendItems. Instead of adapting the concept of processing (filters that may be or will appear elsewhere) to a single exception (Crosshair), it may be better to adapt this exception to an existing concept.

I will implement this now to bring Crosshair into consistency with the rest of the plottables provided with this library

Then there would be no need to change the existing filter

I am happy the filter was updated to protect against null because, even though none of ScottPlot's plottables return null, the user may create their own IPlottable which does, and this fix protects against that case.

In ScottPlot 5 I'll use the nullable project feature to make it clear when functions are not supposed to return null, and it makes sense to me that GetLegendItems() should always return an array (even an empty one) to prevent this issue in the future.

@swharden
Copy link
Member

Quick update, wow, lots of plottables were returning null! 4bcf592

Returning null was even recommended in the XML documentation of IPlottable 😅 I fixed that

@swharden swharden enabled auto-merge April 30, 2022 17:51
@swharden swharden merged commit 237f34d into ScottPlot:main Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FormsPlot: default right-click menu throws exception for some plot types

3 participants