Skip to content

Commit 81a972b

Browse files
authored
Add Legend.AllowUseFullExtent property (#1743) (#1752)
1 parent 588c7de commit 81a972b

5 files changed

Lines changed: 110 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88
- Example for label placement on BarSeries with non-zero BaseValue (#1726)
99
- Add control over how far from the series the tracker fires (#1736)
1010
- Add option to check distance for result between data points (#1736)
11+
- Legend.AllowUseFullExtent property to control whether legends should be able to use the full extent of the plot (#1743)
1112

1213
### Changed
1314

‎Source/Examples/ExampleLibrary/Examples/LegendExamples.cs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,64 @@ public static PlotModel MultipleLegendsWithSeriesGroups()
387387
return model;
388388
}
389389

390+
[Example("Full width legend")]
391+
public static PlotModel LegendFullWidth()
392+
{
393+
var model = CreateModel(21);
394+
395+
var l1 = new Legend
396+
{
397+
LegendPlacement = LegendPlacement.Outside,
398+
LegendPosition = LegendPosition.TopCenter,
399+
LegendOrientation = LegendOrientation.Horizontal,
400+
AllowUseFullExtent = true,
401+
};
402+
403+
var l2 = new Legend
404+
{
405+
LegendPlacement = LegendPlacement.Outside,
406+
LegendPosition = LegendPosition.BottomCenter,
407+
LegendOrientation = LegendOrientation.Horizontal,
408+
AllowUseFullExtent = true,
409+
};
410+
411+
model.PlotMargins = new OxyThickness(50);
412+
413+
model.Legends.Add(l1);
414+
model.Legends.Add(l2);
415+
416+
model.InvalidatePlot(true);
417+
418+
return model;
419+
}
420+
421+
[Example("Full height legend")]
422+
public static PlotModel LegendFullHeight()
423+
{
424+
var model = CreateModel(21);
425+
426+
var l1 = new Legend
427+
{
428+
LegendPlacement = LegendPlacement.Outside,
429+
LegendPosition = LegendPosition.LeftTop,
430+
LegendOrientation = LegendOrientation.Vertical,
431+
AllowUseFullExtent = true,
432+
};
433+
434+
var l2 = new Legend
435+
{
436+
LegendPlacement = LegendPlacement.Outside,
437+
LegendPosition = LegendPosition.RightTop,
438+
LegendOrientation = LegendOrientation.Vertical,
439+
AllowUseFullExtent = true,
440+
};
441+
442+
model.Legends.Add(l1);
443+
model.Legends.Add(l2);
444+
445+
return model;
446+
}
447+
390448
private static PlotModel CreateModel(int n = 20)
391449
{
392450
var model = new PlotModel { Title = "LineSeries" };

‎Source/OxyPlot/Legends/Legend.Rendering.cs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,35 @@ public override OxyRect GetLegendRectangle(OxySize legendSize)
101101
break;
102102
}
103103

104+
var bounds = this.AllowUseFullExtent
105+
? this.PlotModel.PlotAndAxisArea
106+
: this.PlotModel.PlotArea;
107+
104108
switch (this.LegendPosition)
105109
{
106110
case LegendPosition.TopLeft:
107111
case LegendPosition.BottomLeft:
108-
left = this.PlotModel.PlotArea.Left;
112+
left = bounds.Left;
109113
break;
110114
case LegendPosition.TopRight:
111115
case LegendPosition.BottomRight:
112-
left = this.PlotModel.PlotArea.Right - legendSize.Width;
116+
left = bounds.Right - legendSize.Width;
113117
break;
114118
case LegendPosition.LeftTop:
115119
case LegendPosition.RightTop:
116-
top = this.PlotModel.PlotArea.Top;
120+
top = bounds.Top;
117121
break;
118122
case LegendPosition.LeftBottom:
119123
case LegendPosition.RightBottom:
120-
top = this.PlotModel.PlotArea.Bottom - legendSize.Height;
124+
top = bounds.Bottom - legendSize.Height;
121125
break;
122126
case LegendPosition.LeftMiddle:
123127
case LegendPosition.RightMiddle:
124-
top = (this.PlotModel.PlotArea.Top + this.PlotModel.PlotArea.Bottom - legendSize.Height) * 0.5;
128+
top = (bounds.Top + bounds.Bottom - legendSize.Height) * 0.5;
125129
break;
126130
case LegendPosition.TopCenter:
127131
case LegendPosition.BottomCenter:
128-
left = (this.PlotModel.PlotArea.Left + this.PlotModel.PlotArea.Right - legendSize.Width) * 0.5;
132+
left = (bounds.Left + bounds.Right - legendSize.Width) * 0.5;
129133
break;
130134
}
131135
}

‎Source/OxyPlot/Legends/LegendBase.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ protected override HitTestResult HitTestOverride(HitTestArguments args)
346346
/// <value>The legend position.</value>
347347
public LegendPosition LegendPosition { get; set; }
348348

349+
/// <summary>
350+
/// Gets or sets a value indicating whether the legend should use the full extend of the plot when <see cref="LegendPlacement"/> equals <c>LegendPlacement.Outside</c>.
351+
/// </summary>
352+
/// <value>Whether the legends is allowed to use the full extent of the plot.</value>
353+
public bool AllowUseFullExtent { get; set; }
354+
349355
/// <summary>
350356
/// Makes the LegendOrientation property safe.
351357
/// </summary>

‎Source/OxyPlot/PlotModel/PlotModel.Rendering.cs‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private void RenderTitle(IRenderContext rc)
457457
/// <param name="rc">The rendering context.</param>
458458
private void UpdatePlotArea(IRenderContext rc)
459459
{
460-
var plotArea = new OxyRect(
460+
var plotAndAxisArea = new OxyRect(
461461
this.PlotBounds.Left + this.Padding.Left,
462462
this.PlotBounds.Top + this.Padding.Top,
463463
Math.Max(0, this.Width - this.Padding.Left - this.Padding.Right),
@@ -468,10 +468,10 @@ private void UpdatePlotArea(IRenderContext rc)
468468
if (titleSize.Height > 0)
469469
{
470470
var titleHeight = titleSize.Height + this.TitlePadding;
471-
plotArea = new OxyRect(plotArea.Left, plotArea.Top + titleHeight, plotArea.Width, Math.Max(0, plotArea.Height - titleHeight));
471+
plotAndAxisArea = new OxyRect(plotAndAxisArea.Left, plotAndAxisArea.Top + titleHeight, plotAndAxisArea.Width, Math.Max(0, plotAndAxisArea.Height - titleHeight));
472472
}
473473

474-
plotArea = plotArea.Deflate(this.ActualPlotMargins);
474+
var plotArea = plotAndAxisArea.Deflate(this.ActualPlotMargins);
475475

476476
if (this.IsLegendVisible)
477477
{
@@ -485,9 +485,14 @@ private void UpdatePlotArea(IRenderContext rc)
485485
(l.LegendPosition == LegendPosition.LeftTop || l.LegendPosition == LegendPosition.LeftMiddle || l.LegendPosition == LegendPosition.LeftBottom))))
486486
{
487487
// Find the available size for the legend box
488-
var availableLegendWidth = plotArea.Width;
489-
var availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
490-
plotArea.Height : Math.Min(plotArea.Height, legend.LegendMaxHeight);
488+
var availableLegendWidth = legend.AllowUseFullExtent
489+
? plotAndAxisArea.Width
490+
: plotArea.Width;
491+
var availableLegendHeight = legend.AllowUseFullExtent
492+
? plotAndAxisArea.Height
493+
: plotArea.Height;
494+
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
495+
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
491496

492497
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
493498
legend.LegendSize = lsiz;
@@ -511,9 +516,14 @@ private void UpdatePlotArea(IRenderContext rc)
511516
(l.LegendPosition == LegendPosition.RightTop || l.LegendPosition == LegendPosition.RightMiddle || l.LegendPosition == LegendPosition.RightBottom))))
512517
{
513518
// Find the available size for the legend box
514-
var availableLegendWidth = plotArea.Width;
515-
var availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
516-
plotArea.Height : Math.Min(plotArea.Height, legend.LegendMaxHeight);
519+
var availableLegendWidth = legend.AllowUseFullExtent
520+
? plotAndAxisArea.Width
521+
: plotArea.Width;
522+
var availableLegendHeight = legend.AllowUseFullExtent
523+
? plotAndAxisArea.Height
524+
: plotArea.Height;
525+
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
526+
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
517527

518528
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
519529
legend.LegendSize = lsiz;
@@ -537,9 +547,14 @@ private void UpdatePlotArea(IRenderContext rc)
537547
(l.LegendPosition == LegendPosition.TopLeft || l.LegendPosition == LegendPosition.TopCenter || l.LegendPosition == LegendPosition.TopRight))))
538548
{
539549
// Find the available size for the legend box
540-
var availableLegendWidth = plotArea.Width;
541-
var availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
542-
plotArea.Height : Math.Min(plotArea.Height, legend.LegendMaxHeight);
550+
var availableLegendWidth = legend.AllowUseFullExtent
551+
? plotAndAxisArea.Width
552+
: plotArea.Width;
553+
var availableLegendHeight = legend.AllowUseFullExtent
554+
? plotAndAxisArea.Height
555+
: plotArea.Height;
556+
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
557+
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
543558

544559
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
545560
legend.LegendSize = lsiz;
@@ -563,9 +578,14 @@ private void UpdatePlotArea(IRenderContext rc)
563578
(l.LegendPosition == LegendPosition.BottomLeft || l.LegendPosition == LegendPosition.BottomCenter || l.LegendPosition == LegendPosition.BottomRight))))
564579
{
565580
// Find the available size for the legend box
566-
var availableLegendWidth = plotArea.Width;
567-
var availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
568-
plotArea.Height : Math.Min(plotArea.Height, legend.LegendMaxHeight);
581+
var availableLegendWidth = legend.AllowUseFullExtent
582+
? plotAndAxisArea.Width
583+
: plotArea.Width;
584+
var availableLegendHeight = legend.AllowUseFullExtent
585+
? plotAndAxisArea.Height
586+
: plotArea.Height;
587+
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
588+
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
569589

570590
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
571591
legend.LegendSize = lsiz;

0 commit comments

Comments
 (0)