Skip to content

Commit

Permalink
Merge pull request oxyplot#1562 from Jonarw/margin
Browse files Browse the repository at this point in the history
Fix Auto plot margin not taking width of labels into account (oxyplot#453)
  • Loading branch information
Jonarw committed May 16, 2020
2 parents 12979ae + 5c013b3 commit 50e9eb0
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 218 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ All notable changes to this project will be documented in this file.
- OxyPlot.ImageSharp now targets .NET Standard 1.3 (#1530)
- SkiaRenderContext does not apply pixel snapping when rendering to vector graphic (#1539)
- Mark OxyPlot.PdfExporter and OxyPlot.Pdf.PdfExporter as obsolete (#1527)
- Replace Axis.DesiredSize by Axis.DesiredMargin, change signature of Axis.Measure(...) (#453)

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

## [2.0.0] - 2019-10-19
### Added
Expand Down
7 changes: 0 additions & 7 deletions Source/Examples/ExampleLibrary/Axes/PolarPlotExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static PlotModel ArchimedeanSpiral()
Subtitle = "Archimedean spiral with equation r(θ) = θ for 0 < θ < 6π",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(0),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};
model.Axes.Add(
new AngleAxis
Expand Down Expand Up @@ -81,7 +80,6 @@ public static PlotModel OffsetAngles()
Title = "Offset angle axis",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(0),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};

var angleAxis = new AngleAxis
Expand Down Expand Up @@ -134,7 +132,6 @@ public static PlotModel SemiCircle()
Title = "Semi-circle polar plot",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(0),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};
model.Axes.Add(
new AngleAxis
Expand Down Expand Up @@ -168,7 +165,6 @@ public static PlotModel SemiCircleOffsetAngleAxisRange()
Subtitle = "Angle axis range offset to -180 - 180",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(0),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};
model.Axes.Add(
new AngleAxis
Expand Down Expand Up @@ -205,7 +201,6 @@ public static PlotModel EastWestDirections()
Title = "East/west directions",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(0),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};
model.Axes.Add(
new AngleAxis
Expand Down Expand Up @@ -253,7 +248,6 @@ public static PlotModel SemiCircleFullPlotArea()
Subtitle = "The center can be move using the right mouse button",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(1),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};
model.Axes.Add(
new AngleAxisFullPlotArea
Expand Down Expand Up @@ -289,7 +283,6 @@ public static PlotModel ArchimedeanSpiralFullPlotArea()
Subtitle = "The center can be move using the right mouse button",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(1),
PlotMargins = new OxyThickness(60, 20, 4, 40)
};
model.Axes.Add(
new AngleAxisFullPlotArea
Expand Down
10 changes: 10 additions & 0 deletions Source/Examples/ExampleLibrary/Examples/PlotModelExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public static PlotModel PlotMargins()
return model;
}

[Example("Auto PlotMargins")]
public static PlotModel AutoPlotMarginAndAxisLabelWidths()
{
var plotModel1 = new PlotModel { Title = "PlotMargins = (NaN,NaN,NaN,NaN)" };
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = 1e8, EndPosition = 0.5, StringFormat = "f0" });
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = 80, StartPosition = 0.5, StringFormat = "f0" });
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = 0, Maximum = 1e8, StringFormat = "f0", Angle = -90 });
return plotModel1;
}

[Example("No model")]
public static PlotModel NoModel()
{
Expand Down
8 changes: 0 additions & 8 deletions Source/Examples/ExampleLibrary/Issues/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,14 +1725,6 @@ public static PlotModel AxisMinMax()
return plotModel1;
}

[Example("#453: Auto plot margin and width of labels")]
public static PlotModel AutoPlotMarginAndAxisLabelWidths()
{
var plotModel1 = new PlotModel { Title = "Auto plot margin not taking width of axis tick labels into account" };
plotModel1.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = -1e8, Maximum = 1e8 });
return plotModel1;
}

/// <summary>
/// Creates a demo PlotModel with MinimumRange defined
/// and with series with values which are within this range.
Expand Down
68 changes: 60 additions & 8 deletions Source/OxyPlot.Tests/Axes/AxisTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void D04_InvalidLogAxis()
/// Test DesiredSize property to see if working property
/// </summary>
[Test]
public void Axis_DesiredSize()
public void Axis_DesiredMargin()
{
var xaxis = new LinearAxis { Position = AxisPosition.Bottom, Title = "X-axis" };
var yaxis = new LinearAxis { Position = AxisPosition.Left, Title = "Y-axis" };
Expand All @@ -451,18 +451,70 @@ public void Axis_DesiredSize()

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

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

// larger numbers on axis -> larger desired size
yaxis.Zoom(10000, 11000);
xaxis.Zoom(10000, 11000);
plot.UpdateAndRenderToNull(800, 600);

Assert.That(yaxis.DesiredSize.Width, Is.EqualTo(50.0).Within(0.5), "y-axis width");
Assert.That(yaxis.DesiredSize.Height, Is.EqualTo(0.0).Within(1e-6), "y-axis height");
Assert.That(yaxis.DesiredMargin.Left, Is.EqualTo(50.0).Within(0.5), "y-axis left");
Assert.That(yaxis.DesiredMargin.Top, Is.EqualTo(5).Within(0.5), "y-axis top");
Assert.That(yaxis.DesiredMargin.Right, Is.EqualTo(0.0).Within(1e-6), "y-axis right");
Assert.That(yaxis.DesiredMargin.Bottom, Is.EqualTo(5).Within(0.5), "y-axis bottom");

Assert.That(xaxis.DesiredMargin.Left, Is.EqualTo(12.5).Within(0.5), "x-axis left");
Assert.That(xaxis.DesiredMargin.Top, Is.EqualTo(0.0).Within(1e-6), "x-axis top");
Assert.That(xaxis.DesiredMargin.Right, Is.EqualTo(12.5).Within(0.5), "x-axis right");
Assert.That(xaxis.DesiredMargin.Bottom, Is.EqualTo(35.0).Within(0.5), "x-axis bottom");
}

