Add PathAnnotation.Border properties (#1900) · oxyplot/oxyplot@41f4013 · GitHub
Skip to content

Commit 41f4013

Browse files
authored
Add PathAnnotation.Border properties (#1900)
1 parent 816120c commit 41f4013

5 files changed

Lines changed: 79 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions

‎Source/Examples/ExampleLibrary/Annotations/LineAnnotationExamples.cs‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,48 @@ public static PlotModel LineAnnotationOnLinearAxes()
1919
var model = new PlotModel { Title = "LineAnnotation on linear axes" };
2020
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = -20, Maximum = 80 });
2121
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = -10, Maximum = 10 });
22-
model.Annotations.Add(new LineAnnotation { Slope = 0.1, Intercept = 1, Text = "First" });
2322
model.Annotations.Add(
24-
new LineAnnotation { Slope = 0.3, Intercept = 2, MaximumX = 40, Color = OxyColors.Red, Text = "Second" });
23+
new LineAnnotation
24+
{
25+
Slope = 0.1,
26+
Intercept = 1,
27+
Text = "First",
28+
BorderBackground = OxyColors.White.ChangeOpacity(0.75),
29+
BorderPadding = new OxyThickness(5)
30+
});
31+
model.Annotations.Add(
32+
new LineAnnotation
33+
{
34+
Slope = 0.3,
35+
Intercept = 2,
36+
MaximumX = 40,
37+
Color = OxyColors.Red,
38+
Text = "Second",
39+
BorderBackground = OxyColors.White.ChangeOpacity(0.75),
40+
BorderPadding = new OxyThickness(5)
41+
});
2542
model.Annotations.Add(
2643
new LineAnnotation
2744
{
2845
Type = LineAnnotationType.Vertical,
2946
X = 4,
3047
MaximumY = 10,
3148
Color = OxyColors.Green,
32-
Text = "Vertical"
33-
});
49+
Text = "Vertical",
50+
BorderBackground = OxyColors.White.ChangeOpacity(0.75),
51+
BorderPadding = new OxyThickness(5)
52+
});
3453
model.Annotations.Add(
3554
new LineAnnotation
3655
{
3756
Type = LineAnnotationType.Horizontal,
3857
Y = 2,
3958
MaximumX = 4,
4059
Color = OxyColors.Gold,
41-
Text = "Horizontal"
42-
});
60+
Text = "Horizontal",
61+
BorderBackground = OxyColors.White.ChangeOpacity(0.75),
62+
BorderPadding = new OxyThickness(5)
63+
});
4364
return model;
4465
}
4566

@@ -164,4 +185,4 @@ public static PlotModel ReversedAxes()
164185
return model;
165186
}
166187
}
167-
}
188+
}

‎Source/OxyPlot/Annotations/PathAnnotation.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ protected PathAnnotation()
102102
/// <value>The text padding.</value>
103103
public double TextPadding { get; set; }
104104

105+
/// <summary>
106+
/// Gets or sets the padding of the border rectangle.
107+
/// </summary>
108+
/// <value>The border padding.</value>
109+
public OxyThickness BorderPadding { get; set; }
110+
111+
/// <summary>
112+
/// Gets or sets the fill color of the border rectangle.
113+
/// </summary>
114+
/// <value>The border background color.</value>
115+
public OxyColor BorderBackground { get; set; }
116+
117+
/// <summary>
118+
/// Gets or sets the stroke color of the border rectangle.
119+
/// </summary>
120+
/// <value>The border stroke color.</value>
121+
public OxyColor BorderStroke { get; set; }
122+
123+
/// <summary>
124+
/// Gets or sets the stroke thickness of the border rectangle.
125+
/// </summary>
126+
/// <value>The border stroke thickness.</value>
127+
public double BorderStrokeThickness { get; set; }
128+
105129
/// <summary>
106130
/// Gets or sets the text orientation.
107131
/// </summary>
@@ -254,6 +278,20 @@ public override void Render(IRenderContext rc)
254278
angle = this.TextRotation;
255279
}
256280

281+
var textSize = rc.MeasureText(this.Text, this.ActualFont, this.ActualFontSize, this.ActualFontWeight);
282+
283+
var textBounds = TextAnnotation.GetTextBounds(position, textSize, this.BorderPadding, angle, ha, va);
284+
285+
if ((angle % 90).Equals(0))
286+
{
287+
var actualRect = new OxyRect(textBounds[0], textBounds[2]);
288+
rc.DrawRectangle(actualRect, this.BorderBackground, this.BorderStroke, this.BorderStrokeThickness, this.EdgeRenderingMode);
289+
}
290+
else
291+
{
292+
rc.DrawPolygon(textBounds, this.BorderBackground, this.BorderStroke, this.BorderStrokeThickness, this.EdgeRenderingMode);
293+
}
294+
257295
rc.DrawText(
258296
textPosition,
259297
this.Text,

‎Source/OxyPlot/Annotations/TextAnnotation.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public override void Render(IRenderContext rc)
133133
/// <param name="horizontalAlignment">The horizontal alignment.</param>
134134
/// <param name="verticalAlignment">The vertical alignment.</param>
135135
/// <returns>The background rectangle coordinates.</returns>
136-
private static IList<ScreenPoint> GetTextBounds(
136+
internal static IList<ScreenPoint> GetTextBounds(
137137
ScreenPoint position,
138138
OxySize size,
139139
OxyThickness padding,

‎Source/OxyPlot/Rendering/OxyColorExtensions.cs‎

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)