Skip to content

Commit 5c013b3

Browse files
committed
Fix Auto plot margin not taking width of labels into account (#453)
1 parent 24023ce commit 5c013b3

16 files changed

Lines changed: 237 additions & 218 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ All notable changes to this project will be documented in this file.
4646
- OxyPlot.ImageSharp now targets .NET Standard 1.3 (#1530)
4747
- SkiaRenderContext does not apply pixel snapping when rendering to vector graphic (#1539)
4848
- Mark OxyPlot.PdfExporter and OxyPlot.Pdf.PdfExporter as obsolete (#1527)
49+
- Replace Axis.DesiredSize by Axis.DesiredMargin, change signature of Axis.Measure(...) (#453)
4950

5051
### Removed
5152
- Remove PlotModel.Legends (#644)
@@ -66,6 +67,7 @@ All notable changes to this project will be documented in this file.
6667
- WPF CanvasRenderContext draws ellipses too small by half stroke thickness (#1537)
6768
- Text measurement and rendering in OxyPlot.ImageSharp
6869
- ExampleLibrary reporting annotation-only PlotModels as transposable (#1544)
70+
- Auto plot margin not taking width of labels into account (#453)
6971

7072
## [2.0.0] - 2019-10-19
7173
### Added

Source/Examples/ExampleLibrary/Axes/PolarPlotExamples.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public static PlotModel ArchimedeanSpiral()
2727
Subtitle = "Archimedean spiral with equation r(θ) = θ for 0 < θ < 6π",
2828
PlotType = PlotType.Polar,
2929
PlotAreaBorderThickness = new OxyThickness(0),
30-
PlotMargins = new OxyThickness(60, 20, 4, 40)
3130
};
3231
model.Axes.Add(
3332
new AngleAxis
@@ -81,7 +80,6 @@ public static PlotModel OffsetAngles()
8180
Title = "Offset angle axis",
8281
PlotType = PlotType.Polar,
8382
PlotAreaBorderThickness = new OxyThickness(0),
84-
PlotMargins = new OxyThickness(60, 20, 4, 40)
8583
};
8684

8785
var angleAxis = new AngleAxis
@@ -134,7 +132,6 @@ public static PlotModel SemiCircle()
134132
Title = "Semi-circle polar plot",
135133
PlotType = PlotType.Polar,
136134
PlotAreaBorderThickness = new OxyThickness(0),
137-
PlotMargins = new OxyThickness(60, 20, 4, 40)
138135
};
139136
model.Axes.Add(
140137
new AngleAxis
@@ -168,7 +165,6 @@ public static PlotModel SemiCircleOffsetAngleAxisRange()
168165
Subtitle = "Angle axis range offset to -180 - 180",
169166
PlotType = PlotType.Polar,
170167
PlotAreaBorderThickness = new OxyThickness(0),
171-
PlotMargins = new OxyThickness(60, 20, 4, 40)
172168
};
173169
model.Axes.Add(
174170
new AngleAxis
@@ -205,7 +201,6 @@ public static PlotModel EastWestDirections()
205201
Title = "East/west directions",
206202
PlotType = PlotType.Polar,
207203
PlotAreaBorderThickness = new OxyThickness(0),
208-
PlotMargins = new OxyThickness(60, 20, 4, 40)
209204
};
210205
model.Axes.Add(
211206
new AngleAxis
@@ -253,7 +248,6 @@ public static PlotModel SemiCircleFullPlotArea()
253248
Subtitle = "The center can be move using the right mouse button",
254249
PlotType = PlotType.Polar,
255250
PlotAreaBorderThickness = new OxyThickness(1),
256-
PlotMargins = new OxyThickness(60, 20, 4, 40)
257251
};
258252
model.Axes.Add(
259253
new AngleAxisFullPlotArea
@@ -289,7 +283,6 @@ public static PlotModel ArchimedeanSpiralFullPlotArea()
289283
Subtitle = "The center can be move using the right mouse button",
290284
PlotType = PlotType.Polar,
291285
PlotAreaBorderThickness = new OxyThickness(1),
292-
PlotMargins = new OxyThickness(60, 20, 4, 40)
293286
};
294287
model.Axes.Add(
295288
new AngleAxisFullPlotArea

Source/Examples/ExampleLibrary/Examples/PlotModelExamples.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ public static PlotModel PlotMargins()
9494
return model;
9595
}
9696

97+
[Example("Auto PlotMargins")]
98+
public static PlotModel AutoPlotMarginAndAxisLabelWidths()
99+
{
100+
var plotModel1 = new PlotModel { Title = "PlotMargins = (NaN,NaN,NaN,NaN)" };
101+
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = 1e8, EndPosition = 0.5, StringFormat = "f0" });
102+
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = 80, StartPosition = 0.5, StringFormat = "f0" });
103+
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = 0, Maximum = 1e8, StringFormat = "f0", Angle = -90 });
104+
return plotModel1;
105+
}
106+
97107
[Example("No model")]
98108
public static PlotModel NoModel()
99109
{

Source/Examples/ExampleLibrary/Issues/Issues.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,14 +1725,6 @@ public static PlotModel AxisMinMax()
17251725
return plotModel1;
17261726
}
17271727

1728-
[Example("#453: Auto plot margin and width of labels")]
1729-
public static PlotModel AutoPlotMarginAndAxisLabelWidths()
1730-
{
1731-
var plotModel1 = new PlotModel { Title = "Auto plot margin not taking width of axis tick labels into account" };
1732-
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = -1e8, Maximum = 1e8 });
1733-
return plotModel1;
1734-
}
1735-
17361728
/// <summary>
17371729
/// Creates a demo PlotModel with MinimumRange defined
17381730
/// and with series with values which are within this range.

Source/OxyPlot.Tests/Axes/AxisTests.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public void D04_InvalidLogAxis()
432432
/// Test DesiredSize property to see if working property
433433
/// </summary>
434434
[Test]
435-
public void Axis_DesiredSize()
435+
public void Axis_DesiredMargin()
436436
{
437437
var xaxis = new LinearAxis { Position = AxisPosition.Bottom, Title = "X-axis" };
438438
var yaxis = new LinearAxis { Position = AxisPosition.Left, Title = "Y-axis" };
@@ -451,18 +451,70 @@ public void Axis_DesiredSize()
451451

452452
// initial setting
453453
plot.UpdateAndRenderToNull(800, 600);
454-
Assert.That(yaxis.DesiredSize.Width, Is.EqualTo(35.0).Within(0.5), "y-axis width");
455-
Assert.That(yaxis.DesiredSize.Height, Is.EqualTo(0.0).Within(1e-6), "y-axis height");
454+
Assert.That(yaxis.DesiredMargin.Left, Is.EqualTo(35.0).Within(0.5), "y-axis left");
455+
Assert.That(yaxis.DesiredMargin.Top, Is.EqualTo(5).Within(0.5), "y-axis top");
456+
Assert.That(yaxis.DesiredMargin.Right, Is.EqualTo(0.0).Within(1e-6), "y-axis right");
457+
Assert.That(yaxis.DesiredMargin.Bottom, Is.EqualTo(5).Within(0.5), "y-axis bottom");
456458

457-
Assert.That(xaxis.DesiredSize.Width, Is.EqualTo(0.0).Within(1e-6), "x-axis width");
458-
Assert.That(xaxis.DesiredSize.Height, Is.EqualTo(35.0).Within(0.5), "x-axis height");
459+
Assert.That(xaxis.DesiredMargin.Left, Is.EqualTo(5).Within(0.5), "x-axis left");
460+
Assert.That(xaxis.DesiredMargin.Top, Is.EqualTo(0.0).Within(1e-6), "x-axis top");
461+
Assert.That(xaxis.DesiredMargin.Right, Is.EqualTo(5).Within(0.5), "x-axis right");
462+
Assert.That(xaxis.DesiredMargin.Bottom, Is.EqualTo(35.0).Within(0.5), "x-axis bottom");
459463

460464
// larger numbers on axis -> larger desired size
461465
yaxis.Zoom(10000, 11000);
466+
xaxis.Zoom(10000, 11000);
462467
plot.UpdateAndRenderToNull(800, 600);
463468

464-
Assert.That(yaxis.DesiredSize.Width, Is.EqualTo(50.0).Within(0.5), "y-axis width");
465-
Assert.That(yaxis.DesiredSize.Height, Is.EqualTo(0.0).Within(1e-6), "y-axis height");
469+
Assert.That(yaxis.DesiredMargin.Left, Is.EqualTo(50.0).Within(0.5), "y-axis left");
470+
Assert.That(yaxis.DesiredMargin.Top, Is.EqualTo(5).Within(0.5), "y-axis top");
471+
Assert.That(yaxis.DesiredMargin.Right, Is.EqualTo(0.0).Within(1e-6), "y-axis right");
472+
Assert.That(yaxis.DesiredMargin.Bottom, Is.EqualTo(5).Within(0.5), "y-axis bottom");
473+
474+
Assert.That(xaxis.DesiredMargin.Left, Is.EqualTo(12.5).Within(0.5), "x-axis left");
475+
Assert.That(xaxis.DesiredMargin.Top, Is.EqualTo(0.0).Within(1e-6), "x-axis top");
476+
Assert.That(xaxis.DesiredMargin.Right, Is.EqualTo(12.5).Within(0.5), "x-axis right");
477+
Assert.That(xaxis.DesiredMargin.Bottom, Is.EqualTo(35.0).Within(0.5), "x-axis bottom");
478+
}
479+
480+
/// <summary>
481+
/// Test DesiredSize property with axis start and end position
482+
/// </summary>
483+
[Test]
484+
public void Axis_DesiredMargin_WithPosition()
485+
{
486+
var plot = new PlotModel();
487+
var axis1 = new LinearAxis { Position = AxisPosition.Bottom, StartPosition = 0, EndPosition = 0.5, Title = "X-axis 1" };
488+
var axis2 = new LinearAxis { Position = AxisPosition.Bottom, StartPosition = 1, EndPosition = 0.5, Title = "X-axis 2" };
489+
plot.Axes.Add(axis1);
490+
plot.Axes.Add(axis2);
491+
492+
axis1.Zoom(0, 80);
493+
axis2.Zoom(0, 80);
494+
plot.UpdateAndRenderToNull(800, 600);
495+
Assert.That(axis1.DesiredMargin.Left, Is.EqualTo(5).Within(0.5), "axis1 left");
496+
Assert.That(axis1.DesiredMargin.Top, Is.EqualTo(0).Within(1e-6), "axis1 top");
497+
Assert.That(axis1.DesiredMargin.Right, Is.EqualTo(0).Within(1e-6), "axis1 right");
498+
Assert.That(axis1.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis1 bottom");
499+
500+
Assert.That(axis2.DesiredMargin.Left, Is.EqualTo(0d).Within(1e-6), "axis2 left");
501+
Assert.That(axis2.DesiredMargin.Top, Is.EqualTo(0d).Within(1e-6), "axis2 top");
502+
Assert.That(axis2.DesiredMargin.Right, Is.EqualTo(5).Within(0.5), "axis2 right");
503+
Assert.That(axis2.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis2 bottom");
504+
505+
// larger numbers on axis -> larger desired size
506+
axis1.Zoom(10000, 11000);
507+
axis2.Zoom(10000, 11000);
508+
plot.UpdateAndRenderToNull(800, 600);
509+
Assert.That(axis1.DesiredMargin.Left, Is.EqualTo(12.5).Within(0.5), "axis1 left");
510+
Assert.That(axis1.DesiredMargin.Top, Is.EqualTo(0).Within(1e-6), "axis1 top");
511+
Assert.That(axis1.DesiredMargin.Right, Is.EqualTo(0).Within(1e-6), "axis1 right");
512+
Assert.That(axis1.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis1 bottom");
513+
514+
Assert.That(axis2.DesiredMargin.Left, Is.EqualTo(0d).Within(1e-6), "axis2 left");
515+
Assert.That(axis2.DesiredMargin.Top, Is.EqualTo(0d).Within(1e-6), "axis2 top");
516+
Assert.That(axis2.DesiredMargin.Right, Is.EqualTo(12.5).Within(0.5), "axis2 right");
517+
Assert.That(axis2.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis2 bottom");
466518
}
467519

468520
/// <summary>
@@ -694,4 +746,4 @@ public void Axis_Toggle_Between_Linear_And_Log_Axis()
694746
Assert.AreEqual(0.004, plot.Axes[0].DataMinimum);
695747
}
696748
}
697-
}
749+
}

Source/OxyPlot.Tests/PlotModel/PlotModelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ public void AutoPlotMargins()
155155
plot.Axes.Add(horizontalAxis);
156156
plot.UpdateAndRenderToNull(800, 600);
157157
Assert.That(plot.ActualPlotMargins.Left, Is.EqualTo(26).Within(1), "left");
158-
Assert.That(plot.ActualPlotMargins.Top, Is.EqualTo(0).Within(1), "top");
159-
Assert.That(plot.ActualPlotMargins.Right, Is.EqualTo(0).Within(1), "right");
158+
Assert.That(plot.ActualPlotMargins.Top, Is.EqualTo(5).Within(1), "top");
159+
Assert.That(plot.ActualPlotMargins.Right, Is.EqualTo(7.5).Within(1), "right");
160160
Assert.That(plot.ActualPlotMargins.Bottom, Is.EqualTo(21).Within(1), "bottom");
161161
}
162162

Binary file not shown.
Binary file not shown.
Binary file not shown.

Source/OxyPlot.Wpf.Tests/OxyPlot.Wpf.Tests.csproj

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,4 @@
2020
<ItemGroup>
2121
<None Include="app.config" />
2222
</ItemGroup>
23-
<ItemGroup>
24-
<Content Include="Baseline\PngExporterTests_ExportWithResolution_115dpi.png">
25-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26-
</Content>
27-
<Content Include="Baseline\PngExporterTests_ExportWithResolution_192dpi.png">
28-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29-
</Content>
30-
<Content Include="Baseline\PngExporterTests_ExportWithResolution_301dpi.png">
31-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32-
</Content>
33-
</ItemGroup>
3423
</Project>

0 commit comments

Comments
 (0)