/// <summary>
/// Test DesiredSize property with axis start and end position
/// </summary>
[Test]
public void Axis_DesiredMargin_WithPosition()
{
var plot = new PlotModel();
var axis1 = new LinearAxis { Position = AxisPosition.Bottom, StartPosition = 0, EndPosition = 0.5, Title = "X-axis 1" };
var axis2 = new LinearAxis { Position = AxisPosition.Bottom, StartPosition = 1, EndPosition = 0.5, Title = "X-axis 2" };
plot.Axes.Add(axis1);
plot.Axes.Add(axis2);

axis1.Zoom(0, 80);
axis2.Zoom(0, 80);
plot.UpdateAndRenderToNull(800, 600);
Assert.That(axis1.DesiredMargin.Left, Is.EqualTo(5).Within(0.5), "axis1 left");
Assert.That(axis1.DesiredMargin.Top, Is.EqualTo(0).Within(1e-6), "axis1 top");
Assert.That(axis1.DesiredMargin.Right, Is.EqualTo(0).Within(1e-6), "axis1 right");
Assert.That(axis1.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis1 bottom");

Assert.That(axis2.DesiredMargin.Left, Is.EqualTo(0d).Within(1e-6), "axis2 left");
Assert.That(axis2.DesiredMargin.Top, Is.EqualTo(0d).Within(1e-6), "axis2 top");
Assert.That(axis2.DesiredMargin.Right, Is.EqualTo(5).Within(0.5), "axis2 right");
Assert.That(axis2.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis2 bottom");

// larger numbers on axis -> larger desired size
axis1.Zoom(10000, 11000);
axis2.Zoom(10000, 11000);
plot.UpdateAndRenderToNull(800, 600);
Assert.That(axis1.DesiredMargin.Left, Is.EqualTo(12.5).Within(0.5), "axis1 left");
Assert.That(axis1.DesiredMargin.Top, Is.EqualTo(0).Within(1e-6), "axis1 top");
Assert.That(axis1.DesiredMargin.Right, Is.EqualTo(0).Within(1e-6), "axis1 right");
Assert.That(axis1.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis1 bottom");

Assert.That(axis2.DesiredMargin.Left, Is.EqualTo(0d).Within(1e-6), "axis2 left");
Assert.That(axis2.DesiredMargin.Top, Is.EqualTo(0d).Within(1e-6), "axis2 top");
Assert.That(axis2.DesiredMargin.Right, Is.EqualTo(12.5).Within(0.5), "axis2 right");
Assert.That(axis2.DesiredMargin.Bottom, Is.EqualTo(35).Within(0.5), "axis2 bottom");
}

/// <summary>
Expand Down Expand Up @@ -694,4 +746,4 @@ public void Axis_Toggle_Between_Linear_And_Log_Axis()
Assert.AreEqual(0.004, plot.Axes[0].DataMinimum);
}
}
}
}
4 changes: 2 additions & 2 deletions Source/OxyPlot.Tests/PlotModel/PlotModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public void AutoPlotMargins()
plot.Axes.Add(horizontalAxis);
plot.UpdateAndRenderToNull(800, 600);
Assert.That(plot.ActualPlotMargins.Left, Is.EqualTo(26).Within(1), "left");
Assert.That(plot.ActualPlotMargins.Top, Is.EqualTo(0).Within(1), "top");
Assert.That(plot.ActualPlotMargins.Right, Is.EqualTo(0).Within(1), "right");
Assert.That(plot.ActualPlotMargins.Top, Is.EqualTo(5).Within(1), "top");
Assert.That(plot.ActualPlotMargins.Right, Is.EqualTo(7.5).Within(1), "right");
Assert.That(plot.ActualPlotMargins.Bottom, Is.EqualTo(21).Within(1), "bottom");
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 0 additions & 11 deletions Source/OxyPlot.Wpf.Tests/OxyPlot.Wpf.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,4 @@
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Baseline\PngExporterTests_ExportWithResolution_115dpi.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Baseline\PngExporterTests_ExportWithResolution_192dpi.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Baseline\PngExporterTests_ExportWithResolution_301dpi.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
11 changes: 10 additions & 1 deletion Source/OxyPlot.Wpf.Tests/PngExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void ExportWithResolution(double factor)
var resolution = (int)(96 * factor);
var plotModel = CreateTestModel1();
Directory.CreateDirectory("Actual");
Directory.CreateDirectory("Baseline");
var fileName = string.Format(CultureInfo.InvariantCulture, "PngExporterTests_ExportWithResolution_{0}dpi.png", resolution);
var exporter = new PngExporter { Width = (int)(400 * factor), Height = (int)(300 * factor), Resolution = resolution };
var actual = Path.Combine("Actual", fileName);
Expand All @@ -90,7 +91,15 @@ public void ExportWithResolution(double factor)
}

Assert.IsTrue(File.Exists(actual));
PngAssert.AreEqual(Path.Combine("Baseline", fileName), actual, fileName, Path.Combine("Diff", fileName));
var baselinePath = Path.Combine("Baseline", fileName);
if (File.Exists(baselinePath))
{
PngAssert.AreEqual(baselinePath, actual, fileName, Path.Combine("Diff", fileName));
}
else
{
File.Copy(actual, baselinePath);
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 50e9eb0

Please sign in to comment.