Skip to main content

BarChart

Struct BarChart 

Source
pub struct BarChart { /* private fields */ }
Expand description

Bars: by default horizontal, a label, a bar and the value on each line; with Orientation::Vertical, columns side by side.

  • Lengths are measured from zero on a scale that runs from the smallest to the largest value and always includes zero (override with range). Negative values extend left of zero, at whole-cell precision, in chart.negative; their written value keeps its minus sign.
  • Charset::Blocks draws with eighth-cell precision (█▉▊▋▌▍▎▏), Charset::Braille with half cells (⣿⡇), Charset::Ascii with # and a half =. A value above zero always shows at least a sliver.
  • It measures to the label, the bar (bar_width, 40 by default) and the value. Given less, the bar shrinks to 4 cells, then the values go, then the labels are cut (with …, or . in ASCII), then the bar takes whatever is left.
  • Orientation::Vertical draws columns bottom up, as tall as bar_width rows, or the height it is given (a Layout region, an intuiTUIve node) less its labels and values, or 8 rows. Blocks are eighth-cell (▁▂▃▄▅▆▇█), Braille half-cell (⣤⣿), ASCII # and a half .. Each bar has a slot as wide as the widest label or value (or its thickness), with a cell between slots (bar_gap), and is up to 3 cells thick unless a thickness is set. Labels go under the bars and each value just above its bar; negative values hang below the zero line at whole-cell precision, their values under them. Given less width the values go, then the labels are cut (and dropped below two cells), then the gaps, and bars that still do not fit are left off the right. An explicit thickness, bar_gap or group_gap is kept, as ratatui keeps them: the slots narrow to the thickness, then as many whole bars as fit are drawn, from the first, and the rest are left off.

What ratatui’s BarChart has, by our names:

  • group adds a BarGroup: bars with a label, set apart by group_gap (1 cell vertical, 2 rows horizontal, by default).
  • thickness is ratatui’s bar_width: how many cells wide a vertical bar is, or rows tall a horizontal one. bar_width keeps its meaning here: the longest bar’s length, or the tallest column’s height.
  • bar_gap is the space between bars (1 cell vertical, no rows horizontal, by default).
  • Bar::text writes text instead of the value (76°).
  • label_style and value_style, and Bar::label_style and Bar::value_style over them, style the labels and values.
  • values_inside writes each value on its bar, as ratatui draws them.
use rich::Console;
use rich_ext::chart::{BarChart, Charset};

let console = Console::builder().width(40).color_system(None).build();
let chart = BarChart::new()
    .bar("api", 31.0)
    .bar("web", 12.5)
    .bar("worker", 4.0)
    .bar_width(8);
assert_eq!(
    console.render_export(&chart),
    "api    ████████   31\n\
     web    ███▎     12.5\n\
     worker █           4\n"
);
let ascii = chart.charset(Charset::Ascii);
assert_eq!(
    console.render_export(&ascii),
    "api    ########   31\n\
     web    ###      12.5\n\
     worker #           4\n"
);

Vertical:

use rich::Console;
use rich_ext::chart::{BarChart, Orientation};

let console = Console::builder().width(40).color_system(None).build();
let chart = BarChart::from_pairs([("mon", 4.0), ("tue", 7.5), ("wed", -2.0)])
    .orientation(Orientation::Vertical)
    .bar_width(4);
assert_eq!(
    console.render_export(&chart),
    concat!(
        "    7.5    \n",
        " 4  ███    \n",
        "▅▅▅ ███    \n",
        "███ ███    \n",
        "        ███\n",
        "        -2 \n",
        "mon tue wed\n",
    )
);

Grouped, with the values on the bars:

use rich::Console;
use rich_ext::chart::{Bar, BarChart, BarGroup, Orientation};

let console = Console::builder().width(40).color_system(None).build();
let chart = BarChart::new()
    .group(BarGroup::new("Q1").bar(Bar::new("a", 4.0)).bar(Bar::new("b", 2.0)))
    .group(BarGroup::new("Q2").bar(Bar::new("a", 3.0)).bar(Bar::new("b", 1.0)))
    .orientation(Orientation::Vertical)
    .thickness(3)
    .values_inside(true)
    .bar_width(4);
assert_eq!(
    console.render_export(&chart),
    concat!(
        "███             \n",
        "███      ███    \n",
        "███ ███  ███    \n",
        "█4█ █2█  █3█ █1█\n",
        " a   b    a   b \n",
        "  Q1       Q2   \n",
    )
);

Implementations§

Source§

impl BarChart

Source

pub fn new() -> Self

An empty chart; add rows with bar.

Source

pub fn from_pairs<L: Into<String>>( pairs: impl IntoIterator<Item = (L, f64)>, ) -> Self

A chart of (label, value) pairs.

Source

