Skip to content

Commit 87e5471

Browse files
aespitiaswharden
andauthored
Legend: default marker style (ScottPlot#5006)
* changing default shape of legend marker * logic updates for defaults * legend logic updates * formatting * Update CHANGELOG.md --------- Co-authored-by: Scott W Harden <swharden@gmail.com>
1 parent 0bf3f3b commit 87e5471

3 files changed

Lines changed: 77 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ _Not yet on NuGet..._
2020
* Maui: Improve behavior for interactive plots when their user input processor is disabled (#4990, #4989) @Adam--
2121
* Fonts: Added new styling options for weight, slant, density, etc. (#5013, #4873) @aespitia @Christoph-Wagner
2222
* Labels: Added styling options for underline with customizable thickness and offset (#4893) @manaruto
23+
* Legend: Added the ability to customize default marker shape for legend items (#5005, #5006) @aespitia
2324

2425
## ScottPlot 5.0.55
2526
_Published on [NuGet](https://www.nuget.org/profiles/ScottPlot) on 2025-03-22_

src/ScottPlot5/ScottPlot5 Cookbook/Recipes/General/Legend.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override void Execute()
4949
public class ManualLegend : RecipeBase
5050
{
5151
public override string Name => "Manual Legend Items";
52-
public override string Description => "Legends may be constructed manually.";
52+
public override string Description => "Legends may be constructed manually and markers customized.";
5353

5454
[Test]
5555
public override void Execute()
@@ -63,6 +63,7 @@ public override void Execute()
6363
LineColor = Colors.Magenta,
6464
MarkerFillColor = Colors.Magenta,
6565
MarkerLineColor = Colors.Magenta,
66+
MarkerShape = MarkerShape.Cross,
6667
LineWidth = 2,
6768
LabelText = "Alpha"
6869
};
@@ -106,11 +107,49 @@ public override void Execute()
106107
myPlot.Legend.ShadowColor = Colors.Blue.WithOpacity(.2);
107108
myPlot.Legend.ShadowOffset = new(10, 10);
108109

109-
myPlot.Legend.FontSize = 32;
110+
myPlot.Legend.FontSize = 22;
110111
myPlot.Legend.FontName = Fonts.Serif;
111112
}
112113
}
113114

115+
public class LegendOverrideSymbol : RecipeBase
116+
{
117+
public override string Name => "Legend Default Marker";
118+
public override string Description => @"You can override the default rectangular marker used when rendering the legend. It also maintains the desired marker for manually added items.";
119+
120+
[Test]
121+
public override void Execute()
122+
{
123+
var sig1 = myPlot.Add.Signal(Generate.Sin(51));
124+
sig1.LegendText = "Sin";
125+
126+
var sig2 = myPlot.Add.Signal(Generate.Cos(51));
127+
sig2.LegendText = "Cos";
128+
129+
myPlot.Legend.MarkerShapeDefault = MarkerShape.FilledCircle;
130+
131+
132+
LegendItem item1 = new()
133+
{
134+
MarkerColor = Colors.Red,
135+
MarkerShape = MarkerShape.Cross,
136+
LabelText = "Alpha"
137+
};
138+
139+
LegendItem item2 = new()
140+
{
141+
MarkerColor = Colors.Green,
142+
MarkerShape = MarkerShape.FilledSquare,
143+
LabelText = "Beta"
144+
};
145+
146+
myPlot.Legend.ManualItems.Add(item1);
147+
myPlot.Legend.ManualItems.Add(item2);
148+
149+
myPlot.ShowLegend();
150+
}
151+
}
152+
114153
public class LegendOrientation : RecipeBase
115154
{
116155
public override string Name => "Legend Orientation";

src/ScottPlot5/ScottPlot5/Primitives/Legend.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public class Legend(Plot plot) : IPlottable, IHasOutline, IHasBackground, IHasSh
114114
public IEnumerable<LegendItem> LegendItems => LegendItem.None;
115115
public AxisLimits GetAxisLimits() => AxisLimits.NoLimits;
116116

117+
public MarkerShape MarkerShapeDefault { get; set; } = MarkerShape.None;
118+
117119
public bool DisplayPlottableLegendItems { get; set; } = true;
118120

119121
/// <summary>
@@ -292,6 +294,12 @@ private void RenderLayout(SKCanvas canvas, LegendLayout layout)
292294
PixelRect symbolFillOutlineRect = symbolFillRect.Expand(1 - item.OutlineWidth);
293295
PixelLine symbolLine = new(symbolRect.RightCenter, symbolRect.LeftCenter);
294296

297+
if (item.MarkerShape == MarkerShape.None && item.MarkerStyle.Shape == MarkerShape.None)
298+
{
299+
item.MarkerShape = MarkerShapeDefault != MarkerShape.None ? MarkerShapeDefault : MarkerShape.None;
300+
item.MarkerColor = item.MarkerColor == Colors.Transparent ? Colors.Black : item.MarkerColor;
301+
}
302+
295303
item.LabelStyle.Render(canvas, labelRect.LeftCenter, paint, true);
296304

297305
if (ShowItemRectangles_DEBUG)
@@ -300,9 +308,33 @@ private void RenderLayout(SKCanvas canvas, LegendLayout layout)
300308
Drawing.DrawRectangle(canvas, labelRect, Colors.Magenta.WithAlpha(.2));
301309
}
302310

303-
item.LineStyle.Render(canvas, symbolLine, paint);
304-
item.FillStyle.Render(canvas, symbolFillRect, paint);
305-
item.OutlineStyle.Render(canvas, symbolFillOutlineRect, paint);
311+
if (MarkerShapeDefault != MarkerShape.None)
312+
{
313+
item.MarkerStyle.Shape = item.MarkerShape != MarkerShape.None ? item.MarkerShape : MarkerShapeDefault;
314+
315+
//NOTE: tried this but size seems to be defaulting something to something small, so you can barely see the shape, defaulting to font size for now
316+
//item.MarkerStyle.Size = item.MarkerStyle.Size != 0 ? item.MarkerStyle.Size : item.LabelFontSize;
317+
item.MarkerStyle.Size = item.LabelFontSize;
318+
}
319+
else
320+
{
321+
item.MarkerStyle.Shape = item.MarkerShape;
322+
323+
// If the marker size is not set, use the label font size as a fallback
324+
//NOTE: Same as above, tried it, but somehow the default is really small, setting to font size for now.
325+
//item.MarkerStyle.Size = item.MarkerStyle.Shape != MarkerShape.None && item.MarkerStyle.Size != 0? item.MarkerStyle.Size : item.LabelFontSize;
326+
327+
item.MarkerStyle.Size = item.LabelFontSize;
328+
329+
if (item.MarkerShape == MarkerShape.None && item.MarkerStyle.Shape == MarkerShape.None)
330+
{
331+
item.LineStyle.Render(canvas, symbolLine, paint);
332+
}
333+
334+
item.FillStyle.Render(canvas, symbolFillRect, paint);
335+
item.OutlineStyle.Render(canvas, symbolFillOutlineRect, paint);
336+
}
337+
306338
item.MarkerStyle.Render(canvas, symbolRect.Center, paint);
307339
item.ArrowStyle.Render(canvas, symbolLine, paint);
308340

0 commit comments

Comments
 (0)