ratatui: Barchart doesn't render labels in entire width

Description

The barchart widget doesn’t render labels that are the full width (this affects both non-unicode and unicode)

To Reproduce

fn render_simple_barchart(area: Rect, buf: &mut Buffer) {
    let data = [
        ("Sat", 76),
        ("Sun", 69),
        ("Mon", 65),
        ("Tue", 67),
        ("Wed", 65),
        ("Thu", 69),
        ("Fri", 73),
    ];
    let data = data
        .into_iter()
        .map(|(label, value)| {
            Bar::default()
                .value(value)
                // cannot use this until the column width bug is fixed
                .text_value(format!("{}°", value))
                .label(label.into())
        })
        .collect_vec();
    let group = BarGroup::default().bars(&data);
    BarChart::default()
        .data(group)
        .bar_width(3)
        .bar_gap(1)
        .render(area, buf);
}
image

with the following line removed:

                .text_value(format!("{}°", value))
image

And the same occurs replacing that with

                .text_value(format!("{}x", value))

Expected behavior

I think this should probably render like the following (hacking a couple of things in the barchart implementation to make it work):

image

Environment

Mac/iterm/FiraCode(NF)/main/crossterm

Additional context

  • Is this expected behavior for some historical reason?
  • If this is expected, how should it be documented?
  • What significance should unicode play here (e.g. the degree symbol has string len = 2 and unicode width = 1)? Is #75 relevant to the degree symbol?
  • What is the history of this? Check for any guidance from commits, and comments on PRs/ issues in Ratatui and tui-rs.

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 19 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I actually think this is what I’d prefer:

  1 2 3 4 5 6 7 8 
  ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ 
a b c d e f g h i 

and I’d prefer this if there’s lesser room:

  ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ 
a b c d e f g h i 

And this with a height of just 1:

  ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ 

My reasoning is that if someone is using the barchart widget, not getting any bar chart is an unexpected experience.

Please consider adding each of those as tests (rendering that data into 1,2,3 height), as they’re good characterization tests to have if we decide to make it possible to set the label position.

I’m not following the 2 chars width suggestion you made image