pub fn bar(self, label: impl Into<String>, value: f64) -> Self

Add a bar.

Source

pub fn push(self, bar: Bar) -> Self

Add a Bar built with its own style, text or value style.

Source

pub fn group(self, group: BarGroup) -> Self

Add a BarGroup: its bars, set apart from the others, with its label. A group without bars is left out, as in ratatui.

Source

pub fn bars(&self) -> &[Bar]

The bars, in order, across their groups.

Source

pub fn range(self, min: f64, max: f64) -> Self

Fix the scale (default: the values’ range, stretched to include zero). Values outside are clamped.

Source

pub fn max(self, max: f64) -> Self

Fix only the top of the scale, such as 100.0 for percentages.

Source

pub fn charset(self, charset: Charset) -> Self

Glyphs to draw with (default Charset::Auto: blocks).

Source

pub fn orientation(self, orientation: Orientation) -> Self

Which way the bars run (default Orientation::Horizontal).

Source

pub fn bar_width(self, width: usize) -> Self

Cells for the longest bar (default 40, at least 1); rows for the tallest when Orientation::Vertical (default: the height given, less labels and values, or 8; at most 65535). For how thick a bar is, ratatui’s bar_width, see thickness.

Source

pub fn thickness(self, thickness: usize) -> Self

How thick each bar is (ratatui’s bar_width): cells across a vertical bar, rows down a horizontal one (at least 1). By default a horizontal bar is a row and a vertical one as thick as its slot, up to 3 cells. A horizontal bar’s label and value sit on its middle row.

Source

pub fn bar_gap(self, gap: usize) -> Self

The space between two bars: cells when vertical (default 1), rows when horizontal (default none).

Source

pub fn group_gap(self, gap: usize) -> Self

The space between two groups, on top of the bar_gap: cells when vertical (default 1), rows when horizontal (default 2). A horizontal group’s label takes the first of those rows, or a row of its own when there are none.

Source

pub fn show_values(self, show: bool) -> Self

Write each value after its bar (default on).

Source

pub fn values_inside(self, inside: bool) -> Self

Write each value on its bar, as ratatui draws values: from the start of a horizontal bar, past its end when it is longer; on the bottom row of a vertical one, centred and cut to its thickness. Values on bars take no room of their own. Without a value style they are the bar’s colour reversed, so they read on the bar.

Source

pub fn format(self, format: ValueFormat) -> Self

How values are written (default ValueFormat::Compact).

Source

pub fn legend(self, legend: bool) -> Self

Show the legend of a chart with stacked groups: each part’s marker and name (default on). Other charts have none.

Source

pub fn style(self, style: impl Into<String>) -> Self

The style of every bar: a theme key or a definition (default chart.bar).

Source

pub fn label_style(self, style: impl Into<String>) -> Self

The labels’ style, over chart.label: a theme key or a definition. It styles the group labels too.

Source

pub fn value_style(self, style: impl Into<String>) -> Self

The values’ style, over chart.value: a theme key or a definition.

Trait Implementations§

Source§

impl AccessibleText for BarChart

Bar chart, 4 bars, values from 12 to 412, then every bar (up to six) or the three largest and the smallest. A grouped bar is named with its group’s label first.

Source§

fn accessible_text(&self, _width: usize) -> String

The content as plain lines, laid out for width where layout matters.
Source§

impl Clone for BarChart

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BarChart

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BarChart

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Draw for BarChart

Source§

fn draw( &self, console: &Console, options: &ConsoleOptions, screen: &mut Screen, rect: Rect, )

Draw into rect of screen, rendered with options (its width and height are the rectangle’s): the cells screen.write_lines(rect, &console.render_lines(self, options, false)) would give.
Source§

impl From<BarChart> for Facet

Source§

fn from(chart: BarChart) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for BarChart

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Renderable for BarChart

Source§

fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Source§

fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement

The (minimum, maximum) cell width this renderable wants. Port of __rich_measure__. The default is what upstream’s Measurement.get returns for a renderable without one: anything from nothing to the whole width, (0, max_width).
Source§

fn fit_to_measurement(&self) -> bool

Whether a top-level Console::print shrinks this renderable to its measured width. Upstream renders every top-level renderable at the full console width, so the default is false; an extension may opt in.
Source§

fn vertical(&self) -> Option<VerticalAlign>

The vertical alignment a Table cell holding this renderable uses in place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.
Source§

fn print_end_line(&self, _options: &ConsoleOptions) -> bool

Whether a top-level print ends this renderable’s last line. This port’s renderables separate lines, where upstream’s end each line with "\n", so a print adds the final line break. A renderable whose upstream output stops without one (Screen, LiveRender, a Rule whose end has no newline) returns false, and the next output continues its last line. options are the print’s options.
Source§

impl StructuralPartialEq for BarChart

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more