Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Example to Show/Hide Legend (#1470)
- Example of BarSeries stacked and with labels (#1979)
- Example of issue with AreaSeries tracker (#1982)
- BarSeries.LabelAngle property (#1870)

### Changed

Expand Down
13 changes: 13 additions & 0 deletions Source/Examples/ExampleLibrary/Series/BarSeriesExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public static PlotModel WithLabels()
return model;
}

[Example("With labels (at an angle)")]
public static PlotModel WithLabelsAtAnAngle()
{
var model = WithLabels();

foreach (BarSeries b in model.Series)
{
b.LabelAngle = -45;
}

return model;
}

[Example("With labels (Value Axis reversed)")]
public static PlotModel WithLabelsXAxisReversed()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public override void Render(Axis axis, int pass)
//Text rendering
foreach (var value in this.MajorLabelValues.Take(majorTickCount))
{
ScreenPoint pt = TransformToClientRectangle(magnitudeAxis.ClipMaximum, value, axis, this.Plot.PlotArea, magnitudeAxis.MidPoint);
ScreenPoint pt = this.TransformToClientRectangle(magnitudeAxis.ClipMaximum, value, axis, this.Plot.PlotArea, magnitudeAxis.MidPoint);

var angle = Math.Atan2(pt.y - magnitudeAxis.MidPoint.y, pt.x - magnitudeAxis.MidPoint.x);

Expand Down
35 changes: 22 additions & 13 deletions Source/OxyPlot/Series/BarSeries/BarSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public BarSeries()
this.NegativeFillColor = OxyColors.Undefined;
this.TrackerFormatString = DefaultTrackerFormatString;
this.LabelMargin = 2;
this.LabelAngle = 0;
this.StackGroup = string.Empty;
this.StrokeThickness = 0;
}
Expand Down Expand Up @@ -77,10 +78,15 @@ public BarSeries()
public string LabelFormatString { get; set; }

/// <summary>
/// Gets or sets the label margins.
/// Gets or sets the label margins. Default value is 2.
/// </summary>
public double LabelMargin { get; set; }

/// <summary>
/// Gets or sets the label angle in degrees. Default value is 0.
/// </summary>
public double LabelAngle { get; set; }

/// <summary>
/// Gets or sets label placements.
/// </summary>
Expand Down Expand Up @@ -307,40 +313,43 @@ protected void RenderLabel(
double categoryEndValue)
{
var s = StringHelper.Format(this.ActualCulture, this.LabelFormatString, item, item.Value);
HorizontalAlignment ha;
ScreenPoint pt;
var y = (categoryEndValue + categoryValue) / 2;
var sign = Math.Sign(topValue - baseValue);
var marginVector = new ScreenVector(this.LabelMargin, 0) * sign;
var centreVector = new ScreenVector(0, 0);

var size = rc.MeasureText(
s,
this.ActualFont,
this.ActualFontSize,
this.ActualFontWeight,
this.LabelAngle);

switch (this.LabelPlacement)
{
case LabelPlacement.Inside:
pt = this.Transform(topValue, y);
marginVector = -marginVector;
ha = (HorizontalAlignment)sign;
centreVector = new ScreenVector(-sign * size.Width / 2, 0);
break;
case LabelPlacement.Outside:
pt = this.Transform(topValue, y);
ha = (HorizontalAlignment)(-sign);
centreVector = new ScreenVector(sign * size.Width / 2, 0);
break;
case LabelPlacement.Middle:
pt = this.Transform((topValue + baseValue) / 2, y);
marginVector = new ScreenVector(0, 0);
ha = HorizontalAlignment.Center;
break;
case LabelPlacement.Base:
pt = this.Transform(baseValue, y);
ha = (HorizontalAlignment)(-sign);
centreVector = new ScreenVector(sign * size.Width / 2, 0);
break;
default:
throw new ArgumentOutOfRangeException();
}

var va = VerticalAlignment.Middle;
this.Orientate(ref ha, ref va);

pt += this.Orientate(marginVector);
pt += this.Orientate(marginVector) + this.Orientate(centreVector);

rc.DrawText(
pt,
Expand All @@ -349,9 +358,9 @@ protected void RenderLabel(
this.ActualFont,
this.ActualFontSize,
this.ActualFontWeight,
0,
ha,
va);
this.LabelAngle,
HorizontalAlignment.Center,
VerticalAlignment.Middle);
}

/// <inheritdoc/>
Expand Down