Skip to content

AxisLines: Allow them to be excluded from the legend#3612

Merged
swharden merged 5 commits intoScottPlot:mainfrom
MCF:disable-lines-in-legend
Apr 8, 2024
Merged

AxisLines: Allow them to be excluded from the legend#3612
swharden merged 5 commits intoScottPlot:mainfrom
MCF:disable-lines-in-legend

Conversation

@MCF
Copy link
Contributor

@MCF MCF commented Apr 7, 2024

If an AxisLine (horizontal or vertical) has its Text property set it would always be included in any legend - both the automatic or manually built legends. The Text property is also used to display the text along the appropriate axis for the line so it is a valid use case to want the Text set but not add the line to the legend.

This change adds an ExcludeFromLegend property to the AxisLine base class which defaults to false (no change to current default behavior). Setting this property to true keeps the line out of the legend regardless of the Text property being set or not.

A cookbook recipe was added demonstrating how the AxisLines and legend work together. The recipe code and result is shown below.

myPlot.Add.Signal(Generate.Sin());
myPlot.Add.Signal(Generate.Cos());

var axLine1 = myPlot.Add.VerticalLine(24);
axLine1.Text = "Line 1";

var axLine2 = myPlot.Add.HorizontalLine(0.75);

var axLine3 = myPlot.Add.VerticalLine(37);
axLine3.Text = "Line 3";
axLine3.ExcludeFromLegend = true;

var axLine4 = myPlot.Add.HorizontalLine(0.25);
axLine4.Text = "Line 4";

var axLine5 = myPlot.Add.HorizontalLine(-.75);
axLine5.Text = "Line 5";
axLine5.ExcludeFromLegend = true;

myPlot.ShowLegend();

AxisLineInLegend

MCF added 2 commits April 7, 2024 12:31
If an AxisLine (horizontal or vertical) has its Text property set it
would always be included in anyt legend - both the automatic or manually
built legends. The Text property is also used to display the text along
the appropriate axis for the line so it is a valid use case to want the
Text set but not add the line to the legend.

This change adds an ExcludeFromLegend property to the AxisLine base
class which defaults to false (no change to current default behavior).
Setting this property to true keeps the line out of the legend
regardless of the Text property being set or not.
Demonstrates how the AxisLine's Text and ExcludeFromLegend properties
affects the legend.
@MCF
Copy link
Contributor Author

MCF commented Apr 7, 2024

I've realized there is a corollary use case that is not fixed by this change: if the Text/Label is only wanted in the legend and not at the end of the drawn line on the axis. That is currently impossible to do and still impossible with this change.

A better, general solution to all of this may be:

  1. Separate the Label from the Text property. They can both be set independently.
  2. The Text property controls the visibility of what is displayed on the axis.
  3. The Label property controls the visibility of any legend item.

This feels more consistent with (all?) other plottables that are added to the legend. AFAIK it is the Label property that controls legend visibility for all the rest. I think it would be clearer if the Text property is separate and only controls the along axis annotation. If you want both, you just set Text and Label to the same thing. I think it is very unlikely you would want both, but there is no harm in supporting all use cases.

If you feel this proposed solution is better I'd be happy to close this PR and put a new one together that implements what I've described above.

@swharden
Copy link
Member

swharden commented Apr 8, 2024

Hi @MCF, this looks great, thanks!

Regarding alternative approaches, I'll add a flag in the Label class to allow the legend to exclude it even if text is present.

@swharden swharden enabled auto-merge April 8, 2024 23:05
@swharden swharden merged commit b01ad7e into ScottPlot:main Apr 8, 2024
@MCF MCF deleted the disable-lines-in-legend branch April 9, 2024 05:11
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.

2 participants