Bar with Custom Text
Full control over bar labels may be achieved by placing fully customizable text objects on top of bars according to their dimensions.
ScottPlot.Plot myPlot = new();
ScottPlot.Palettes.Category10 palette = new();
// create 5 groups of stacked bars
for (int i = 0; i < 5; i++)
{
// create 3 individual bars with stacking
List<ScottPlot.Bar> bars = [];
double valueBase = 0;
for (int j = 0; j < 3; j++)
{
double barSize = Generate.RandomInteger(10, 20);
ScottPlot.Bar bar1 = new()
{
FillColor = palette.GetColor(j),
Position = i,
ValueBase = valueBase,
Value = valueBase + barSize,
Label = $"{barSize}",
CenterLabel = true,
};
bars.Add(bar1);
valueBase += barSize;
}
// plot the stacked bars
var barPlot = myPlot.Add.Bars(bars);
barPlot.Horizontal = true;
}
// style the plot so the bars start on the left edge
myPlot.Axes.Margins(left: 0);
// add custom group labels
double[] tickPositions = Generate.Consecutive(5);
string[] tickLabels = Enumerable.Range(1, 5).Select(x => $"Worker #{x}").ToArray();
myPlot.Axes.Left.SetTicks(tickPositions, tickLabels);
myPlot.SavePng("demo.png", 400, 300);
This recipe is one of many in the Bar Plot category
