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

Horizontal Barchart does not print labels #499

Closed
joshka opened this issue Sep 13, 2023 · 7 comments · Fixed by #518
Closed

Horizontal Barchart does not print labels #499

joshka opened this issue Sep 13, 2023 · 7 comments · Fixed by #518
Labels
bug Something isn't working

Comments

@joshka
Copy link
Member

joshka commented Sep 13, 2023

Description

Horizontal Barcharts were added recently (0.23.0), but labels were not implemented.
Barcharts have two label types: label and text_value (a formatted version of the value), only the latter is rendered.

To Reproduce

let data = vec![
    ("Jan", 10),
    ("Feb", 20),
    ("Mar", 30),
    ("Apr", 40),
    ("May", 50),
    ("Jun", 60),
];
BarChart::default()
    .direction(Direction::Horizontal)
    .data(&data)
    .render(area, buf);
10█████

20█████████████

30█████████████████████

40████████████████████████████

50████████████████████████████████████

60████████████████████████████████████████████

Expected behavior

Jan 10█████

Feb 20█████████████

Mar 30█████████████████████

Apr 40████████████████████████████

May 50████████████████████████████████████

Jun 60████████████████████████████████████████████

Screenshots

Environment

version 0.23-alpha (main branch as of today)

Additional context

@joshka joshka added the bug Something isn't working label Sep 13, 2023
@Valentin271
Copy link
Member

Valentin271 commented Sep 19, 2023

I can work on that.

I'm wondering how should we handle something like this.

let data = vec![
    ("January", 10),
    ("February", 20),
    ("March", 30),
    ("April", 40),
    ("May", 50),
    ("June", 60),
];

Bars should always be aligned so we have to have a max label length somehow.

I'd suggest having an option to configure the max_label_len (feel free to propose a better naming). If not defined, we'd use a default value - either an arbitrary value like 3, the longest label length or maybe the average length.

@karthago1
Copy link
Contributor

this is not a bug, It is a feature to use the space more efficiently 😃 and force you to print what you need on the bar instead

@Valentin271
Copy link
Member

I'll wait to see what maintainers have to say.

I understand your logic but then there is a useless setter when using horizonal bars.

I'd say it's better to add the label as requested here, so users have the choice, and document the fact that this will use space and that could be avoided by using your technique.

@joshka
Copy link
Member Author

joshka commented Sep 21, 2023

I don't quite understand the rationale / implementation in #518 of the length constraint. Looking at this test example:

BarChart::default()
            .direction(Direction::Horizontal)
            .bar_gap(0)
            .data(&[("January", 10), ("February", 20), ("March", 5)])
            .label_max_len(1);

Which renders as:

J 10██    
F 20██████
M 5█      
  1. Why wouldn't a user pass in single letter labels instead of the full label?
  2. Why does it make sense to do the truncation of the labels in the widget instead of outside?

@Valentin271
Copy link
Member

I just thought it would be useful.

Why wouldn't a user pass in single letter labels instead of the full label?

I was thinking of some API (whether it be web, system, or from crate) that may return labels of different length. In that case it would be annoying to truncate the labels.
But thinking about it again, maybe it's not such a common use case.

Why does it make sense to do the truncation of the labels in the widget instead of outside?

Both way are perfectly fine, just giving the possibility to the user.

Overall, if you think this is too much, let's just remove it and only merge the fix.

@joshka
Copy link
Member Author

joshka commented Sep 21, 2023

+1 Let's take this bit out for now like you said.

it would be annoying to truncate the labels

Its this bit I'm confused about - if you're getting the data from somewhere else, the likelyhood it's exactly in the shape of &[(&'a str, u64)] seems low, so you're probably massaging it already and can add a [..1] on your string during that mapping just as easily as a .label_max_len(1)

@Valentin271
Copy link
Member

Fair enough. Kept only the fix.

joshka pushed a commit that referenced this issue Sep 22, 2023
Labels were missed in the initial implementation of the horizontal
mode for the BarChart widget. This adds them.

Fixes #499
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants