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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ All notable changes to this project will be documented in this file.
## Unreleased

### Added
- Example of BarSeries stacked and with labels (#1979)

### Changed

### Removed

### Fixed
- Placement of BarSeries labels when stacked (#1979)

## [2.1.2] - 2022-12-03

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

[Example("Stacked (labels)")]
public static PlotModel StackedLabels()
{
var model = new PlotModel { Title = "Stacked with Labels" };

int i = 0;
foreach (var placement in new[] { LabelPlacement.Base, LabelPlacement.Inside, LabelPlacement.Middle, LabelPlacement.Outside })
{
var categoryAxis = new CategoryAxis
{
Title = "Category",
Position = AxisPosition.Left,
StartPosition = 1 - i * 0.25,
EndPosition = 0.75 - i * 0.25,
Key = $"C{i}",
};

var bs1 = new BarSeries { Title = placement.ToString(), LabelPlacement = placement, LabelFormatString = "{0:0}", IsStacked = true, YAxisKey = categoryAxis.Key };
bs1.Items.Add(new BarItem { Value = 5 });
bs1.Items.Add(new BarItem { Value = 10 });
bs1.Items.Add(new BarItem { Value = 15 });
model.Series.Add(bs1);

var bs2 = new BarSeries { Title = placement.ToString(), LabelPlacement = placement, LabelFormatString = "{0:0}", IsStacked = true, YAxisKey = categoryAxis.Key };
bs2.Items.Add(new BarItem { Value = 15 });
bs2.Items.Add(new BarItem { Value = 10 });
bs2.Items.Add(new BarItem { Value = 5 });
model.Series.Add(bs2);

categoryAxis.Labels.AddRange(new[] { "A", "B", "C" });
model.Axes.Add(categoryAxis);

i++;
}

model.Legends.Add(new Legend());

return model;
}

[Example("GapWidth 0%")]
public static PlotModel GapWidth0()
{
Expand Down
2 changes: 1 addition & 1 deletion Source/OxyPlot/Series/BarSeries/BarSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ protected void RenderLabel(
HorizontalAlignment ha;
ScreenPoint pt;
var y = (categoryEndValue + categoryValue) / 2;
var sign = Math.Sign(item.Value - baseValue);
var sign = Math.Sign(topValue - baseValue);
var marginVector = new ScreenVector(this.LabelMargin, 0) * sign;

switch (this.LabelPlacement)
Expand Down