Skip to content

Improve Barchart creation #683

@joshka

Description

@joshka

Problem

BarChart's construction is overly verbose. To construct anything other than the default value / text you need to do:

let data = BarGroup::default().bars(&[
        Bar::default()
            .label("Red".into())
            .value(2)
            .style(Style::new().red()),
        Bar::default()
            .label("Green".into())
            .value(7)
            .style(Style::new().green()),
        Bar::default()
            .label("Blue".into())
            .value(11)
            .style(Style::new().blue()),
    ]);

Solution

  • Bar should have a new() method that accepts a label and value Bar::new("Red", 2)
  • Bar::label and the new new method should accept Into<Line> so we can write Bar::default().label("foo") - Bar should implement styled so that style shorthands work Bar::new("Red", 2").red()
  • BarGroup should have a new method that accepts I: IntoIterator, with I::Item = Into so that Bargroup::new([Bar::new("R", 2").red(), Bar::new("B", 3).blue()]) works
  • BarGroup::label should accept Into
  • BarGroup::bars should accept I:IntoIterator, with I::Item : Into
  • BarChart should have a new function that accepts bars (into iterator item = into bar) (unsure if this should also take direction - probably worth doing it as it highlights that direction is available better than just defaulting it)
  • Docs and unit tests for all of the above

Generally this should aim to be a non-breaking change. There may be places that are under-tested, so make sure to add any missing unit tests. If you can change a method's behavior and some test doesn't break, then there's probably a missing test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort: Good First IssueGood for newcomers without much Rust or Ratatui experiance. Apply only if willing to help implementType: EnhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions