Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label: incorrect horizontal alignment for multiline strings #3859

Closed
karlipl opened this issue May 22, 2024 · 2 comments · Fixed by #4045
Closed

Label: incorrect horizontal alignment for multiline strings #3859

karlipl opened this issue May 22, 2024 · 2 comments · Fixed by #4045

Comments

@karlipl
Copy link

karlipl commented May 22, 2024

Question: Hi, I have a barplot with a multiline label. Is there a way to center each row of the label?
image
I would like the following result:
-------Row top
--Row in the middle
-----Row bottom

ScottPlot Version: ScottPlot 5.0.34

Code Sample:

Bar[] bars =
{
        new()
        {
            Position = 1,
            Value = 60000,
            BorderColor = Colors.White,
            FillColor = Color.FromHex("E56646"),
            ValueBase = 0,
            Label = $"Row top\nRow in the middle\nRow bottom",
            CenterLabel = true
        },
        new ()
        {
            Position = 1,
            Value = 60000 + 40000,
            BorderColor = Colors.White,
            FillColor = Color.FromHex("68BC5A"),
            ValueBase = 60000,
            Label = $"Row top\nRow in the middle\nRow bottom",
            CenterLabel = true
        }
    };

var barPlot = plot.Add.Bars(bars);

barPlot.ValueLabelStyle.ForeColor = Colors.White;
barPlot.ValueLabelStyle.Bold = true;
barPlot.ValueLabelStyle.FontSize = 16;

This is just a snippet of my code. I could provide a minimum full example if needed.

I would really appreciate some help.

Regards
Karl

@swharden swharden changed the title Center multi line label in barplot Bar: support modern LabelStyle properties May 22, 2024
@swharden
Copy link
Member

swharden commented May 22, 2024

Thanks for reporting this @karlipl! Recently a lot of plottable objects were upgraded to use advanced label styling features, but I think Bar got overlooked because it's in the primitives folder and not the plottables folder. I'll use this issue to track adding these features (including alignment).

In a pinch you could place a text object over your bars! Actually, that has the same problem. Here's the code I used to test that:

using ScottPlot;

ScottPlot.Plot plot = new();

Bar[] bars =
[
    new()
    {
        Position = 1,
        Value = 60000,
        BorderColor = Colors.Transparent,
        FillColor = Color.FromHex("E56646"),
        ValueBase = 0,
    },
    new ()
    {
        Position = 1,
        Value = 60000 + 40000,
        BorderColor = Colors.Transparent,
        FillColor = Color.FromHex("68BC5A"),
        ValueBase = 60000,
    }
];

plot.Add.Bars(bars);
plot.Add.Text($"Row top\nRow in the middle\nRow bottom", bars[0].Position, bars[0].Value);
plot.Add.Text($"Row top\nRow in the middle\nRow bottom", bars[1].Position, bars[1].Value);

foreach (var txt in plot.GetPlottables<ScottPlot.Plottables.Text>())
{
    txt.LabelFontColor = Colors.White;
    txt.LabelBold = true;
    txt.LabelFontSize = 16;
    txt.Alignment = Alignment.UpperCenter;
}

plot.Title(ScottPlot.Version.VersionString);
plot.SavePng("demo.png", 400, 300).LaunchFile();

I think the core issue is that horizontal centering of multiline labels is failing. It's aligning horizontally based on the first line, not the longest line. Here's a simpler demonstration of this issue:

ScottPlot.Plot plot = new();
var txt = plot.Add.Text($"aaa\nbbbbbbbbbbb\nccc", 0, 0);
txt.Alignment = ScottPlot.Alignment.MiddleCenter;
txt.LabelBackgroundColor = ScottPlot.Colors.SkyBlue;
plot.SavePng("demo.png", 400, 300).LaunchFile();

demo

I'll mark this issue high priority and use it to track fixing this problem too 👍

@swharden swharden changed the title Bar: support modern LabelStyle properties Label: incorrect horizontal alignment for multiline strings May 22, 2024
@karlipl
Copy link
Author

karlipl commented May 27, 2024

Hi, thanks for the reply and working on this.

Regards
Karl

swharden added a commit that referenced this issue Jul 5, 2024
swharden added a commit that referenced this issue Jul 5, 2024
* LabelStyle: improve horizontal alignment for multiline strings

resolves #3859
resolves #3958

* Update CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants