Overhaul Text Arrangement and Trimming by VisualMelon · Pull Request #1556 · oxyplot/oxyplot · GitHub
Skip to content
Open
7 changes: 7 additions & 0 deletions CHANGELOG.md
35 changes: 35 additions & 0 deletions Source/Examples/ExampleLibrary/Examples/PerformanceExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ public static PlotModel IntOverflow100k()
return IntOverflow(100000);
}

[Example("Rotated Text")]
public static PlotModel RotatedText()
{
return RotatedText("Rotated Text", null);
}

[Example("Rotated Text (trimmed)")]
public static PlotModel RotatedTextTrimmed()
{
return RotatedText("Rotated Text (trimmed)", new OxySize(100, 50));
}

private static PlotModel IntOverflow(int n)
{
var model = new PlotModel { Title = "int overflow", Subtitle = "n = " + n };
Expand Down Expand Up @@ -350,5 +362,28 @@ private static void AddPoints(ICollection<ScatterPoint> points, int n)
points.Add(new ScatterPoint(x * Math.Cos(x), x * Math.Sin(x)));
}
}

private static PlotModel RotatedText(string title, OxySize? maxSize)
{
var model = new PlotModel { Title = title };
var xaxis = new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = 100 };
var yaxis = new LinearAxis { Position = AxisPosition.Left, Minimum = 0, Maximum = 100 };
model.Axes.Add(xaxis);
model.Axes.Add(yaxis);

model.Annotations.Add(new RenderingCapabilities.DelegateAnnotation(rc =>
{
for (int x = 0; x <= 100; x += 10)
{
for (int y = 0; y <= 100; y += 10)
{
var p = xaxis.Transform(x, y, yaxis);
rc.DrawText(p, $"({x}, {y}): A piece of multiline\ntext that can be trimmed.", OxyColors.Black, null, 16, 400, x + y, HorizontalAlignment.Center, VerticalAlignment.Middle, maxSize);
}
}
}));

return model;
}
}
}
54 changes: 54 additions & 0 deletions Source/Examples/ExampleLibrary/Examples/RenderingCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,60 @@ public static PlotModel DrawMultilineTextAlignmentRotation()
return model;
}

/// <summary>
/// Shows max size capabilities for the DrawText method.
/// </summary>
/// <returns>A plot model.</returns>
[Example("DrawText - Bounded Multi-line Alignment/Rotation")]
public static PlotModel DrawBoundedMultilineTextAlignmentRotation()
{
var model = new PlotModel();
model.Annotations.Add(new DelegateAnnotation(rc =>
{
for (var ha = HorizontalAlignment.Left; ha <= HorizontalAlignment.Right; ha++)
{
for (var va = VerticalAlignment.Top; va <= VerticalAlignment.Bottom; va++)
{
var origin = new ScreenPoint(((int)ha + 2) * 170, ((int)va + 2) * 170);
rc.FillCircle(origin, 3, OxyColors.Blue, EdgeRenderingMode.Adaptive);
for (var rotation = 0; rotation < 360; rotation += 90)
{
rc.DrawText(origin, $"R{rotation:000}\n{ha}\n{va}", OxyColors.Black, fontSize: 20d, rotation: rotation, horizontalAlignment: ha, verticalAlignment: va, maxSize: new OxySize(50, 70));
}
}
}
}));
return model;
}

/// <summary>
/// Shows max size capabilities for the DrawText method.
/// </summary>
/// <returns>A plot model.</returns>
[Example("DrawText - Horizontal Text Trimming")]
public static PlotModel DrawHorizontalTextTrimmed()
{
var text = "This is a long piece of text with many words that barely qualifies as a sentence.";
var multiLineText = "This is a long piece of multiline text\nwith many words that\nbarely qualifies as a sentence.";

var model = new PlotModel();
model.Annotations.Add(new DelegateAnnotation(rc =>
{
for (int i = 0; i < 20; i++)
{
var p = new ScreenPoint(10, 10 + i * 20);
rc.DrawText(p, text, OxyColors.Black, maxSize: new OxySize(i * 20, double.MaxValue));
}

for (int i = 0; i < 5; i++)
{
var p = new ScreenPoint(10, 400 + i * 50);
rc.DrawText(p, multiLineText, OxyColors.Black, maxSize: new OxySize(i * 25, double.MaxValue));
}
}));
return model;
}

/// <summary>
/// Shows color capabilities for the DrawText method.
/// </summary>
Expand Down
12 changes: 11 additions & 1 deletion Source/OxyPlot.ImageSharp.Tests/PngExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,17 @@ public void LargeImageTest(bool interpolate)
public void TestMultilineAlignment()
{
var plotModel = ExampleLibrary.RenderingCapabilities.DrawMultilineTextAlignmentRotation();
var fileName = Path.Combine(this.outputDirectory, "Text.png");
var fileName = Path.Combine(this.outputDirectory, "Multiline-Alignment.png");
PngExporter.Export(plotModel, fileName, 700, 700);

Assert.IsTrue(File.Exists(fileName));
}

[Test]
public void TestBoundedMultilineAlignment()
{
var plotModel = ExampleLibrary.RenderingCapabilities.DrawBoundedMultilineTextAlignmentRotation();
var fileName = Path.Combine(this.outputDirectory, "Bounded-Multiline-Alignment.png");
PngExporter.Export(plotModel, fileName, 700, 700);

Assert.IsTrue(File.Exists(fileName));
Expand Down
210 changes: 210 additions & 0 deletions Source/OxyPlot.ImageSharp.Tests/SvgExporterTests.cs
Loading