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, inchart.negative; their written value keeps its minus sign. Charset::Blocksdraws with eighth-cell precision (█▉▊▋▌▍▎▏),Charset::Braillewith half cells (⣿⡇),Charset::Asciiwith#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::Verticaldraws columns bottom up, as tall asbar_widthrows, or the height it is given (aLayoutregion, 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 itsthickness), 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 explicitthickness,bar_gaporgroup_gapis 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:
groupadds aBarGroup: bars with a label, set apart bygroup_gap(1 cell vertical, 2 rows horizontal, by default).thicknessis ratatui’sbar_width: how many cells wide a vertical bar is, or rows tall a horizontal one.bar_widthkeeps its meaning here: the longest bar’s length, or the tallest column’s height.bar_gapis the space between bars (1 cell vertical, no rows horizontal, by default).Bar::textwrites text instead of the value (76°).label_styleandvalue_style, andBar::label_styleandBar::value_styleover them, style the labels and values.values_insidewrites 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
impl BarChart
Sourcepub fn from_pairs<L: Into<String>>(
pairs: impl IntoIterator<Item = (L, f64)>,
) -> Self
pub fn from_pairs<L: Into<String>>( pairs: impl IntoIterator<Item = (L, f64)>, ) -> Self
A chart of (label, value) pairs.
Sourcepub fn group(self, group: BarGroup) -> Self
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.
Sourcepub fn range(self, min: f64, max: f64) -> Self
pub fn range(self, min: f64, max: f64) -> Self
Fix the scale (default: the values’ range, stretched to include zero). Values outside are clamped.
Sourcepub fn max(self, max: f64) -> Self
pub fn max(self, max: f64) -> Self
Fix only the top of the scale, such as 100.0 for percentages.
Sourcepub fn charset(self, charset: Charset) -> Self
pub fn charset(self, charset: Charset) -> Self
Glyphs to draw with (default Charset::Auto: blocks).
Sourcepub fn orientation(self, orientation: Orientation) -> Self
pub fn orientation(self, orientation: Orientation) -> Self
Which way the bars run (default Orientation::Horizontal).
Sourcepub fn bar_width(self, width: usize) -> Self
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.
Sourcepub fn thickness(self, thickness: usize) -> Self
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.
Sourcepub fn bar_gap(self, gap: usize) -> Self
pub fn bar_gap(self, gap: usize) -> Self
The space between two bars: cells when vertical (default 1), rows when horizontal (default none).
Sourcepub fn group_gap(self, gap: usize) -> Self
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.
Sourcepub fn show_values(self, show: bool) -> Self
pub fn show_values(self, show: bool) -> Self
Write each value after its bar (default on).
Sourcepub fn values_inside(self, inside: bool) -> Self
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.
Sourcepub fn format(self, format: ValueFormat) -> Self
pub fn format(self, format: ValueFormat) -> Self
How values are written (default ValueFormat::Compact).
Sourcepub fn legend(self, legend: bool) -> Self
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.
Sourcepub fn style(self, style: impl Into<String>) -> Self
pub fn style(self, style: impl Into<String>) -> Self
The style of every bar: a theme key or a definition (default
chart.bar).
Sourcepub fn label_style(self, style: impl Into<String>) -> Self
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.
Sourcepub fn value_style(self, style: impl Into<String>) -> Self
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.
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
fn accessible_text(&self, _width: usize) -> String
width where layout matters.Source§impl Renderable for BarChart
impl Renderable for BarChart
fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Source§fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement
fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement
(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
fn fit_to_measurement(&self) -> bool
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>
fn vertical(&self) -> Option<VerticalAlign>
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
fn print_end_line(&self, _options: &ConsoleOptions) -> bool
"\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.